Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
RgbwwColor.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 RgbwwColor 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 // RgbwwColor represents a color object that is represented by Red, Green, Blue
34 // component values and two extra White components.
35 // While the white components are labeled as WW (warm) and CW (cool), they can be
36 // considered as the "warmer" and "cooler" whites of your LEDs; so that if yours
37 // have Nuetral and Cool, you could consider the WW as your nuetral.
38 // It contains helpful color routines to manipulate the color.
39 // ------------------------------------------------------------------------
41 {
43 
44  // ------------------------------------------------------------------------
45  // Construct a RgbwwColor using R, G, B, WW, CW values (0-255)
46  // ------------------------------------------------------------------------
47  RgbwwColor(uint8_t r, uint8_t g, uint8_t b, uint8_t warmW = 0, uint8_t coolW = 0) :
48  R(r), G(g), B(b), WW(warmW), CW(coolW)
49  {
50  };
51 
52  // ------------------------------------------------------------------------
53  // Construct a RgbwwColor using a single brightness value (0-255)
54  // This works well for creating gray tone colors
55  // (0) = black, (255) = white, (128) = gray
56  // ------------------------------------------------------------------------
57  RgbwwColor(uint8_t brightness) :
58  R(0), G(0), B(0), WW(brightness), CW(brightness)
59  {
60  };
61 
62  // ------------------------------------------------------------------------
63  // Construct a RgbwwColor using RgbColor
64  // ------------------------------------------------------------------------
65  RgbwwColor(const RgbColor& color) :
66  R(color.R),
67  G(color.G),
68  B(color.B),
69  WW(0),
70  CW(0)
71  {
72  };
73 
74  // ------------------------------------------------------------------------
75  // Construct a RgbwwColor using RgbwColor
76  // ------------------------------------------------------------------------
77  RgbwwColor(const RgbwColor& color) :
78  R(color.R),
79  G(color.G),
80  B(color.B),
81  WW(color.W),
82  CW(color.W)
83  {
84  };
85 
86  // ------------------------------------------------------------------------
87  // Construct a RgbwwColor using HtmlColor
88  // ------------------------------------------------------------------------
89  RgbwwColor(const HtmlColor& color);
90 
91  // ------------------------------------------------------------------------
92  // Construct a RgbwwColor using HslColor
93  // ------------------------------------------------------------------------
94  RgbwwColor(const HslColor& color);
95 
96  // ------------------------------------------------------------------------
97  // Construct a RgbwwColor using HsbColor
98  // ------------------------------------------------------------------------
99  RgbwwColor(const HsbColor& color);
100 
101  // ------------------------------------------------------------------------
102  // Construct a RgbwwColor that will have its values set in latter operations
103  // CAUTION: The R,G,B, WW, CW members are not initialized and may not be consistent
104  // ------------------------------------------------------------------------
106  {
107  };
108 
109  // ------------------------------------------------------------------------
110  // Comparison operators
111  // ------------------------------------------------------------------------
112  bool operator==(const RgbwwColor& other) const
113  {
114  return (R == other.R && G == other.G && B == other.B && WW == other.WW && CW == other.CW);
115  };
116 
117  bool operator!=(const RgbwwColor& other) const
118  {
119  return !(*this == other);
120  };
121 
122  // ------------------------------------------------------------------------
123  // CompareTo method
124  // compares against another color with the given epsilon (delta allowed)
125  // returns the greatest difference of a set of elements,
126  // 0 = equal within epsilon delta
127  // negative - this is less than other
128  // positive - this is greater than other
129  // ------------------------------------------------------------------------
130  int16_t CompareTo(const RgbwwColor& other, uint8_t epsilon = 1)
131  {
132  return _Compare<RgbwwColor, int16_t>(*this, other, epsilon);
133  }
134 
135  // ------------------------------------------------------------------------
136  // Compare method
137  // compares two colors with the given epsilon (delta allowed)
138  // returns the greatest difference of a set of elements,
139  // 0 = equal within epsilon delta
140  // negative - left is less than right
141  // positive - left is greater than right
142  // ------------------------------------------------------------------------
143  static int16_t Compare(const RgbwwColor& left, const RgbwwColor& right, uint8_t epsilon = 1)
144  {
145  return _Compare<RgbwwColor, int16_t>(left, right, epsilon);
146  }
147 
148  // ------------------------------------------------------------------------
149  // operator [] - readonly
150  // access elements in order by index rather than R,G,B,WW,CW
151  // see static Count for the number of elements
152  // ------------------------------------------------------------------------
153  uint8_t operator[](size_t idx) const
154  {
155  switch (idx)
156  {
157  case 0:
158  return R;
159  case 1:
160  return G;
161  case 2:
162  return B;
163  case 3:
164  return WW;
165  default:
166  return CW;
167  }
168  }
169 
170  // ------------------------------------------------------------------------
171  // operator [] - read write
172  // access elements in order by index rather than R,G,B,WW,CW
173  // see static Count for the number of elements
174  // ------------------------------------------------------------------------
175  uint8_t& operator[](size_t idx)
176  {
177  switch (idx)
178  {
179  case 0:
180  return R;
181  case 1:
182  return G;
183  case 2:
184  return B;
185  case 3:
186  return WW;
187  default:
188  return CW;
189  }
190  }
191 
192  // ------------------------------------------------------------------------
193  // Returns if the color is grey, all values are equal other than whites
194  // ------------------------------------------------------------------------
195  bool IsMonotone() const
196  {
197  return (R == B && R == G);
198  };
199 
200  // ------------------------------------------------------------------------
201  // Returns if the color components are all zero, the white components maybe
202  // anything
203  // ------------------------------------------------------------------------
204  bool IsColorLess() const
205  {
206  return (R == 0 && B == 0 && G == 0);
207  };
208 
209  // ------------------------------------------------------------------------
210  // CalculateBrightness will calculate the overall brightness
211  // NOTE: This is a simple linear brightness
212  // ------------------------------------------------------------------------
213  uint8_t CalculateBrightness() const;
214 
215  // ------------------------------------------------------------------------
216  // Dim will return a new color that is blended to black with the given ratio
217  // ratio - (0-255) where 255 will return the original color and 0 will return black
218  //
219  // NOTE: This is a simple linear blend
220  // ------------------------------------------------------------------------
221  RgbwwColor Dim(uint8_t ratio) const;
222 
223  // ------------------------------------------------------------------------
224  // Brighten will return a new color that is blended to white with the given ratio
225  // ratio - (0-255) where 255 will return the original color and 0 will return white
226  //
227  // NOTE: This is a simple linear blend
228  // ------------------------------------------------------------------------
229  RgbwwColor Brighten(uint8_t ratio) const;
230 
231  // ------------------------------------------------------------------------
232  // Darken will adjust the color by the given delta toward black
233  // NOTE: This is a simple linear change
234  // delta - (0-255) the amount to dim the color
235  // ------------------------------------------------------------------------
236  void Darken(uint8_t delta);
237 
238  // ------------------------------------------------------------------------
239  // Lighten will adjust the color by the given delta toward white
240  // NOTE: This is a simple linear change
241  // delta - (0-255) the amount to lighten the color
242  // ------------------------------------------------------------------------
243  void Lighten(uint8_t delta);
244 
245  // ------------------------------------------------------------------------
246  // LinearBlend between two colors by the amount defined by progress variable
247  // left - the color to start the blend at
248  // right - the color to end the blend at
249  // progress - (0.0 - 1.0) value where 0 will return left and 1.0 will return right
250  // and a value between will blend the color weighted linearly between them
251  // ------------------------------------------------------------------------
252  static RgbwwColor LinearBlend(const RgbwwColor& left, const RgbwwColor& right, float progress);
253  // progress - (0 - 255) value where 0 will return left and 255 will return right
254  // and a value between will blend the color weighted linearly between them
255  // ------------------------------------------------------------------------
256  static RgbwwColor LinearBlend(const RgbwwColor& left, const RgbwwColor& right, uint8_t progress);
257 
258  // ------------------------------------------------------------------------
259  // BilinearBlend between four colors by the amount defined by 2d variable
260  // c00 - upper left quadrant color
261  // c01 - upper right quadrant color
262  // c10 - lower left quadrant color
263  // c11 - lower right quadrant color
264  // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space
265  // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space
266  // ------------------------------------------------------------------------
267  static RgbwwColor BilinearBlend(const RgbwwColor& c00,
268  const RgbwwColor& c01,
269  const RgbwwColor& c10,
270  const RgbwwColor& c11,
271  float x,
272  float y);
273 
274  uint16_t CalcTotalTenthMilliAmpere(const SettingsObject& settings)
275  {
276  auto total = 0;
277 
278  total += R * settings.RedTenthMilliAmpere / Max;
279  total += G * settings.GreenTenthMilliAmpere / Max;
280  total += B * settings.BlueTenthMilliAmpere / Max;
281  total += WW * settings.WarmWhiteTenthMilliAmpere / Max;
282  total += CW * settings.CoolWhiteTenthMilliAmpere / Max;
283 
284  return total;
285  }
286 
287  // ------------------------------------------------------------------------
288  // Red, Green, Blue, Warm White, Cool White color members (0-255) where
289  // (0,0,0,0,0) is black and
290  // (255,255,255, 0, 0) is a white
291  // (0,0,0,255,0) is warm white and
292  // (0,0,0,0,255) is cool white and
293  // Note (255,255,255,255,255) is extreme bright white
294  // ------------------------------------------------------------------------
295  uint8_t R;
296  uint8_t G;
297  uint8_t B;
298  uint8_t WW;
299  uint8_t CW;
300 
301  const static uint8_t Max = 255;
302  const static size_t Count = 5; // four elements in []
303 
304 private:
305  inline static uint8_t _elementDim(uint8_t value, uint8_t ratio)
306  {
307  return (static_cast<uint16_t>(value) * (static_cast<uint16_t>(ratio) + 1)) >> 8;
308  }
309 
310  inline static uint8_t _elementBrighten(uint8_t value, uint8_t ratio)
311  {
312  uint16_t element = ((static_cast<uint16_t>(value) + 1) << 8) / (static_cast<uint16_t>(ratio) + 1);
313 
314  if (element > Max)
315  {
316  element = Max;
317  }
318  else
319  {
320  element -= 1;
321  }
322  return element;
323  }
324 };
325 
Definition: NeoSettings.h:101
const uint16_t GreenTenthMilliAmpere
Definition: NeoSettings.h:134
const uint16_t BlueTenthMilliAmpere
Definition: NeoSettings.h:135
const uint16_t RedTenthMilliAmpere
Definition: NeoSettings.h:133
const uint16_t WarmWhiteTenthMilliAmpere
Definition: NeoSettings.h:136
const uint16_t CoolWhiteTenthMilliAmpere
Definition: NeoSettings.h:137
Definition: HsbColor.h:35
Definition: HslColor.h:34
Definition: HtmlColor.h:70
Definition: RgbColorBase.h:34
Definition: RgbColor.h:36
Definition: RgbwColor.h:38
Definition: RgbwwColor.h:41
bool IsColorLess() const
Definition: RgbwwColor.h:204
static int16_t Compare(const RgbwwColor &left, const RgbwwColor &right, uint8_t epsilon=1)
Definition: RgbwwColor.h:143
RgbwwColor Brighten(uint8_t ratio) const
Definition: RgbwwColor.cpp:82
uint8_t CW
Definition: RgbwwColor.h:299
RgbwwColor(uint8_t brightness)
Definition: RgbwwColor.h:57
uint8_t operator[](size_t idx) const
Definition: RgbwwColor.h:153
RgbwwColor()
Definition: RgbwwColor.h:105
static const uint8_t Max
Definition: RgbwwColor.h:301
uint8_t G
Definition: RgbwwColor.h:296
bool operator==(const RgbwwColor &other) const
Definition: RgbwwColor.h:112
bool operator!=(const RgbwwColor &other) const
Definition: RgbwwColor.h:117
RgbwwColor(uint8_t r, uint8_t g, uint8_t b, uint8_t warmW=0, uint8_t coolW=0)
Definition: RgbwwColor.h:47
static const size_t Count
Definition: RgbwwColor.h:302
static RgbwwColor LinearBlend(const RgbwwColor &left, const RgbwwColor &right, float progress)
Definition: RgbwwColor.cpp:193
void Lighten(uint8_t delta)
Definition: RgbwwColor.cpp:140
uint8_t & operator[](size_t idx)
Definition: RgbwwColor.h:175
NeoRgbwwCurrentSettings SettingsObject
Definition: RgbwwColor.h:42
RgbwwColor Dim(uint8_t ratio) const
Definition: RgbwwColor.cpp:72
uint8_t CalculateBrightness() const
Definition: RgbwwColor.cpp:64
RgbwwColor(const RgbColor &color)
Definition: RgbwwColor.h:65
uint8_t WW
Definition: RgbwwColor.h:298
int16_t CompareTo(const RgbwwColor &other, uint8_t epsilon=1)
Definition: RgbwwColor.h:130
uint16_t CalcTotalTenthMilliAmpere(const SettingsObject &settings)
Definition: RgbwwColor.h:274
RgbwwColor(const RgbwColor &color)
Definition: RgbwwColor.h:77
bool IsMonotone() const
Definition: RgbwwColor.h:195
uint8_t R
Definition: RgbwwColor.h:295
static RgbwwColor BilinearBlend(const RgbwwColor &c00, const RgbwwColor &c01, const RgbwwColor &c10, const RgbwwColor &c11, float x, float y)
Definition: RgbwwColor.cpp:211
void Darken(uint8_t delta)
Definition: RgbwwColor.cpp:92
uint8_t B
Definition: RgbwwColor.h:297