MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Color.hpp
Go to the documentation of this file.
1 #ifndef COLOR_H
2 #define COLOR_H
3 
9 #include <Config.hpp>
10 
11 #include <Clamp.hpp>
12 #include <math.h>
13 #include <QColor>
14 #include <Vector.hpp>
15 
16 namespace mgx {
17 namespace util {
22 template <class T> class Color : public Vector<4, T> {
23 public:
27  template <typename T1>
28  Color(const Vector<4, T1>& color, const T& scale = 1)
29  : Vector<4, T>(color[0] * scale, color[1] * scale, color[2] * scale, color[3] * scale)
30  {
31  }
32 
33  template <typename T1>
34  Color(const Vector<4, T1>& color, const T1& scale)
35  : Vector<4, T>(color[0] * scale, color[1] * scale, color[2] * scale, color[3] * scale)
36  {
37  }
38 
39  Color(const Vector<4, T>& copy)
40  : Vector<4, T>(copy)
41  {
42  }
43 
44  Color(const QColor& c) {
45  convertFromQColor(*this, c);
46  }
47 
55  Color(const T& r = T(), const T& g = T(), const T& b = T(), const T& a = T())
56  : Vector<4, T>(r, g, b, a)
57  {
58  }
59 
63  T& r() {
64  return this->x();
65  }
66 
70  T& g() {
71  return this->y();
72  }
73 
77  T& b() {
78  return this->z();
79  }
80 
84  T& a() {
85  return this->t();
86  }
87 
91  const T& r() const {
92  return this->x();
93  }
94 
98  const T& g() const {
99  return this->y();
100  }
101 
105  const T& b() const {
106  return this->z();
107  }
108 
112  const T& a() const {
113  return this->t();
114  }
115 
119  void r(const T& val) {
120  this->x(val);
121  }
122 
126  void g(const T& val) {
127  this->y(val);
128  }
129 
133  void b(const T& val) {
134  this->z(val);
135  }
136 
140  void a(const T& val) {
141  this->t(val);
142  }
143 
144  Color<T>& operator=(const Color<T>& c);
145  Color<T>& operator=(const Vector<4, T>& c);
146  Color<T>& operator=(const T& val);
147  Color<T>& operator=(const QColor& c) {
148  convertFromQColor(*this, c);
149  }
150 
151  operator QColor() const { return convertToQColor(*this); }
152 };
153 
154 mgx_EXPORT QColor convertToQColor(const Color<float>& c);
155 mgx_EXPORT QColor convertToQColor(const Color<double>& c);
156 mgx_EXPORT QColor convertToQColor(const Color<long double>& c);
157 mgx_EXPORT QColor convertToQColor(const Color<unsigned char>& c);
158 mgx_EXPORT QColor convertToQColor(const Color<unsigned short>& c);
159 mgx_EXPORT QColor convertToQColor(const Color<unsigned int>& c);
160 mgx_EXPORT QColor convertToQColor(const Color<unsigned long>& c);
161 mgx_EXPORT QColor convertToQColor(const Color<unsigned long long>& c);
162 mgx_EXPORT QColor convertToQColor(const Color<char>& c);
163 mgx_EXPORT QColor convertToQColor(const Color<short>& c);
164 mgx_EXPORT QColor convertToQColor(const Color<int>& c);
165 mgx_EXPORT QColor convertToQColor(const Color<long>& c);
166 mgx_EXPORT QColor convertToQColor(const Color<long long>& c);
167 
168 mgx_EXPORT void convertFromQColor(Color<float>& c, const QColor& col);
169 mgx_EXPORT void convertFromQColor(Color<double>& c, const QColor& col);
170 mgx_EXPORT void convertFromQColor(Color<long double>& c, const QColor& col);
171 mgx_EXPORT void convertFromQColor(Color<unsigned char>& c, const QColor& col);
172 mgx_EXPORT void convertFromQColor(Color<unsigned short>& c, const QColor& col);
173 mgx_EXPORT void convertFromQColor(Color<unsigned int>& c, const QColor& col);
174 mgx_EXPORT void convertFromQColor(Color<unsigned long>& c, const QColor& col);
175 mgx_EXPORT void convertFromQColor(Color<unsigned long long>& c, const QColor& col);
176 mgx_EXPORT void convertFromQColor(Color<char>& c, const QColor& col);
177 mgx_EXPORT void convertFromQColor(Color<short>& c, const QColor& col);
178 mgx_EXPORT void convertFromQColor(Color<int>& c, const QColor& col);
179 mgx_EXPORT void convertFromQColor(Color<long>& c, const QColor& col);
180 mgx_EXPORT void convertFromQColor(Color<long long>& c, const QColor& col);
181 
183 template <class T> Color<T>& Color<T>::operator=(const Color<T>& c)
184 {
185  this->Vector<4, T>::operator=(c);
186  return *this;
187 }
188 
189 template <class T> Color<T>& Color<T>::operator=(const T& val)
190 {
191  this->Vector<4, T>::operator=(val);
192  return *this;
193 }
194 
195 template <class T> Color<T>& Color<T>::operator=(const Vector<4, T>& c)
196 {
197  this->Vector<4, T>::operator=(c);
198  return *this;
199 }
200 
202 template <class T> Color<T> convertHSVtoRGB(T h, T s, T v)
203 {
204  // based on Jo's code in medit
205 
206  Color<T> rgb;
207  rgb.a(1.0);
208 
209  while(h > 360.0)
210  h -= 360.0;
211  while(h < 0.0)
212  h += 360.0;
213 
214  h /= 60.0;
215 
216  int i = int(h);
217 
218  double f = h - i;
219  double p = v * (1 - s);
220  double q = v * (1 - (s * f));
221  double t = v * (1 - (s * (1 - f)));
222 
223  switch(i) {
224  case 0:
225  rgb.r(v);
226  rgb.g(t);
227  rgb.b(p);
228  break;
229  case 1:
230  rgb.r(q);
231  rgb.g(v);
232  rgb.b(p);
233  break;
234  case 2:
235  rgb.r(p);
236  rgb.g(v);
237  rgb.b(t);
238  break;
239  case 3:
240  rgb.r(p);
241  rgb.g(q);
242  rgb.b(v);
243  break;
244  case 4:
245  rgb.r(t);
246  rgb.g(p);
247  rgb.b(v);
248  break;
249  case 5:
250  rgb.r(v);
251  rgb.g(p);
252  rgb.b(q);
253  break;
254  }
255 
256  return rgb;
257 }
258 
260 template <class T> Color<T> convertHSVtoRGB(const Color<T>& hsv)
261 {
262  // based on Jo's code in medit
263  Color<T> rgb;
264  rgb.a() = hsv.a();
265 
266  T h = hsv[0];
267  T s = hsv[1];
268  T v = hsv[2];
269 
270  while(h > 360.0)
271  h -= 360.0;
272  while(h < 0.0)
273  h += 360.0;
274 
275  h /= 60.0;
276 
277  int i = (int)floor(h);
278 
279  double f = h - i;
280  double p = v * (1 - s);
281  double q = v * (1 - (s * f));
282  double t = v * (1 - (s * (1 - f)));
283 
284  switch(i) {
285  case 0:
286  rgb.r(v);
287  rgb.g(t);
288  rgb.b(p);
289  break;
290  case 1:
291  rgb.r(q);
292  rgb.g(v);
293  rgb.b(p);
294  break;
295  case 2:
296  rgb.r(p);
297  rgb.g(v);
298  rgb.b(t);
299  break;
300  case 3:
301  rgb.r(p);
302  rgb.g(q);
303  rgb.b(v);
304  break;
305  case 4:
306  rgb.r(t);
307  rgb.g(p);
308  rgb.b(v);
309  break;
310  case 5:
311  rgb.r(v);
312  rgb.g(p);
313  rgb.b(q);
314  break;
315  }
316 
317  return rgb;
318 }
319 
321 template <class T> Color<T> convertRGBtoHSV(const Color<T>& rgb)
322 {
323  // based on Wikipedia's page
324 
325  T r = rgb.r();
326  T g = rgb.g();
327  T b = rgb.b();
328 
329  T M = std::max(std::max(r, g), b);
330  T m = std::min(std::min(r, g), b);
331  T c = M - m;
332 
333  T h;
334  if(c == 0)
335  h = 0;
336  else if(M == r)
337  h = fmod((g - b) / c, 6);
338  else if(M == g)
339  h = (b - r) / c + 2;
340  else
341  h = (r - g) / c + 4;
342  h *= 60;
343  T v = M;
344  T s;
345  if(c == 0)
346  s = 0;
347  else
348  s = c / v;
349 
350  return Color<T>(h, s, v, rgb.a());
351 }
352 } // namespace util
353 } // namespace mgx
354 #endif
Defines the util::clamp function.
A utility class to encapsulate color data.
Definition: Color.hpp:22
Color(const Vector< 4, T1 > &color, const T &scale=1)
Constructor to convert from one color type to another.
Definition: Color.hpp:28
T & b()
Return the blue component.
Definition: Color.hpp:77
const T & r() const
Return the red component.
Definition: Color.hpp:91
const T & a() const
Return the alpha component.
Definition: Color.hpp:112
T & r()
Return the red component.
Definition: Color.hpp:63
void g(const T &val)
Set the green component.
Definition: Color.hpp:126
CU_HOST_DEVICE T & y()
Short access to the second element.
Definition: Vector.hpp:696
CU_HOST_DEVICE T & t()
Short access to the fourth element.
Definition: Vector.hpp:714
void r(const T &val)
Set the red component.
Definition: Color.hpp:119
T & a()
Return the alpha component.
Definition: Color.hpp:84
CU_HOST_DEVICE Vector(const Vector &vec)
Copy another vector.
Definition: Vector.hpp:54
T & g()
Return the green component.
Definition: Color.hpp:70
CU_HOST_DEVICE T & x()
Short access to the first element.
Definition: Vector.hpp:687
const T & g() const
Return the green component.
Definition: Color.hpp:98
Namespace containing all the utility classes.
Definition: Vector.hpp:37
const T & b() const
Return the blue component.
Definition: Color.hpp:105
CU_HOST_DEVICE T & z()
Short access to the third element.
Definition: Vector.hpp:705
Color(const T &r=T(), const T &g=T(), const T &b=T(), const T &a=T())
Constructor.
Definition: Color.hpp:55
Color< T > & operator=(const Color< T > &c)
Assignment of color data.
Definition: Color.hpp:183
void a(const T &val)
Set the alpha component.
Definition: Color.hpp:140
void b(const T &val)
Set the blue component.
Definition: Color.hpp:133
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can't...
CU_HOST_DEVICE Vector & operator=(const Vector &vec)
Vector copy.
Definition: Vector.hpp:387