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