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