7 #include "imstkColor.h" 8 #include "imstkLogger.h" 15 isColorRangeCorrect(
double c)
17 return (c >= 0 && c <= 1.0);
21 Color Color::Black(0.0, 0.0, 0.0, 1.0);
22 Color Color::DarkGray(0.3, 0.3, 0.3, 1.0);
23 Color Color::LightGray(0.8, 0.8, 0.8, 1.0);
24 Color Color::Blue(0.0, 0.0, 1.0, 1.0);
25 Color Color::Green(0.0, 1.0, 0.0, 1.0);
26 Color Color::Red(1.0, 0.0, 0.0, 1.0);
27 Color Color::Yellow(1.0, 1.0, 0.0, 1.0);
28 Color Color::Orange(1.0, 0.6, 0.0, 1.0);
29 Color Color::Pink(1.0, 0.0, 1.0, 1.0);
30 Color Color::Teal(0.5, 1.0, 0.8, 1.0);
31 Color Color::Marigold(0.9, 0.9, 0.4);
32 Color Color::YellowBone(0.828, 0.785, 0.501);
33 Color Color::Bone(0.89, 0.86, 0.79);
34 Color Color::Blood(0.4, 0.0, 0.0);
35 Color Color::LightSkin(1.0, 0.86, 0.7);
36 Color Color::DarkSkin(0.38, 0.27, 0.18);
46 Color::Color(
const double red,
54 Color::Color(
const double* rgba_color)
56 setValue(rgba_color[0], rgba_color[1], rgba_color[2], rgba_color[3]);
59 Color::Color(
const Color& color,
const double alpha)
61 setValue(color.r, color.g, color.b, alpha);
67 rgba[0] = p_color.rgba[0];
68 rgba[1] = p_color.rgba[1];
69 rgba[2] = p_color.rgba[2];
70 rgba[3] = p_color.rgba[3];
77 if (p_i < 0 || p_i > 3)
88 os <<
"R = " << c.r <<
'\n' 89 <<
"G = " << c.g <<
'\n' 90 <<
"B = " << c.b <<
'\n' 101 if (!isColorRangeCorrect(p_red)
102 || !isColorRangeCorrect(p_green)
103 || !isColorRangeCorrect(p_blue)
104 || !isColorRangeCorrect(p_alpha))
106 LOG(WARNING) <<
"Can not set Color: value outside of [0.0, 1.0] range.";
131 Color::darken(
const Color color,
const double factor)
133 return clamp(color - color * factor, Color::Black,
Color::White);
137 Color::lighten(
const Color color,
const double factor)
139 return clamp(color + color * factor, Color::Black,
Color::White);
146 (((color.r < min.r) ? min.r : color.r) > max.r) ? max.r : color.r,
147 (((color.g < min.g) ? min.g : color.g) > max.g) ? max.g : color.g,
148 (((color.b < min.b) ? min.b : color.b) > max.b) ? max.b : color.b,
149 (((color.a < min.a) ? min.a : color.a) > max.a) ? max.a : color.a);
155 return start + (end - start) * t;
159 Color::lerpRgb(
const Color& start,
const Color& end,
const double t)
161 return Color(start + (end - start) * t, 1.0);
167 std::stringstream ss;
168 const int red =
static_cast<int>(r * 255.0);
169 const int green =
static_cast<int>(g * 255.0);
170 const int blue =
static_cast<int>(b * 255.0);
171 ss << std::setfill(
'0') << std::setw(2) << std::hex << red;
172 ss << std::setfill(
'0') << std::setw(2) << std::hex << green;
173 ss << std::setfill(
'0') << std::setw(2) << std::hex << blue;
181 results.r = color_lhs.r * color_rhs.r;
182 results.g = color_lhs.g * color_rhs.g;
183 results.b = color_lhs.b * color_rhs.b;
184 results.a = color_lhs.a * color_rhs.a;
192 results.r = color_lhs.r * intensity_rhs;
193 results.g = color_lhs.g * intensity_rhs;
194 results.b = color_lhs.b * intensity_rhs;
195 results.a = color_lhs.a * intensity_rhs;
200 operator*=(
const Color& color_lhs,
const Color& color_rhs)
203 results.r = color_lhs.r * color_rhs.r;
204 results.g = color_lhs.g * color_rhs.g;
205 results.b = color_lhs.b * color_rhs.b;
206 results.a = color_lhs.a * color_rhs.a;
211 operator*=(
const Color& color_lhs,
const double intensity_rhs)
214 results.r = color_lhs.r * intensity_rhs;
215 results.g = color_lhs.g * intensity_rhs;
216 results.b = color_lhs.b * intensity_rhs;
217 results.a = color_lhs.a * intensity_rhs;
225 results.r = color_lhs.r + color_rhs.r;
226 results.g = color_lhs.g + color_rhs.g;
227 results.b = color_lhs.b + color_rhs.b;
228 results.a = color_lhs.a + color_rhs.a;
236 results.r = color_lhs.r + intensity_rhs;
237 results.g = color_lhs.g + intensity_rhs;
238 results.b = color_lhs.b + intensity_rhs;
239 results.a = color_lhs.a + intensity_rhs;
244 operator+=(
const Color& color_lhs,
const Color& color_rhs)
247 results.r = color_lhs.r + color_rhs.r;
248 results.g = color_lhs.g + color_rhs.g;
249 results.b = color_lhs.b + color_rhs.b;
250 results.a = color_lhs.a + color_rhs.a;
255 operator+=(
const Color& color_lhs,
const double intensity_rhs)
258 results.r = color_lhs.r + intensity_rhs;
259 results.g = color_lhs.g + intensity_rhs;
260 results.b = color_lhs.b + intensity_rhs;
261 results.a = color_lhs.a + intensity_rhs;
269 results.r = color_lhs.r - color_rhs.r;
270 results.g = color_lhs.g - color_rhs.g;
271 results.b = color_lhs.b - color_rhs.b;
272 results.a = color_lhs.a - color_rhs.a;
280 results.r = color_lhs.r - intensity_rhs;
281 results.g = color_lhs.g - intensity_rhs;
282 results.b = color_lhs.b - intensity_rhs;
283 results.a = color_lhs.a - intensity_rhs;
288 operator-=(
const Color& color_lhs,
const Color& color_rhs)
291 results.r = color_lhs.r - color_rhs.r;
292 results.g = color_lhs.g - color_rhs.g;
293 results.b = color_lhs.b - color_rhs.b;
294 results.a = color_lhs.a - color_rhs.a;
299 operator-=(
const Color& color_lhs,
const double intensity_rhs)
302 results.r = color_lhs.r - intensity_rhs;
303 results.g = color_lhs.g - intensity_rhs;
304 results.b = color_lhs.b - intensity_rhs;
305 results.a = color_lhs.a - intensity_rhs;
312 return (color_lhs.r == color_rhs.r) && (color_lhs.g == color_rhs.g) && (color_lhs.b == color_rhs.b);
316 operator!=(
const Color& color_lhs,
const Color& color_rhs)
318 return (color_lhs.r != color_rhs.r) || (color_lhs.g != color_rhs.g) || (color_lhs.b != color_rhs.b);
Color operator-(const Color &color_lhs, const Color &color_rhs)
Subtraction operators.
double operator()(const int p_i) const
returns the color value given with the index
friend std ::ostream & operator<<(std::ostream &os, const Color &c)
Bitwise operator.
void setValue(const double p_red, const double p_green, const double p_blue, const double p_alpha=1.0)
set RGB color
static Color lerpRgba(const Color &start, const Color &end, const double t)
interpolate between two colors by ratio t
static Color White
Various commonly used colors.
const double * getValue() const
get RGB color
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Color & operator=(const Color &p_color)
Equality operator.
Color operator*(const Color &color_lhs, const Color &color_rhs)
Multiplication operators.
Color operator+(const Color &color_lhs, const Color &color_rhs)
Addition operators.