This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRGBWWLedColor.h
447 lines (373 loc) · 9.34 KB
/
RGBWWLedColor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* RGBWWLed - simple Library for controlling RGB WarmWhite ColdWhite LEDs via PWM
* @file
* @author Patrick Jahns http://github.com/patrickjahns
*
* All files of this project are provided under the LGPL v3 license.
*/
#ifndef RGBWWLedColor_h
#define RGBWWLedColor_h
#include "RGBWWLed.h"
enum RGBWW_COLORMODE {
RGB = 0,
RGBWW = 1,
RGBCW = 2,
RGBWWCW = 3,
NUM_COLORMODES = 4
};
enum RGBWW_HSVMODEL {
RAW = 0,
SPEKTRUM = 1,
RAINBOW = 2,
NUM_HSVMODELS = 3
};
enum RGBWW_CHANNELS {
RED = 0,
GREEN = 1,
BLUE = 2,
WW = 3,
CW = 4,
NUM_CHANNELS = 5
};
//struct for RGBW + Kelvin
struct RGBWCT {
union {
int r;
int red;
};
union {
int g;
int green;
};
union {
int b;
int blue;
};
union {
int w;
int white;
};
union {
int ct;
int colortemp;
};
RGBWCT() {}
RGBWCT(int red, int green, int blue, int white) : r(red), g(green), b(blue), w(white), ct(0) {}
RGBWCT(int red, int green, int blue, int white, int kelvin) : r(red), g(green), b(blue), w(white), ct(kelvin) {}
RGBWCT(const RGBWCT& rgbwct)
{
this->r = rgbwct.r;
this->g = rgbwct.g;
this->b = rgbwct.b;
this->w = rgbwct.w;
this->ct = rgbwct.ct;
}
RGBWCT& operator= (const RGBWCT& rgbwct)
{
this->r = rgbwct.r;
this->g = rgbwct.g;
this->b = rgbwct.b;
this->w = rgbwct.w;
this->ct = rgbwct.ct;
return *this;
}
};
struct ChannelOutput {
union {
int r;
int red;
};
union {
int g;
int green;
};
union {
int b;
int blue;
};
union {
int ww;
int warmwhite;
};
union {
int cw;
int coldwhite;
};
ChannelOutput() {}
ChannelOutput(int red, int green, int blue, int warmwhite, int coldwhite) : r(red), g(green), b(blue), ww(warmwhite), cw(coldwhite) {}
ChannelOutput(const ChannelOutput& output)
{
this->r = output.r;
this->g = output.g;
this->b = output.b;
this->ww = output.ww;
this->cw = output.cw;
}
ChannelOutput& operator= (const ChannelOutput& output)
{
this->r = output.r;
this->g = output.g;
this->b = output.b;
this->ww = output.ww;
this->cw = output.cw;
return *this;
}
};
// struct for HSV + Kelvin
struct HSVCT {
union {
int h;
int hue;
};
union {
int s;
int sat;
int saturation;
};
union {
int v;
int val;
int value;
};
union {
int ct;
int colortemp;
};
HSVCT() {}
HSVCT(int hue, int sat, int val) : h(hue), s(sat), v(val), ct(0) {}
HSVCT(int hue, int sat, int val, int ct) : h(hue), s(sat), v(val), ct(ct) {}
//construct from float values
HSVCT( float hue, float sat, float val) {
this->h = (constrain(hue, 0.0, 360.0) / 360) * RGBWW_CALC_HUEWHEELMAX;
this->s = (constrain(sat, 0.0, 100.0) / 100) * RGBWW_CALC_MAXVAL;
this->v = (constrain(val, 0.0, 100.0) / 100) * RGBWW_CALC_MAXVAL;
//TODO: default value for White color?
this->ct = 0;
}
HSVCT( float hue, float sat, float val, int ct) {
this->h = (constrain(hue, 0.0, 360.0) / 360) * RGBWW_CALC_HUEWHEELMAX;
this->s = (constrain(sat, 0.0, 100.0) / 100) * RGBWW_CALC_MAXVAL;
this->v = (constrain(val, 0.0, 100.0) / 100) * RGBWW_CALC_MAXVAL;
this->ct = constrain(ct, 0, 10000);
}
HSVCT(const HSVCT& hsvk)
{
this->h = hsvk.h;
this->s = hsvk.s;
this->v = hsvk.v;
this->ct = hsvk.ct;
}
HSVCT& operator= (const HSVCT& hsvct)
{
this->h = hsvct.h;
this->s = hsvct.s;
this->v = hsvct.v;
this->ct = hsvct.ct;
return *this;
}
void asRadian(float& hue, float& sat, float& val) {
hue = (float(h) / float(RGBWW_CALC_HUEWHEELMAX)) * 360.0;
sat = (float(s) / float(RGBWW_CALC_MAXVAL)) * 100.0;
val = (float(v) / float(RGBWW_CALC_MAXVAL)) * 100.0;
}
void asRadian(float& hue, float& sat, float& val, int& ct) {
hue = (float(this->h) / float(RGBWW_CALC_HUEWHEELMAX)) * 360.0;
sat = (float(this->s) / float(RGBWW_CALC_MAXVAL)) * 100.0;
val = (float(this->v) / float(RGBWW_CALC_MAXVAL)) * 100.0;
ct = this->ct;
}
};
struct COLOR {
union {
RGBWCT rgbw;
HSVCT hsv;
};
union {
int ct;
int colortemp;
};
};
/**
* Class with functions for converting between different colorspaces
* (HSVK, RGBWK), changing outputmodes (RGBWW_COLORMODE) and
* calculating color temperature and white balance
*
*/
class RGBWWColorUtils {
public:
RGBWWColorUtils();
virtual ~RGBWWColorUtils(){};
/**
* Set the output setting of the controler.
* See RGBWW_COLORMODE for modes
*
* @param mode RGBWW_COLORMODE
*/
void setColorMode(RGBWW_COLORMODE mode);
/**
* Get the current output setting
*
* @return RGBWW_COLORMODE
*/
RGBWW_COLORMODE getColorMode();
/**
* set the default conversion for HSVK to RGBWK color space.
* For valid models see RGBWW_HSVMODEL
*
* @param model RGBWW_HSVMODEL models
*/
void setHSVmodel(RGBWW_HSVMODEL model);
/**
* Returns the model used for default conversion between hsvk and rgbwk
*
* @return RGBWW_HSVMODEL
*/
RGBWW_HSVMODEL getHSVmodel();
/**
* Set the color temperature for warm/cold white channel in kelvin
*
* @param WarmWhite color temperatur of warm white channel in kelvin
* @param ColdWhite color temperature of cold white channel in kelvin
*/
void setWhiteTemperature(int WarmWhite, int ColdWhite);
/**
* Returns the settings for the warm/cold white color temperatures
*
* @param WarmWhite color temperatur of warm white channel in kelvin
* @param ColdWhite color temperature of cold white channel in kelvin
*/
void getWhiteTemperature(int& WarmWhite, int& ColdWhite);
/**
* Correction for HSVtoRGB Normal Mode. Moves the boundaries
* of each color further left/right. Assumes all variables are
* contained in [-30.0, 30.0]
*
* More information on HSV colorspace see:
* https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
*
* @param float red
* @param float yellow
* @param float green
* @param float cyan
* @param float blue
* @param float magenta
*/
void setHSVcorrection(float red, float yellow, float green, float cyan, float blue, float magenta);
/**
* Copies the current values used for HSV correction into the
* provided params
*
* @param float& red
* @param float& yellow
* @param float& green
* @param float& cyan
* @param float& blue
* @param float& magenta
*/
void getHSVcorrection(float& red, float& yellow, float& green, float& cyan, float& blue, float& magenta);
/**
* Set the maximum brightness if output for the channels
* Assumes that r,g,b,cw and ww are contained in the
* set [0 , 100]
*
* @param int r red channel
* @param int g green channel
* @param int b blue channel
* @param int cw cold white channel
* @param int ww warm white channel (
*
*/
void setBrightnessCorrection(int r, int g, int b, int ww, int cw);
/**
* Copies the current value of the brightness factor into the specified variables
*
* @param int& r
* @param int& g
* @param int& b
* @param int& ww
* @param int& cw
*/
void getBrightnessCorrection(int& r, int& g, int& b, int& ww, int& cw);
/**
* Applies the white colortemperature
*
* @param RGBWK& rgbw
* @param int& ww
* @param int& cw
*/
void whiteBalance(RGBWCT& rgbw, ChannelOutput& output);
/**
* Corrects the values in the parsed array according to the set
* brightness correction
*
* @param int[] color
*/
void correctBrightness(ChannelOutput& output);
/**
* Convert HSVK Values to RGBK colorspace
* Uses to conversion model set with setHSVmodel
*
* @param hsvk HSVK struct with values
* @param rgbwk RGBWK struct to hold result
*/
void HSVtoRGB(const HSVCT& hsvk, RGBWCT& rgbwk);
/**
* Convert HSVK Values to RGBK colorspace
*
* @param hsvk HSVK struct with values
* @param rgbwk RGBWK struct to hold result
* @param mode conversion model to be used (RGBWW_HSVMODEL)
*/
void HSVtoRGB(const HSVCT& hsvk, RGBWCT& rgbwk, RGBWW_HSVMODEL mode);
/**
* Convert HSV values to RGB colorspace using the algorithm
* from https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
*
*
* @param hsvk HSVK struct with values
* @param rgbwk RGBWK struct to hold result
*/
void HSVtoRGBraw(const HSVCT& hsvk, RGBWCT& rgbwk);
/**
* Convert HSV values to RGB colorspace by keeping
* the max total color output equal.
* Information see:
* https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors
*
* @param hsvk HSVK struct with values
* @param rgbwk RGBWK struct to hold result
*/
void HSVtoRGBspektrum(const HSVCT& hsvk, RGBWCT& rgbwk);
/**
* Convert HSV values to RGB colorspace with rainbow color table
* Information see:
* https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors
*
* @param hsvk HSVK struct with values
* @param rgbwk RGBWK struct to hold result
*/
void HSVtoRGBrainbow(const HSVCT& hsvk, RGBWCT& rgbwk);
/**
*
* @param rgbwk
* @param hsvk
*/
void RGBtoHSV(const RGBWCT& rgbwk, HSVCT& hsvk);
/**
* Helper function to keep HUE within boundaries [0, HUELWHEELMAX]
*
* @param hue
*/
static void circleHue(int& hue);
private:
int _BrightnessFactor[RGBWW_CHANNELS::NUM_CHANNELS];
int _HueWheelSector[7];
int _HueWheelSectorWidth[6];
int _WarmWhiteKelvin;
int _ColdWhiteKelvin;
RGBWW_COLORMODE _colormode;
RGBWW_HSVMODEL _hsvmodel;
static int parseColorCorrection(float val);
void createHueWheel();
};
#endif