MIRA
Color.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_COLOR_H_
49 #define _MIRA_COLOR_H_
50 
51 #include <math.h>
52 #include <opencv2/core/core.hpp>
53 
54 #include <platform/Platform.h>
55 #include <factory/Factory.h>
58 
59 namespace mira {
60 
62 
63 // forward decl for RGB
64 namespace Color {
66 }
67 
69 
77 public:
78  virtual ~ColorBase() {
79  }
80 
81 public:
83  virtual Color::RGB toRGB() const = 0;
84 
85 public:
90  virtual operator cv::Scalar() const;
91 };
92 
94 
95 namespace Color {
96 
99 
106 public:
107 
112  RGB(float ir, float ig, float ib) :
113  r(ir), g(ig), b(ib) {
114  }
115 
117  RGB() :
118  r(0), g(0), b(0) {
119  }
120 
122  RGB(const RGB& other) {
123  r = other.r;
124  g = other.g;
125  b = other.b;
126  }
127 
128  template<typename Reflector>
129  void reflect(Reflector& reflector) {
130  reflector.property("R", r, "The red component",
131  PropertyHints::limits(0., 1.));
132  reflector.property("G", g, "The green component",
133  PropertyHints::limits(0., 1.));
134  reflector.property("B", b, "The blue component",
135  PropertyHints::limits(0., 1.));
136  }
137 
139  virtual Color::RGB toRGB() const {
140  return *this;
141  }
142 
146  bool isInRange() const {
147  return r >= 0.0f && r <= 1.0f && g >= 0.0f && g <= 1.0f && b >= 0.0f
148  && b <= 1.0f;
149  }
150 
151 public:
152  float r, g, b;
153 };
154 
159 class MIRA_BASE_EXPORT RGBA: public RGB {
161 public:
162 
167  RGBA(float ir, float ig, float ib, float ia) :
168  RGB(ir, ig, ib), a(ia) {
169  }
170 
172  RGBA() :
173  a(1.0f) {
174  }
175 
177  RGBA(const RGBA& other) :
178  RGB(other.r, other.g, other.b), a(other.a) {
179  }
180 
184  RGBA(const RGB& other, float ia = 1.0f) :
185  RGB(other.r, other.g, other.b), a(ia) {
186  }
187 
188  template<typename Reflector>
189  void reflect(Reflector& r) {
191  r.property("A", a, "The alpha component",
192  PropertyHints::limits(0., 1.));
193  }
194 
195  virtual operator cv::Scalar() const {
196  return cv::Scalar(b * 255.0f, g * 255.0f, r * 255.0f, a * 255.0f);
197  }
198 
199 public:
200  float a;
201 };
202 
204 
211 public:
212 
217  HSV(float hue, float saturation, float value) :
218  h(hue), s(saturation), v(value) {
219  }
224  HSV() :
225  h(0.0f), s(0.0f), v(0.0f) {
226  }
227 
228  template<typename Reflector>
229  void reflect(Reflector& r) {
230  r.property("H", h, "The hue component", PropertyHints::limits(0., 1.));
231  r.property("S", s, "The saturation component",
232  PropertyHints::limits(0., 1.));
233  r.property("V", v, "The value component",
234  PropertyHints::limits(0., 1.));
235  }
236 
242  virtual Color::RGB toRGB() const {
243  float f = fmod(h * 6.0f, 6.0f);
244  int hi = ((int) f);
245  f = f - hi; // get the fraction
246 
247  float p = v * (1 - s);
248  float q = v * (1 - s * f);
249  float t = v * (1 - s * (1 - f));
250 
251  switch (hi) {
252  case 0:
253  return RGB(v, t, p);
254  case 1:
255  return RGB(q, v, p);
256  case 2:
257  return RGB(p, v, t);
258  case 3:
259  return RGB(p, q, v);
260  case 4:
261  return RGB(t, p, v);
262  case 5:
263  return RGB(v, p, q);
264  }
265 
266  return RGB(0, 0, 0);
267  }
268 
269 public:
270  float h, s, v;
271 };
272 
274 
288 public:
289 
296  XYZ(float ix, float iy, float iz) :
297  x(ix), y(iy), z(iz) {
298  }
303  XYZ() :
304  x(0.0f), y(0.0f), z(0.0f) {
305  }
306 
307  template<typename Reflector>
308  void reflect(Reflector& r) {
309  r.property("X", x, "The x component",
310  PropertyHints::limits(0., 98.047));
311  r.property("Y", y, "The y component",
312  PropertyHints::limits(0., 100.000));
313  r.property("Z", z, "The z component",
314  PropertyHints::limits(0., 108.883));
315  }
316 
326  virtual Color::RGB toRGB() const {
327  // formulas for conversation were taken from
328  // http://www.easyrgb.com/index.php
329  // Observer = 2 deg, Illuminant = D65
330  float X = x / 100.0f;
331  float Y = y / 100.0f;
332  float Z = z / 100.0f;
333 
334  float R = X * 3.2406f + Y * -1.5372f + Z * -0.4986f;
335  float G = X * -0.9689f + Y * 1.8758f + Z * 0.0415f;
336  float B = X * 0.0557f + Y * -0.2040f + Z * 1.0570f;
337 
338  if (R > 0.0031308f)
339  R = 1.055f * pow(R, 1.0f / 2.4f) - 0.055f;
340  else
341  R = 12.92f * R;
342 
343  if (G > 0.0031308f)
344  G = 1.055f * pow(G, 1.0f / 2.4f) - 0.055f;
345  else
346  G = 12.92f * G;
347 
348  if (B > 0.0031308f)
349  B = 1.055f * pow(B, 1.0f / 2.4f) - 0.055f;
350  else
351  B = 12.92f * B;
352 
353  return RGB(R, G, B);
354  }
355 
356 public:
357  float x, y, z;
358 };
359 
361 
383 public:
390  Lab(float iL, float ia, float ib) :
391  L(iL), a(ia), b(ib) {
392  }
397  Lab() :
398  L(0.0f), a(0.0f), b(0.0f) {
399  }
400 
401  template<typename Reflector>
402  void reflect(Reflector& r) {
403  r.property("L", L, "The L component",
404  PropertyHints::limits(0., 100.));
405  r.property("A", a, "The a component",
406  PropertyHints::limits(-150., 100.));
407  r.property("B", b, "The b component",
408  PropertyHints::limits(-100., 150.));
409  }
410 
416  XYZ toXYZ() const {
417 
418  // formulas for conversation were taken from
419  // http://www.easyrgb.com/index.php
420  float Y = (L + 16.0f) / 116.0f;
421  float X = a / 500.0f + Y;
422  float Z = Y - b / 200.0f;
423 
424  float Y3 = Y * Y * Y;
425  float X3 = X * X * X;
426  float Z3 = Z * Z * Z;
427 
428  if (Y3 > 0.008856f)
429  Y = Y3;
430  else
431  Y = (Y - 16.0f / 116.0f) / 7.787f;
432  if (X3 > 0.008856f)
433  X = X3;
434  else
435  X = (X - 16.0f / 116.0f) / 7.787f;
436  if (Z3 > 0.008856f)
437  Z = Z3;
438  else
439  Z = (Z - 16.0f / 116.0f) / 7.787f;
440 
441  // Reference color space:
442  // Observer= 2 deg, Illuminant= D65
443 
444  float refX = 95.047f;
445  float refY = 100.000f;
446  float refZ = 108.883f;
447 
448  X *= refX;
449  Y *= refY;
450  Z *= refZ;
451 
452  return XYZ(X, Y, Z);
453  }
454 
464  virtual Color::RGB toRGB() const {
465  return toXYZ().toRGB();
466  }
467 
468 public:
469  float L, a, b;
470 };
471 
473 
474 //
475 // The following code (YUV color class) is disabled, since the conversion
476 // to/from is not clear at the moment. There are different formulas avialable:
477 // http://en.wikipedia.org/wiki/YUV and http://www.fourcc.org/fccyvrgb.php
478 //
479 // We decided to remove the YUV color class until somebody has some time of
480 // investigate this into more detail.
481 //
483 // * Color in YUV color space
484 // *
485 // * conversion formulas to/from RGB taken from http://en.wikipedia.org/wiki/YUV
486 // *
487 // * @ingroup ImageModule
488 // */
489 //class MIRA_BASE_EXPORT YUV: public ColorBase {
490 // MIRA_OBJECT(YUV)
491 //public:
492 //
493 // /**
494 // * Constructs color in YUV color space.
495 // * Y: 0.0 to 1.0
496 // * U: -0.436 to 0.436
497 // * V: -0.615 to 0.615
498 // */
499 // YUV(float iy, float iu, float iv) :
500 // y(iy), u(iu), v(iv) {
501 // }
502 //
503 // /**
504 // * Default constructor
505 // * initializes Y,U, and V to 0.0
506 // * equivalent to RGB = (0,0,0)
507 // */
508 // YUV() :
509 // y(0.0f), u(0.0f), v(0.0f) {
510 // }
511 //
512 // YUV(const RGB& rgb) :
513 // y( 0.299f * rgb.r +0.587f * rgb.g +0.114f * rgb.b),
514 // u(-0.14713f * rgb.r -0.28886f * rgb.g +0.436f * rgb.b),
515 // v( 0.615f * rgb.r -0.51499f * rgb.g -0.10001f * rgb.b) {
516 // }
517 //
518 // template<typename Reflector>
519 // void reflect(Reflector& r) {
520 // r.property("Y", y, "The luminance component", PropertyHints::limits(0., 1.));
521 // r.property("U", u, "The chrominance component 1 (blue-luminance difference)",
522 // PropertyHints::limits(-0.436f, 0.436f));
523 // r.property("V", v, "The chrominance component 2 (red-luminance difference)",
524 // PropertyHints::limits(-0.615f, 0.615f));
525 // }
526 //
527 // /**
528 // * Converts the YUV color to RGB.
529 // */
530 // virtual Color::RGB toRGB() const {
531 //
532 // float r = 1.0f * y + + 1.13983f * v;
533 // float g = 1.0f * y - 0.39465f * u - 0.58060f * v;
534 // float b = 1.0f * y + 2.03211f * u;
535 //
536 // return RGB(r, g, b);
537 // }
538 //
539 //public:
540 // float y, u, v;
541 //};
542 
544 
545 // some important color predefs
546 const RGB Red(1.0f, 0.0f, 0.0f);
547 const RGB Green(0.0f, 1.0f, 0.0f);
548 const RGB Blue(0.0f, 0.0f, 1.0f);
549 const RGB Cyan(0.0f, 1.0f, 1.0f);
550 const RGB Magenta(1.0f, 0.0f, 1.0f);
551 const RGB Yellow(1.0f, 1.0f, 0.0f);
552 const RGB White(1.0f, 1.0f, 1.0f);
553 const RGB Black(0.0f, 0.0f, 0.0f);
554 
556 
557 }// namespace Color
558 
564 inline ColorBase::operator cv::Scalar() const {
565  Color::RGB c = toRGB();
566  return cv::Scalar(c.b * 255.0f, c.g * 255.0f, c.r * 255.0f, 255.0f);
567 }
568 
570 
571 }// namespace mira
572 
573 #endif
XYZ()
Default constructor Initializes X,Y, and Z with 0.0.
Definition: Color.h:303
virtual Color::RGB toRGB() const
Converts CIE Lab to RGB.
Definition: Color.h:464
const RGB Red(1.0f, 0.0f, 0.0f)
Color in YUV color space.
base interface for all colors in different color spaces The class provides a base interface for all c...
Definition: Color.h:75
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
XYZ(float ix, float iy, float iz)
Constructs color in CIE 1931 XYZ color space X: from 0 to 95.047 Y: from 0 to 100.000 Z: from 0 to 108.883.
Definition: Color.h:296
RGBA()
Default constructor that creates a black color.
Definition: Color.h:172
Color in CIE Lab color space The CIE Lab color space is derived from the XYZ color space and is desig...
Definition: Color.h:381
RGB(const RGB &other)
copy constructor
Definition: Color.h:122
RGBA(const RGBA &other)
copy constructor
Definition: Color.h:177
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:956
Provides property hints and attributes.
Lab(float iL, float ia, float ib)
Constructs color in CIE Lab color space.
Definition: Color.h:390
void reflect(Reflector &r)
Definition: Color.h:229
float r
Definition: Color.h:152
RGB(float ir, float ig, float ib)
Constructs RGB color R,G and B: range from 0.0 to 1.0.
Definition: Color.h:112
const RGB White(1.0f, 1.0f, 1.0f)
Lab()
Default constructor Initializes L,a, and b with 0.0.
Definition: Color.h:397
void reflect(Reflector &reflector)
Definition: Color.h:129
void reflect(Reflector &r)
Definition: Color.h:308
Color in RGBA color space.
Definition: Color.h:159
virtual Color::RGB toRGB() const
no conversion is done here, because it is already RGB
Definition: Color.h:139
HSV(float hue, float saturation, float value)
Constructs color in HSV color space.
Definition: Color.h:217
Contains the base interface of all Reflectors, Serializers, etc.
$Header file containing base classes to enable class creation using a class factory$ ...
float g
Definition: Color.h:152
RGBA(const RGB &other, float ia=1.0f)
Constructs RGBA color from RGB color and alpha value (that is 1.0f per default).
Definition: Color.h:184
PropertyHint limits(const T &min, const T &max)
Sets both attributes "minimum" and "maximum" to the specified values.
Definition: PropertyHint.h:275
void reflect(Reflector &r)
Definition: Color.h:189
const RGB Black(0.0f, 0.0f, 0.0f)
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
void reflect(Reflector &r)
Definition: Color.h:402
const RGB Yellow(1.0f, 1.0f, 0.0f)
const RGB Magenta(1.0f, 0.0f, 1.0f)
#define MIRA_OBJECT(classIdentifier)
Use this MACRO if you like the factory to automatically extract the class name from the given identif...
Definition: FactoryMacros.h:183
bool isInRange() const
Returns true, if all rgb components are within valid ranges, i.e.
Definition: Color.h:146
float a
Definition: Color.h:200
float z
Definition: Color.h:357
const RGB Green(0.0f, 1.0f, 0.0f)
const RGB Cyan(0.0f, 1.0f, 1.0f)
T pow(T base)
Computes the power of &#39;base&#39; with a constant integral exponent.
Definition: Power.h:104
RGB()
Default constructor that creates a black color.
Definition: Color.h:117
class MIRA_BASE_EXPORT RGB
Definition: Color.h:65
float b
Definition: Color.h:152
XYZ toXYZ() const
Converts CIE Lab to CIE XYZ.
Definition: Color.h:416
Color in CIE 1931 XYZ color space The CIE XYZ color space is the master for the derived CIE Lab color...
Definition: Color.h:286
The different color spaces.
Definition: Color.h:104
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
float v
Definition: Color.h:270
RGBA(float ir, float ig, float ib, float ia)
Constructs RGBA color R,G,B and A: range from 0.0 to 1.0.
Definition: Color.h:167
HSV()
Default constructor initializes H,S, and V to 0.0.
Definition: Color.h:224
float L
Definition: Color.h:469
virtual Color::RGB toRGB() const
Converts the HSV color to RGB.
Definition: Color.h:242
virtual Color::RGB toRGB() const
Converts the CIE XYZ color to RGB.
Definition: Color.h:326
Color in HSV color space.
Definition: Color.h:209
virtual ~ColorBase()
Definition: Color.h:78
const RGB Blue(0.0f, 0.0f, 1.0f)
Platform dependent defines and macros.