Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
RgbwColor.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 RgbwColor provides a color object that can be directly consumed by LumitronixIFlexBus
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 #pragma once
27 
28 struct RgbColor;
29 struct HslColor;
30 struct HsbColor;
31 
32 // ------------------------------------------------------------------------
33 // RgbwColor represents a color object that is represented by Red, Green, Blue
34 // component values and an extra White component. It contains helpful color
35 // routines to manipulate the color.
36 // ------------------------------------------------------------------------
38 {
40 
41  // ------------------------------------------------------------------------
42  // Construct a RgbwColor using R, G, B, W values (0-255)
43  // ------------------------------------------------------------------------
44  RgbwColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0) :
45  R(r), G(g), B(b), W(w)
46  {
47  };
48 
49  // ------------------------------------------------------------------------
50  // Construct a RgbColor using a single brightness value (0-255)
51  // This works well for creating gray tone colors
52  // (0) = black, (255) = white, (128) = gray
53  // ------------------------------------------------------------------------
54  RgbwColor(uint8_t brightness) :
55  R(0), G(0), B(0), W(brightness)
56  {
57  };
58 
59  // ------------------------------------------------------------------------
60  // Construct a RgbwColor using RgbColor
61  // ------------------------------------------------------------------------
62  RgbwColor(const RgbColor& color) :
63  R(color.R),
64  G(color.G),
65  B(color.B),
66  W(0)
67  {
68  };
69 
70  // ------------------------------------------------------------------------
71  // Construct a RgbwColor using HtmlColor
72  // ------------------------------------------------------------------------
73  RgbwColor(const HtmlColor& color);
74 
75  // ------------------------------------------------------------------------
76  // Construct a RgbwColor using HslColor
77  // ------------------------------------------------------------------------
78  RgbwColor(const HslColor& color);
79 
80  // ------------------------------------------------------------------------
81  // Construct a RgbwColor using HsbColor
82  // ------------------------------------------------------------------------
83  RgbwColor(const HsbColor& color);
84 
85  // ------------------------------------------------------------------------
86  // Construct a RgbwColor that will have its values set in latter operations
87  // CAUTION: The R,G,B, W members are not initialized and may not be consistent
88  // ------------------------------------------------------------------------
90  {
91  };
92 
93  // ------------------------------------------------------------------------
94  // Comparison operators
95  // ------------------------------------------------------------------------
96  bool operator==(const RgbwColor& other) const
97  {
98  return (R == other.R && G == other.G && B == other.B && W == other.W);
99  };
100 
101  bool operator!=(const RgbwColor& other) const
102  {
103  return !(*this == other);
104  };
105 
106  // ------------------------------------------------------------------------
107  // CompareTo method
108  // compares against another color with the given epsilon (delta allowed)
109  // returns the greatest difference of a set of elements,
110  // 0 = equal within epsilon delta
111  // negative - this is less than other
112  // positive - this is greater than other
113  // ------------------------------------------------------------------------
114  int16_t CompareTo(const RgbwColor& other, uint8_t epsilon = 1)
115  {
116  return _Compare<RgbwColor, int16_t>(*this, other, epsilon);
117  }
118 
119  // ------------------------------------------------------------------------
120  // Compare method
121  // compares two colors with the given epsilon (delta allowed)
122  // returns the greatest difference of a set of elements,
123  // 0 = equal within epsilon delta
124  // negative - left is less than right
125  // positive - left is greater than right
126  // ------------------------------------------------------------------------
127  static int16_t Compare(const RgbwColor& left, const RgbwColor& right, uint8_t epsilon = 1)
128  {
129  return _Compare<RgbwColor, int16_t>(left, right, epsilon);
130  }
131 
132  // ------------------------------------------------------------------------
133  // operator [] - readonly
134  // access elements in order by index rather than R,G,B
135  // see static Count for the number of elements
136  // ------------------------------------------------------------------------
137  uint8_t operator[](size_t idx) const
138  {
139  switch (idx)
140  {
141  case 0:
142  return R;
143  case 1:
144  return G;
145  case 2:
146  return B;
147  default:
148  return W;
149  }
150  }
151 
152  // ------------------------------------------------------------------------
153  // operator [] - read write
154  // access elements in order by index rather than R,G,B
155  // see static Count for the number of elements
156  // ------------------------------------------------------------------------
157  uint8_t& operator[](size_t idx)
158  {
159  switch (idx)
160  {
161  case 0:
162  return R;
163  case 1:
164  return G;
165  case 2:
166  return B;
167  default:
168  return W;
169  }
170  }
171 
172  // ------------------------------------------------------------------------
173  // Returns if the color is grey, all values are equal other than white
174  // ------------------------------------------------------------------------
175  bool IsMonotone() const
176  {
177  return (R == B && R == G);
178  };
179 
180  // ------------------------------------------------------------------------
181  // Returns if the color components are all zero, the white component maybe
182  // anything
183  // ------------------------------------------------------------------------
184  bool IsColorLess() const
185  {
186  return (R == 0 && B == 0 && G == 0);
187  };
188 
189  // ------------------------------------------------------------------------
190  // CalculateBrightness will calculate the overall brightness
191  // NOTE: This is a simple linear brightness
192  // ------------------------------------------------------------------------
193  uint8_t CalculateBrightness() const;
194 
195  // ------------------------------------------------------------------------
196  // Dim will return a new color that is blended to black with the given ratio
197  // ratio - (0-255) where 255 will return the original color and 0 will return black
198  //
199  // NOTE: This is a simple linear blend
200  // ------------------------------------------------------------------------
201  RgbwColor Dim(uint8_t ratio) const;
202 
203  // ------------------------------------------------------------------------
204  // Brighten will return a new color that is blended to white with the given ratio
205  // ratio - (0-255) where 255 will return the original color and 0 will return white
206  //
207  // NOTE: This is a simple linear blend
208  // ------------------------------------------------------------------------
209  RgbwColor Brighten(uint8_t ratio) const;
210 
211  // ------------------------------------------------------------------------
212  // Darken will adjust the color by the given delta toward black
213  // NOTE: This is a simple linear change
214  // delta - (0-255) the amount to dim the color
215  // ------------------------------------------------------------------------
216  void Darken(uint8_t delta);
217 
218  // ------------------------------------------------------------------------
219  // Lighten will adjust the color by the given delta toward white
220  // NOTE: This is a simple linear change
221  // delta - (0-255) the amount to lighten the color
222  // ------------------------------------------------------------------------
223  void Lighten(uint8_t delta);
224 
225  // ------------------------------------------------------------------------
226  // LinearBlend between two colors by the amount defined by progress variable
227  // left - the color to start the blend at
228  // right - the color to end the blend at
229  // progress - (0.0 - 1.0) value where 0 will return left and 1.0 will return right
230  // and a value between will blend the color weighted linearly between them
231  // ------------------------------------------------------------------------
232  static RgbwColor LinearBlend(const RgbwColor& left, const RgbwColor& right, float progress);
233  // progress - (0 - 255) value where 0 will return left and 255 will return right
234  // and a value between will blend the color weighted linearly between them
235  // ------------------------------------------------------------------------
236  static RgbwColor LinearBlend(const RgbwColor& left, const RgbwColor& right, uint8_t progress);
237 
238  // ------------------------------------------------------------------------
239  // BilinearBlend between four colors by the amount defined by 2d variable
240  // c00 - upper left quadrant color
241  // c01 - upper right quadrant color
242  // c10 - lower left quadrant color
243  // c11 - lower right quadrant color
244  // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space
245  // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space
246  // ------------------------------------------------------------------------
247  static RgbwColor BilinearBlend(const RgbwColor& c00,
248  const RgbwColor& c01,
249  const RgbwColor& c10,
250  const RgbwColor& c11,
251  float x,
252  float y);
253 
254  uint16_t CalcTotalTenthMilliAmpere(const SettingsObject& settings)
255  {
256  auto total = 0;
257 
258  total += R * settings.RedTenthMilliAmpere / Max;
259  total += G * settings.GreenTenthMilliAmpere / Max;
260  total += B * settings.BlueTenthMilliAmpere / Max;
261  total += W * settings.WhiteTenthMilliAmpere / Max;
262 
263  return total;
264  }
265 
266  // ------------------------------------------------------------------------
267  // Red, Green, Blue, White color members (0-255) where
268  // (0,0,0,0) is black and (255,255,255, 0) and (0,0,0,255) is white
269  // Note (255,255,255,255) is extreme bright white
270  // ------------------------------------------------------------------------
271  uint8_t R;
272  uint8_t G;
273  uint8_t B;
274  uint8_t W;
275 
276  const static uint8_t Max = 255;
277  const static size_t Count = 4; // four elements in []
278 
279 private:
280  inline static uint8_t _elementDim(uint8_t value, uint8_t ratio)
281  {
282  return (static_cast<uint16_t>(value) * (static_cast<uint16_t>(ratio) + 1)) >> 8;
283  }
284 
285  inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio)
286  {
287  uint16_t element = ((static_cast<uint16_t>(value) + 1) << 8) / (static_cast<uint16_t>(ratio) + 1);
288 
289  if (element > Max)
290  {
291  element = Max;
292  }
293  else
294  {
295  element -= 1;
296  }
297  return element;
298  }
299 };
300 
Definition: NeoSettings.h:65
const uint16_t RedTenthMilliAmpere
Definition: NeoSettings.h:94
const uint16_t GreenTenthMilliAmpere
Definition: NeoSettings.h:95
const uint16_t BlueTenthMilliAmpere
Definition: NeoSettings.h:96
const uint16_t WhiteTenthMilliAmpere
Definition: NeoSettings.h:97
Definition: HsbColor.h:35
Definition: HslColor.h:34
Definition: HtmlColor.h:70
Definition: RgbColorBase.h:34
Definition: RgbColor.h:36
Definition: RgbwColor.h:38
int16_t CompareTo(const RgbwColor &other, uint8_t epsilon=1)
Definition: RgbwColor.h:114
uint8_t R
Definition: RgbwColor.h:271
bool operator==(const RgbwColor &other) const
Definition: RgbwColor.h:96
RgbwColor()
Definition: RgbwColor.h:89
bool operator!=(const RgbwColor &other) const
Definition: RgbwColor.h:101
static RgbwColor LinearBlend(const RgbwColor &left, const RgbwColor &right, float progress)
Definition: RgbwColor.cpp:169
static const size_t Count
Definition: RgbwColor.h:277
RgbwColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0)
Definition: RgbwColor.h:44
uint16_t CalcTotalTenthMilliAmpere(const SettingsObject &settings)
Definition: RgbwColor.h:254
RgbwColor(const RgbColor &color)
Definition: RgbwColor.h:62
uint8_t W
Definition: RgbwColor.h:274
void Lighten(uint8_t delta)
Definition: RgbwColor.cpp:125
static int16_t Compare(const RgbwColor &left, const RgbwColor &right, uint8_t epsilon=1)
Definition: RgbwColor.h:127
RgbwColor Brighten(uint8_t ratio) const
Definition: RgbwColor.cpp:80
uint8_t B
Definition: RgbwColor.h:273
uint8_t CalculateBrightness() const
Definition: RgbwColor.cpp:61
NeoRgbwCurrentSettings SettingsObject
Definition: RgbwColor.h:39
uint8_t G
Definition: RgbwColor.h:272
bool IsColorLess() const
Definition: RgbwColor.h:184
static const uint8_t Max
Definition: RgbwColor.h:276
uint8_t operator[](size_t idx) const
Definition: RgbwColor.h:137
bool IsMonotone() const
Definition: RgbwColor.h:175
static RgbwColor BilinearBlend(const RgbwColor &c00, const RgbwColor &c01, const RgbwColor &c10, const RgbwColor &c11, float x, float y)
Definition: RgbwColor.cpp:184
RgbwColor Dim(uint8_t ratio) const
Definition: RgbwColor.cpp:74
RgbwColor(uint8_t brightness)
Definition: RgbwColor.h:54
void Darken(uint8_t delta)
Definition: RgbwColor.cpp:86
uint8_t & operator[](size_t idx)
Definition: RgbwColor.h:157