MorphoGraphX  2.0-1-227
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-2016 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 #include <vector>
26 #include <limits>
27 
28 namespace mgx
29 {
34  template <class T> class Color : public Vector<4, T>
35  {
36  public:
40  template <typename T1>
41  explicit Color(const Color<T1> &color)
42  : Vector<4,T>(color[0],color[1],color[2],color[3]) {}
43 
44  template <typename T1>
45  Color(const Vector<4, T1>& color, const T& scale)
46  : Vector<4, T>(color[0] * scale, color[1] * scale, color[2] * scale, color[3] * scale) {}
47 
48  Color(const Vector<4, T>& copy) : Vector<4, T>(copy) {}
49 
50  Color(const QColor& c) { convertFromQColor(*this, c); }
51 
59  Color(const T& r = T(), const T& g = T(), const T& b = T(), const T& a = T())
60  : Vector<4, T>(r, g, b, a) {}
61 
65  T& r() { return this->x(); }
66 
70  T& g() { return this->y(); }
71 
75  T& b() { return this->z(); }
76 
80  T& a() { return this->t(); }
81 
85  const T& r() const { return this->x(); }
86 
90  const T& g() const { return this->y(); }
91 
95  const T& b() const { return this->z(); }
96 
100  const T& a() const { return this->t(); }
101 
105  void r(const T& val) { this->x(val); }
106 
110  void g(const T& val) { this->y(val); }
111 
115  void b(const T& val) { this->z(val); }
116 
120  void a(const T& val) { this->t(val); }
121 
122  Color<T>& operator=(const Color<T>& c);
123  Color<T>& operator=(const Vector<4, T>& c);
124  Color<T>& operator=(const T& val);
125  Color<T>& operator=(const QColor& c)
126  {
127  convertFromQColor(*this, c);
128  return (*this);
129  }
130 
131  operator QColor() const { return convertToQColor(*this); }
132  };
133 
134  mgx_EXPORT QColor convertToQColor(const Color<float>& c);
135  mgx_EXPORT QColor convertToQColor(const Color<double>& c);
136  mgx_EXPORT QColor convertToQColor(const Color<long double>& c);
137  mgx_EXPORT QColor convertToQColor(const Color<unsigned char>& c);
138  mgx_EXPORT QColor convertToQColor(const Color<unsigned short>& c);
139 
140  mgx_EXPORT void convertFromQColor(Color<float>& c, const QColor& col);
141  mgx_EXPORT void convertFromQColor(Color<double>& c, const QColor& col);
142  mgx_EXPORT void convertFromQColor(Color<long double>& c, const QColor& col);
143  mgx_EXPORT void convertFromQColor(Color<unsigned char>& c, const QColor& col);
144  mgx_EXPORT void convertFromQColor(Color<unsigned short>& c, const QColor& col);
145 
147  template <class T> Color<T>& Color<T>::operator=(const Color<T>& c)
148  {
149  this->Vector<4, T>::operator=(c);
150  return *this;
151  }
152 
153  template <class T> Color<T>& Color<T>::operator=(const T& val)
154  {
155  this->Vector<4, T>::operator=(val);
156  return *this;
157  }
158 
159  template <class T> Color<T>& Color<T>::operator=(const Vector<4, T>& c)
160  {
161  this->Vector<4, T>::operator=(c);
162  return *this;
163  }
164 
166  template <class T> Color<T> convertHSVtoRGB(T h, T s, T v)
167  {
168  // based on Jo's code in medit
169 
170  Color<T> rgb;
171  rgb.a(1.0);
172 
173  while(h > 360.0)
174  h -= 360.0;
175  while(h < 0.0)
176  h += 360.0;
177 
178  h /= 60.0;
179 
180  int i = int(h);
181 
182  double f = h - i;
183  double p = v * (1 - s);
184  double q = v * (1 - (s * f));
185  double t = v * (1 - (s * (1 - f)));
186 
187  switch(i) {
188  case 0:
189  rgb.r(v);
190  rgb.g(t);
191  rgb.b(p);
192  break;
193  case 1:
194  rgb.r(q);
195  rgb.g(v);
196  rgb.b(p);
197  break;
198  case 2:
199  rgb.r(p);
200  rgb.g(v);
201  rgb.b(t);
202  break;
203  case 3:
204  rgb.r(p);
205  rgb.g(q);
206  rgb.b(v);
207  break;
208  case 4:
209  rgb.r(t);
210  rgb.g(p);
211  rgb.b(v);
212  break;
213  case 5:
214  rgb.r(v);
215  rgb.g(p);
216  rgb.b(q);
217  break;
218  }
219 
220  return rgb;
221  }
222 
224  template <class T> Color<T> convertHSVtoRGB(const Color<T>& hsv)
225  {
226  // based on Jo's code in medit
227  Color<T> rgb;
228  rgb.a() = hsv.a();
229 
230  T h = hsv[0];
231  T s = hsv[1];
232  T v = hsv[2];
233 
234  while(h > 360.0)
235  h -= 360.0;
236  while(h < 0.0)
237  h += 360.0;
238 
239  h /= 60.0;
240 
241  int i = (int)floor(h);
242 
243  double f = h - i;
244  double p = v * (1 - s);
245  double q = v * (1 - (s * f));
246  double t = v * (1 - (s * (1 - f)));
247 
248  switch(i) {
249  case 0:
250  rgb.r(v);
251  rgb.g(t);
252  rgb.b(p);
253  break;
254  case 1:
255  rgb.r(q);
256  rgb.g(v);
257  rgb.b(p);
258  break;
259  case 2:
260  rgb.r(p);
261  rgb.g(v);
262  rgb.b(t);
263  break;
264  case 3:
265  rgb.r(p);
266  rgb.g(q);
267  rgb.b(v);
268  break;
269  case 4:
270  rgb.r(t);
271  rgb.g(p);
272  rgb.b(v);
273  break;
274  case 5:
275  rgb.r(v);
276  rgb.g(p);
277  rgb.b(q);
278  break;
279  }
280 
281  return rgb;
282  }
283 
285  template <class T> Color<T> convertRGBtoHSV(const Color<T>& rgb)
286  {
287  // based on Wikipedia's page
288 
289  T r = rgb.r();
290  T g = rgb.g();
291  T b = rgb.b();
292 
293  T M = std::max(std::max(r, g), b);
294  T m = std::min(std::min(r, g), b);
295  T c = M - m;
296 
297  T h;
298  if(c == 0)
299  h = 0;
300  else if(M == r)
301  h = fmod((g - b) / c, 6);
302  else if(M == g)
303  h = (b - r) / c + 2;
304  else
305  h = (r - g) / c + 4;
306  h *= 60;
307  T v = M;
308  T s;
309  if(c == 0)
310  s = 0;
311  else
312  s = c / v;
313 
314  return Color<T>(h, s, v, rgb.a());
315  }
316 
319  typedef std::vector<Colorb> ColorbVec;
320 
321  template <> template <>
322  inline Colorb::Color(const Colorf &color)
323  : Vector<4,uchar>(color[0] * 255,color[1] * 255,color[2] * 255,color[3] * 255) {}
324 
325  template <> template <>
326  inline Colorf::Color(const Colorb &color)
327  : Vector<4,float>(color[0] / 255.,color[1] / 255.,color[2] / 255.,color[3] / 255.) {}
328 
329  namespace comp
330  {
333  {
338 
343 // Windows requires this
344 #undef DIFFERENCE
348  };
349  }
350 
352  Colorf composite(const Colorf &dest, const Colorf &source,
354 
356  inline Colorb composite(const Colorb &dest, const Colorb &source,
358  {
359  return Colorb(composite(Colorf(dest),Colorf(source),mode));
360  }
361 
363  QStringList compModes();
364 
366  comp::CompositeMode stringToCompMode(const QString &s);
367 
369  Colorb stringToColorb(const QString &s);
370 }
371 #endif
mgx::comp::LastComposite
@ LastComposite
Definition: Color.hpp:347
mgx::Color::Color
Color(const QColor &c)
Definition: Color.hpp:50
mgx::comp::LINEAR_BURN
@ LINEAR_BURN
Definition: Color.hpp:341
mgx::convertToQColor
mgx_EXPORT QColor convertToQColor(const Color< float > &c)
mgx::Vector::operator=
CU_HOST_DEVICE Vector & operator=(const T &value)
Set all the elements to value.
Definition: Vector.hpp:699
Vector.hpp
mgx::comp::CompositeMode
CompositeMode
Color composition modes.
Definition: Color.hpp:332
mgx::Vector< 4, T >::z
CU_HOST_DEVICE T & z()
Short access to the third element.
Definition: Vector.hpp:775
mgx::comp::LastSimpleComposite
@ LastSimpleComposite
Definition: Color.hpp:337
mgx::convertRGBtoHSV
Color< T > convertRGBtoHSV(const Color< T > &rgb)
Return a color based on HSV values.
Definition: Color.hpp:285
mgx::Colorf
Color< float > Colorf
Definition: Color.hpp:317
mgx::convertHSVtoRGB
Color< T > convertHSVtoRGB(T h, T s, T v)
Return a color based on HSV values.
Definition: Color.hpp:166
mgx::comp::COLOR_DODGE
@ COLOR_DODGE
Definition: Color.hpp:341
mgx::Color::r
void r(const T &val)
Set the red component.
Definition: Color.hpp:105
mgx::comp::MULTIPLY
@ MULTIPLY
Definition: Color.hpp:339
mgx::Color::g
T & g()
Return the green component.
Definition: Color.hpp:70
mgx::comp::SOURCE
@ SOURCE
Definition: Color.hpp:334
mgx::convertFromQColor
mgx_EXPORT void convertFromQColor(Color< float > &c, const QColor &col)
mgx::stringToColorb
Colorb stringToColorb(const QString &s)
Map Gui input string to Colorb.
mgx::Color::b
const T & b() const
Return the blue component.
Definition: Color.hpp:95
mgx::comp::SOFT_LIGHT
@ SOFT_LIGHT
Definition: Color.hpp:342
mgx::comp::DARKEN
@ DARKEN
Definition: Color.hpp:340
mgx::Color::operator=
Color< T > & operator=(const Color< T > &c)
Assignment of color data.
Definition: Color.hpp:147
mgx::Vector< 4, T >::x
CU_HOST_DEVICE T & x()
Short access to the first element.
Definition: Vector.hpp:757
mgx::comp::SATURATE
@ SATURATE
Definition: Color.hpp:336
mgx::comp::LastSeparableComposite
@ LastSeparableComposite
Definition: Color.hpp:346
mgx::Color::Color
Color(const T &r=T(), const T &g=T(), const T &b=T(), const T &a=T())
Constructor.
Definition: Color.hpp:59
mgx::Color::g
void g(const T &val)
Set the green component.
Definition: Color.hpp:110
mgx::comp::EXCLUSION
@ EXCLUSION
Definition: Color.hpp:345
mgx::comp::LINEAR_DODGE
@ LINEAR_DODGE
Definition: Color.hpp:341
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Color::g
const T & g() const
Return the green component.
Definition: Color.hpp:90
mgx::comp::ADD
@ ADD
Definition: Color.hpp:336
Clamp.hpp
mgx::Vector< 4, T >::t
CU_HOST_DEVICE T & t()
Short access to the fourth element.
Definition: Vector.hpp:784
mgx::Color::b
T & b()
Return the blue component.
Definition: Color.hpp:75
mgx::max
T CU_HOST_DEVICE max(const T a, const T b)
Definition: Util.hpp:34
mgx::ColorbVec
std::vector< Colorb > ColorbVec
Definition: Color.hpp:319
mgx::Color::Color
Color(const Color< T1 > &color)
Constructor to convert from one color type to another.
Definition: Color.hpp:41
mgx::comp::DEST_OVER
@ DEST_OVER
Definition: Color.hpp:335
mgx::Colorb
Color< uchar > Colorb
Definition: Color.hpp:318
mgx::Color::r
T & r()
Return the red component.
Definition: Color.hpp:65
mgx::comp::OVERLAY
@ OVERLAY
Definition: Color.hpp:339
mgx::Color::Color
Color(const Vector< 4, T > &copy)
Definition: Color.hpp:48
mgx::comp::DEST
@ DEST
Definition: Color.hpp:334
mgx::uchar
unsigned char uchar
Definition: Geometry.hpp:40
mgx::Color::a
void a(const T &val)
Set the alpha component.
Definition: Color.hpp:120
mgx::Vector
Namespace containing all the utility classes.
Definition: Vector.hpp:48
mgx::stringToCompMode
comp::CompositeMode stringToCompMode(const QString &s)
Map Gui input string to Composition mode.
mgx::comp::COLOR_BURN
@ COLOR_BURN
Definition: Color.hpp:341
mgx::composite
Colorb composite(const Colorb &dest, const Colorb &source, comp::CompositeMode mode=comp::OVER)
Compose a source with dest.
Definition: Color.hpp:356
mgx::Color::r
const T & r() const
Return the red component.
Definition: Color.hpp:85
mgx::Color::operator=
Color< T > & operator=(const QColor &c)
Definition: Color.hpp:125
mgx::Color::b
void b(const T &val)
Set the blue component.
Definition: Color.hpp:115
mgx::min
CU_HOST_DEVICE T min(const T a, const T b)
Definition: Util.hpp:26
mgx::Color::a
const T & a() const
Return the alpha component.
Definition: Color.hpp:100
mgx::compModes
QStringList compModes()
Get the list of composition modes.
mgx::Color
A utility class to encapsulate color data.
Definition: Color.hpp:34
mgx::comp::XOR
@ XOR
Definition: Color.hpp:336
mgx::Vector< 4, T >::y
CU_HOST_DEVICE T & y()
Short access to the second element.
Definition: Vector.hpp:766
mgx::Color::a
T & a()
Return the alpha component.
Definition: Color.hpp:80
mgx::comp::DIFFERENCE
@ DIFFERENCE
Definition: Color.hpp:345
mgx::Color::Color
Color(const Vector< 4, T1 > &color, const T &scale)
Definition: Color.hpp:45
mgx::comp::LIGHTEN
@ LIGHTEN
Definition: Color.hpp:340
mgx::comp::HARD_LIGHT
@ HARD_LIGHT
Definition: Color.hpp:342
mgx::comp::SCREEN
@ SCREEN
Definition: Color.hpp:339
mgx::comp::OVER
@ OVER
Definition: Color.hpp:335