MorphoGraphX
Color.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef COLOR_H
12 #define COLOR_H
13 
19 #include <Config.hpp>
20 
21 #include <Clamp.hpp>
22 #include <math.h>
23 #include <QColor>
24 #include <Vector.hpp>
25 
26 namespace mgx {
27 namespace util {
32 template <class T> class Color : public Vector<4, T> {
33 public:
37  template <typename T1>
38  Color(const Vector<4, T1>& color, const T& scale = 1)
39  : Vector<4, T>(color[0] * scale, color[1] * scale, color[2] * scale, color[3] * scale)
40  {
41  }
42 
43  template <typename T1>
44  Color(const Vector<4, T1>& color, const T1& scale)
45  : Vector<4, T>(color[0] * scale, color[1] * scale, color[2] * scale, color[3] * scale)
46  {
47  }
48 
49  Color(const Vector<4, T>& copy)
50  : Vector<4, T>(copy)
51  {
52  }
53 
54  Color(const QColor& c) {
55  convertFromQColor(*this, c);
56  }
57 
65  Color(const T& r = T(), const T& g = T(), const T& b = T(), const T& a = T())
66  : Vector<4, T>(r, g, b, a)
67  {
68  }
69 
73  T& r() {
74  return this->x();
75  }
76 
80  T& g() {
81  return this->y();
82  }
83 
87  T& b() {
88  return this->z();
89  }
90 
94  T& a() {
95  return this->t();
96  }
97 
101  const T& r() const {
102  return this->x();
103  }
104 
108  const T& g() const {
109  return this->y();
110  }
111 
115  const T& b() const {
116  return this->z();
117  }
118 
122  const T& a() const {
123  return this->t();
124  }
125 
129  void r(const T& val) {
130  this->x(val);
131  }
132 
136  void g(const T& val) {
137  this->y(val);
138  }
139 
143  void b(const T& val) {
144  this->z(val);
145  }
146 
150  void a(const T& val) {
151  this->t(val);
152  }
153 
154  Color<T>& operator=(const Color<T>& c);
155  Color<T>& operator=(const Vector<4, T>& c);
156  Color<T>& operator=(const T& val);
157  Color<T>& operator=(const QColor& c) {
158  convertFromQColor(*this, c);
159  }
160 
161  operator QColor() const { return convertToQColor(*this); }
162 };
163 
164 mgx_EXPORT QColor convertToQColor(const Color<float>& c);
165 mgx_EXPORT QColor convertToQColor(const Color<double>& c);
166 mgx_EXPORT QColor convertToQColor(const Color<long double>& c);
167 mgx_EXPORT QColor convertToQColor(const Color<unsigned char>& c);
168 mgx_EXPORT QColor convertToQColor(const Color<unsigned short>& c);
169 mgx_EXPORT QColor convertToQColor(const Color<unsigned int>& c);
170 mgx_EXPORT QColor convertToQColor(const Color<unsigned long>& c);
171 mgx_EXPORT QColor convertToQColor(const Color<unsigned long long>& c);
172 mgx_EXPORT QColor convertToQColor(const Color<char>& c);
173 mgx_EXPORT QColor convertToQColor(const Color<short>& c);
174 mgx_EXPORT QColor convertToQColor(const Color<int>& c);
175 mgx_EXPORT QColor convertToQColor(const Color<long>& c);
176 mgx_EXPORT QColor convertToQColor(const Color<long long>& c);
177 
178 mgx_EXPORT void convertFromQColor(Color<float>& c, const QColor& col);
179 mgx_EXPORT void convertFromQColor(Color<double>& c, const QColor& col);
180 mgx_EXPORT void convertFromQColor(Color<long double>& c, const QColor& col);
181 mgx_EXPORT void convertFromQColor(Color<unsigned char>& c, const QColor& col);
182 mgx_EXPORT void convertFromQColor(Color<unsigned short>& c, const QColor& col);
183 mgx_EXPORT void convertFromQColor(Color<unsigned int>& c, const QColor& col);
184 mgx_EXPORT void convertFromQColor(Color<unsigned long>& c, const QColor& col);
185 mgx_EXPORT void convertFromQColor(Color<unsigned long long>& c, const QColor& col);
186 mgx_EXPORT void convertFromQColor(Color<char>& c, const QColor& col);
187 mgx_EXPORT void convertFromQColor(Color<short>& c, const QColor& col);
188 mgx_EXPORT void convertFromQColor(Color<int>& c, const QColor& col);
189 mgx_EXPORT void convertFromQColor(Color<long>& c, const QColor& col);
190 mgx_EXPORT void convertFromQColor(Color<long long>& c, const QColor& col);
191 
193 template <class T> Color<T>& Color<T>::operator=(const Color<T>& c)
194 {
195  this->Vector<4, T>::operator=(c);
196  return *this;
197 }
198 
199 template <class T> Color<T>& Color<T>::operator=(const T& val)
200 {
201  this->Vector<4, T>::operator=(val);
202  return *this;
203 }
204 
205 template <class T> Color<T>& Color<T>::operator=(const Vector<4, T>& c)
206 {
207  this->Vector<4, T>::operator=(c);
208  return *this;
209 }
210 
212 template <class T> Color<T> convertHSVtoRGB(T h, T s, T v)
213 {
214  // based on Jo's code in medit
215 
216  Color<T> rgb;
217  rgb.a(1.0);
218 
219  while(h > 360.0)
220  h -= 360.0;
221  while(h < 0.0)
222  h += 360.0;
223 
224  h /= 60.0;
225 
226  int i = int(h);
227 
228  double f = h - i;
229  double p = v * (1 - s);
230  double q = v * (1 - (s * f));
231  double t = v * (1 - (s * (1 - f)));
232 
233  switch(i) {
234  case 0:
235  rgb.r(v);
236  rgb.g(t);
237  rgb.b(p);
238  break;
239  case 1:
240  rgb.r(q);
241  rgb.g(v);
242  rgb.b(p);
243  break;
244  case 2:
245  rgb.r(p);
246  rgb.g(v);
247  rgb.b(t);
248  break;
249  case 3:
250  rgb.r(p);
251  rgb.g(q);
252  rgb.b(v);
253  break;
254  case 4:
255  rgb.r(t);
256  rgb.g(p);
257  rgb.b(v);
258  break;
259  case 5:
260  rgb.r(v);
261  rgb.g(p);
262  rgb.b(q);
263  break;
264  }
265 
266  return rgb;
267 }
268 
270 template <class T> Color<T> convertHSVtoRGB(const Color<T>& hsv)
271 {
272  // based on Jo's code in medit
273  Color<T> rgb;
274  rgb.a() = hsv.a();
275 
276  T h = hsv[0];
277  T s = hsv[1];
278  T v = hsv[2];
279 
280  while(h > 360.0)
281  h -= 360.0;
282  while(h < 0.0)
283  h += 360.0;
284 
285  h /= 60.0;
286 
287  int i = (int)floor(h);
288 
289  double f = h - i;
290  double p = v * (1 - s);
291  double q = v * (1 - (s * f));
292  double t = v * (1 - (s * (1 - f)));
293 
294  switch(i) {
295  case 0:
296  rgb.r(v);
297  rgb.g(t);
298  rgb.b(p);
299  break;
300  case 1:
301  rgb.r(q);
302  rgb.g(v);
303  rgb.b(p);
304  break;
305  case 2:
306  rgb.r(p);
307  rgb.g(v);
308  rgb.b(t);
309  break;
310  case 3:
311  rgb.r(p);
312  rgb.g(q);
313  rgb.b(v);
314  break;
315  case 4:
316  rgb.r(t);
317  rgb.g(p);
318  rgb.b(v);
319  break;
320  case 5:
321  rgb.r(v);
322  rgb.g(p);
323  rgb.b(q);
324  break;
325  }
326 
327  return rgb;
328 }
329 
331 template <class T> Color<T> convertRGBtoHSV(const Color<T>& rgb)
332 {
333  // based on Wikipedia's page
334 
335  T r = rgb.r();
336  T g = rgb.g();
337  T b = rgb.b();
338 
339  T M = std::max(std::max(r, g), b);
340  T m = std::min(std::min(r, g), b);
341  T c = M - m;
342 
343  T h;
344  if(c == 0)
345  h = 0;
346  else if(M == r)
347  h = fmod((g - b) / c, 6);
348  else if(M == g)
349  h = (b - r) / c + 2;
350  else
351  h = (r - g) / c + 4;
352  h *= 60;
353  T v = M;
354  T s;
355  if(c == 0)
356  s = 0;
357  else
358  s = c / v;
359 
360  return Color<T>(h, s, v, rgb.a());
361 }
362 } // namespace util
363 } // namespace mgx
364 #endif
Defines the util::clamp function.
A utility class to encapsulate color data.
Definition: Color.hpp:32
Color(const Vector< 4, T1 > &color, const T &scale=1)
Constructor to convert from one color type to another.
Definition: Color.hpp:38
T & b()
Return the blue component.
Definition: Color.hpp:87
const T & r() const
Return the red component.
Definition: Color.hpp:101
const T & a() const
Return the alpha component.
Definition: Color.hpp:122
T & r()
Return the red component.
Definition: Color.hpp:73
void g(const T &val)
Set the green component.
Definition: Color.hpp:136
CU_HOST_DEVICE T & y()
Short access to the second element.
Definition: Vector.hpp:706
CU_HOST_DEVICE T & t()
Short access to the fourth element.
Definition: Vector.hpp:724
void r(const T &val)
Set the red component.
Definition: Color.hpp:129
T & a()
Return the alpha component.
Definition: Color.hpp:94
CU_HOST_DEVICE T & i()
Short access to the first element.
Definition: Vector.hpp:806
Color< T > convertHSVtoRGB(T h, T s, T v)
Return a color based on HSV values.
Definition: Color.hpp:212
T & g()
Return the green component.
Definition: Color.hpp:80
CU_HOST_DEVICE T & x()
Short access to the first element.
Definition: Vector.hpp:697
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
const T & g() const
Return the green component.
Definition: Color.hpp:108
Namespace containing all the utility classes.
Definition: Vector.hpp:47
const T & b() const
Return the blue component.
Definition: Color.hpp:115
Color< T > convertRGBtoHSV(const Color< T > &rgb)
Return a color based on HSV values.
Definition: Color.hpp:331
CU_HOST_DEVICE T & z()
Short access to the third element.
Definition: Vector.hpp:715
Color(const T &r=T(), const T &g=T(), const T &b=T(), const T &a=T())
Constructor.
Definition: Color.hpp:65
Color< T > & operator=(const Color< T > &c)
Assignment of color data.
Definition: Color.hpp:193
void a(const T &val)
Set the alpha component.
Definition: Color.hpp:150
void b(const T &val)
Set the blue component.
Definition: Color.hpp:143
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...
CU_HOST_DEVICE Vector & operator=(const Vector &vec)
Vector copy.
Definition: Vector.hpp:397