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