iMSTK
Interactive Medical Simulation Toolkit
imstkColor.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkColor.h"
8 #include "imstkLogger.h"
9 
10 #include <iomanip>
11 
12 namespace imstk
13 {
14 bool
15 isColorRangeCorrect(double c)
16 {
17  return (c >= 0 && c <= 1.0);
18 }
19 
20 Color Color::White(1.0, 1.0, 1.0, 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);
37 
38 Color::Color()
39 {
40  rgba[0] = 0.8;
41  rgba[1] = 0.8;
42  rgba[2] = 0.8;
43  rgba[3] = 1.0;
44 }
45 
46 Color::Color(const double red,
47  const double green,
48  const double blue,
49  const double alpha)
50 {
51  setValue(red, green, blue, alpha);
52 }
53 
54 Color::Color(const double* rgba_color)
55 {
56  setValue(rgba_color[0], rgba_color[1], rgba_color[2], rgba_color[3]);
57 }
58 
59 Color::Color(const Color& color, const double alpha)
60 {
61  setValue(color.r, color.g, color.b, alpha);
62 }
63 
64 Color&
65 Color::operator=(const Color& p_color)
66 {
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];
71  return *this;
72 }
73 
74 double
75 Color::operator()(int p_i) const
76 {
77  if (p_i < 0 || p_i > 3)
78  {
79  return -1;
80  }
81 
82  return rgba[p_i];
83 }
84 
85 std::ostream&
86 operator<<(std::ostream& os, const Color& c)
87 {
88  os << "R = " << c.r << '\n'
89  << "G = " << c.g << '\n'
90  << "B = " << c.b << '\n'
91  << "A = " << c.a;
92  return os;
93 }
94 
95 void
96 Color::setValue(const double p_red,
97  const double p_green,
98  const double p_blue,
99  const double p_alpha)
100 {
101  if (!isColorRangeCorrect(p_red)
102  || !isColorRangeCorrect(p_green)
103  || !isColorRangeCorrect(p_blue)
104  || !isColorRangeCorrect(p_alpha))
105  {
106  LOG(WARNING) << "Can not set Color: value outside of [0.0, 1.0] range.";
107  }
108 
109  rgba[0] = p_red;
110  rgba[1] = p_green;
111  rgba[2] = p_blue;
112  rgba[3] = p_alpha;
113 }
114 
115 void
116 Color::getValue(double color[4])
117 {
118  color[0] = rgba[0];
119  color[1] = rgba[1];
120  color[2] = rgba[2];
121  color[3] = rgba[3];
122 }
123 
124 const double*
126 {
127  return rgba;
128 }
129 
130 Color
131 Color::darken(const Color color, const double factor)
132 {
133  return clamp(color - color * factor, Color::Black, Color::White);
134 }
135 
136 Color
137 Color::lighten(const Color color, const double factor)
138 {
139  return clamp(color + color * factor, Color::Black, Color::White);
140 }
141 
142 Color
143 Color::clamp(const Color color, const Color min, const Color max)
144 {
145  return Color(
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);
150 }
151 
152 Color
153 Color::lerpRgba(const Color& start, const Color& end, const double t)
154 {
155  return start + (end - start) * t;
156 }
157 
158 Color
159 Color::lerpRgb(const Color& start, const Color& end, const double t)
160 {
161  return Color(start + (end - start) * t, 1.0);
162 }
163 
164 std::string
166 {
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;
174  return ss.str();
175 }
176 
177 Color
178 operator*(const Color& color_lhs, const Color& color_rhs)
179 {
180  Color results;
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;
185  return results;
186 }
187 
188 Color
189 operator*(const Color& color_lhs, const double intensity_rhs)
190 {
191  Color results;
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;
196  return results;
197 }
198 
199 Color
200 operator*=(const Color& color_lhs, const Color& color_rhs)
201 {
202  Color results;
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;
207  return results;
208 }
209 
210 Color
211 operator*=(const Color& color_lhs, const double intensity_rhs)
212 {
213  Color results;
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;
218  return results;
219 }
220 
221 Color
222 operator+(const Color& color_lhs, const Color& color_rhs)
223 {
224  Color results;
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;
229  return results;
230 }
231 
232 Color
233 operator+(const Color& color_lhs, const double intensity_rhs)
234 {
235  Color results;
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;
240  return results;
241 }
242 
243 Color
244 operator+=(const Color& color_lhs, const Color& color_rhs)
245 {
246  Color results;
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;
251  return results;
252 }
253 
254 Color
255 operator+=(const Color& color_lhs, const double intensity_rhs)
256 {
257  Color results;
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;
262  return results;
263 }
264 
265 Color
266 operator-(const Color& color_lhs, const Color& color_rhs)
267 {
268  Color results;
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;
273  return results;
274 }
275 
276 Color
277 operator-(const Color& color_lhs, const double intensity_rhs)
278 {
279  Color results;
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;
284  return results;
285 }
286 
287 Color
288 operator-=(const Color& color_lhs, const Color& color_rhs)
289 {
290  Color results;
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;
295  return results;
296 }
297 
298 Color
299 operator-=(const Color& color_lhs, const double intensity_rhs)
300 {
301  Color results;
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;
306  return results;
307 }
308 
309 bool
310 operator==(const Color& color_lhs, const Color& color_rhs)
311 {
312  return (color_lhs.r == color_rhs.r) && (color_lhs.g == color_rhs.g) && (color_lhs.b == color_rhs.b);
313 }
314 
315 bool
316 operator!=(const Color& color_lhs, const Color& color_rhs)
317 {
318  return (color_lhs.r != color_rhs.r) || (color_lhs.g != color_rhs.g) || (color_lhs.b != color_rhs.b);
319 }
320 } // namespace imstk
Color operator-(const Color &color_lhs, const Color &color_rhs)
Subtraction operators.
Definition: imstkColor.cpp:266
Compound Geometry.
double operator()(const int p_i) const
returns the color value given with the index
Definition: imstkColor.cpp:75
friend std ::ostream & operator<<(std::ostream &os, const Color &c)
Bitwise operator.
Definition: imstkColor.cpp:86
void setValue(const double p_red, const double p_green, const double p_blue, const double p_alpha=1.0)
set RGB color
Definition: imstkColor.cpp:96
static Color lerpRgba(const Color &start, const Color &end, const double t)
interpolate between two colors by ratio t
Definition: imstkColor.cpp:153
std::string rgbHex()
Definition: imstkColor.cpp:165
Color in RGB space.
Definition: imstkColor.h:24
static Color White
Various commonly used colors.
Definition: imstkColor.h:112
const double * getValue() const
get RGB color
Definition: imstkColor.cpp:125
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Definition: imstkColor.cpp:310
Color & operator=(const Color &p_color)
Equality operator.
Definition: imstkColor.cpp:65
Color operator*(const Color &color_lhs, const Color &color_rhs)
Multiplication operators.
Definition: imstkColor.cpp:178
Color operator+(const Color &color_lhs, const Color &color_rhs)
Addition operators.
Definition: imstkColor.cpp:222