libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Color.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef COLOR_H_
32 #define COLOR_H_
33 
34 #include "Math.h"
35 
36 #include <cmath>
37 #include <string>
38 
39 namespace BaseLib
40 {
41 
42 class Color
43 {
44 public:
45  class NormalizedRGB;
46  class HSV;
47 
51  class RGB
52  {
53  public:
54  RGB() {}
55 
61  RGB(const std::string& rgbString);
62 
68  RGB(NormalizedRGB& rgb) { setRed(rgb.getRed()); setGreen(rgb.getGreen()); setBlue(rgb.getBlue()); }
69  RGB(uint8_t red, uint8_t green, uint8_t blue) { setRed(red); setGreen(green); setBlue(blue); }
70  virtual ~RGB() {}
71 
72  bool opacityDefined() { return _opacityDefined; };
73  uint8_t getOpacity() const { return _opacity; }
74 
80  void setOpacity(double value) { _opacityDefined = true; _opacity = value * 255; }
81  void setOpacity(uint8_t value) { _opacityDefined = true; _opacity = value; }
82 
83  uint8_t getRed() const { return _red; }
84 
90  void setRed(double value) { _red = value * 255; }
91  void setRed(uint8_t value) { _red = value; }
92  uint8_t getGreen() const { return _green; }
93 
99  void setGreen(double value) { _green = value * 255; }
100  void setGreen(uint8_t value) { _green = value; }
101  uint8_t getBlue() const { return _blue; }
102 
108  void setBlue(double value) { _blue = value * 255; }
109  void setBlue(uint8_t value) { _blue = value; }
110 
111  std::string toString();
112  protected:
113  bool _opacityDefined = false;
114  uint8_t _opacity = 255;
115  uint8_t _red = 0;
116  uint8_t _green = 0;
117  uint8_t _blue = 0;
118  };
119 
124  {
125  public:
127 
135  NormalizedRGB(uint8_t red, uint8_t green, uint8_t blue) { setRed(red); setGreen(green); setBlue(blue); }
136 
142  NormalizedRGB(RGB& rgb) { setRed(rgb.getRed()); setGreen(rgb.getGreen()); setBlue(rgb.getBlue()); }
143  NormalizedRGB(double red, double green, double blue) { setRed(red); setGreen(green); setBlue(blue); }
144  virtual ~NormalizedRGB() {}
145 
146  double getRed() const { return _red; }
147 
153  void setRed(uint8_t value) { _red = ((double)value) / 255.0; }
154  void setRed(double value) { _red = value; if(_red < 0) { _red = 0; } else if(_red > 1) { _red = 1; } }
155  double getGreen() const { return _green; }
156 
162  void setGreen(uint8_t value) { _green = ((double)value) / 255.0; }
163  void setGreen(double value) { _green = value; if(_green < 0) { _green = 0; } else if(_green > 1) { _green = 1; } }
164  double getBlue() const { return _blue; }
165 
171  void setBlue(uint8_t value) { _blue = ((double)value) / 255.0; }
172  void setBlue(double value) { _blue = value; if(_blue < 0) { _blue = 0; } else if(_blue > 1) { _blue = 1; } }
173 
177  HSV toHSV();
178  protected:
179  double _red = 0;
180  double _green = 0;
181  double _blue = 0;
182  };
183 
187  class HSV
188  {
189  public:
190  HSV() {}
191  HSV(double hue, double saturation, double brightness) { setHue(hue); setSaturation(saturation); setBrightness(brightness); }
192  virtual ~HSV() {}
193 
194  double getHue() const { return _hue; }
195  void setHue(double value) { _hue = value; if(_hue < 0) { _hue = 0; } else { _hue = std::fmod(_hue, 360.0); } }
196  double getSaturation() const { return _saturation; }
197  void setSaturation(double value) { _saturation = value; if(_saturation < 0) { _saturation = 0; } else if(_saturation > 1) { _saturation = 1; } }
198  double getBrightness() const { return _brightness; }
199  void setBrightness(double value) { _brightness = value; if(_brightness < 0) { _brightness = 0; } else if(_brightness > 1) { _brightness = 1; } }
200 
201  RGB toRGB();
202  protected:
203  double _hue = 0;
204  double _saturation = 0;
205  double _brightness = 0;
206  };
207 
212  virtual ~Color();
213 
223  static void getConversionMatrix(const Math::Triangle& gamut, Math::Matrix3x3& conversionMatrix, Math::Matrix3x3& inversedConversionMatrix);
224 
237  static void rgbToCie1931Xy(const NormalizedRGB& rgb, const Math::Matrix3x3& conversionMatrix, const double& gamma, Math::Point2D& xy, double& brightness);
238 
251  static void cie1931XyToRgb(const Math::Point2D& xy, const double& brightness, const Math::Matrix3x3& conversionMatrix, const double& gamma, NormalizedRGB& rgb);
252 protected:
257  Color();
258 };
259 
260 }
261 
262 #endif
void setSaturation(double value)
Definition: Color.h:197
double getHue() const
Definition: Color.h:194
Class defining a point in 2D space with numbers of type double.
Definition: Math.h:45
RGB(uint8_t red, uint8_t green, uint8_t blue)
Definition: Color.h:69
void setRed(double value)
Converts a normalized intensity between 0 and 1 to an intensity between 0 and 255 and sets the intens...
Definition: Color.h:90
void setOpacity(double value)
Converts a normalized opacity between 0 and 1 to an opacity between 0 and 255 and sets it...
Definition: Color.h:80
NormalizedRGB(uint8_t red, uint8_t green, uint8_t blue)
Constructor taking intensities between 0 and 255 which are being converted to the normalized intensit...
Definition: Color.h:135
double getBrightness() const
Definition: Color.h:198
void setGreen(double value)
Converts a normalized intensity between 0 and 1 to an intensity between 0 and 255 and sets the intens...
Definition: Color.h:99
void setGreen(uint8_t value)
Converts an intensity between 0 and 255 to the normalized intensity between 0 and 1 and sets the inte...
Definition: Color.h:162
RGB(NormalizedRGB &rgb)
Constructor taking a normalized RGB class with color intensities between 0 and 1 which are being conv...
Definition: Color.h:68
double getRed() const
Definition: Color.h:146
double getSaturation() const
Definition: Color.h:196
void setHue(double value)
Definition: Color.h:195
void setOpacity(uint8_t value)
Definition: Color.h:81
Definition: BaseLib.cpp:34
virtual ~HSV()
Definition: Color.h:192
virtual ~Color()
Destructor.
Definition: Color.cpp:205
void setRed(uint8_t value)
Definition: Color.h:91
Class defining a triangle.
Definition: Math.h:156
uint8_t _red
Definition: Color.h:115
HSV()
Definition: Color.h:190
Class defining a 3x3 matrix.
Definition: Math.h:92
NormalizedRGB(RGB &rgb)
Constructor taking a not normalized RGB class with color intensities between 0 and 255 which are bein...
Definition: Color.h:142
std::string toString()
Definition: Color.cpp:112
virtual ~RGB()
Definition: Color.h:70
PVariable value
Definition: UiElements.h:217
Class defining a HSV color.
Definition: Color.h:187
Color()
Constructor.
Definition: Color.cpp:201
void setGreen(uint8_t value)
Definition: Color.h:100
void setBlue(double value)
Converts a normalized intensity between 0 and 1 to an intensity between 0 and 255 and sets the intens...
Definition: Color.h:108
double getBlue() const
Definition: Color.h:164
virtual ~NormalizedRGB()
Definition: Color.h:144
bool _opacityDefined
Definition: Color.h:113
static void cie1931XyToRgb(const Math::Point2D &xy, const double &brightness, const Math::Matrix3x3 &conversionMatrix, const double &gamma, NormalizedRGB &rgb)
Converts CIE 1931 color space chromaticity coordinates (x, y) to RGB as reference white...
Definition: Color.cpp:277
NormalizedRGB()
Definition: Color.h:126
void setGreen(double value)
Definition: Color.h:163
uint8_t _blue
Definition: Color.h:117
Class defining a RGB color with intensity values for each color between 0 and 1.
Definition: Color.h:123
void setBrightness(double value)
Definition: Color.h:199
uint8_t getBlue() const
Definition: Color.h:101
void setRed(double value)
Definition: Color.h:154
RGB()
Definition: Color.h:54
void setRed(uint8_t value)
Converts an intensity between 0 and 255 to the normalized intensity between 0 and 1 and sets the inte...
Definition: Color.h:153
uint8_t getGreen() const
Definition: Color.h:92
uint8_t getRed() const
Definition: Color.h:83
void setBlue(uint8_t value)
Converts an intensity between 0 and 255 to the normalized intensity between 0 and 1 and sets the inte...
Definition: Color.h:171
Class defining a RGB color with intensity values for each color between 0 and 255.
Definition: Color.h:51
void setBlue(uint8_t value)
Definition: Color.h:109
static void rgbToCie1931Xy(const NormalizedRGB &rgb, const Math::Matrix3x3 &conversionMatrix, const double &gamma, Math::Point2D &xy, double &brightness)
Converts a RGB value to it&#39;s CIE 1931 color space chromaticity coordinates (x, y).
Definition: Color.cpp:246
Definition: Color.h:42
bool opacityDefined()
Definition: Color.h:72
NormalizedRGB(double red, double green, double blue)
Definition: Color.h:143
double getGreen() const
Definition: Color.h:155
uint8_t _green
Definition: Color.h:116
HSV(double hue, double saturation, double brightness)
Definition: Color.h:191
static void getConversionMatrix(const Math::Triangle &gamut, Math::Matrix3x3 &conversionMatrix, Math::Matrix3x3 &inversedConversionMatrix)
Gets the RGB to XYZ and XYZ to RGB conversion matrix for the specified chromaticity coordinates of an...
Definition: Color.cpp:209
uint8_t getOpacity() const
Definition: Color.h:73
uint8_t _opacity
Definition: Color.h:114
void setBlue(double value)
Definition: Color.h:172