23
23
// define a clamp macro to substitute the std::clamp macro which is available from C++17 onwards
24
24
#define clamp (a , min , max ) ((a) < (min) ? (min) : ((a) > (max) ? (max) : (a)))
25
25
26
- const espHsvColor_t HSV_BLACK = {0 , 0 , 0 };
27
- const espHsvColor_t HSV_WHITE = {0 , 0 , 254 };
28
- const espHsvColor_t HSV_RED = {0 , 254 , 254 };
29
- const espHsvColor_t HSV_YELLOW = {42 , 254 , 254 };
30
- const espHsvColor_t HSV_GREEN = {84 , 254 , 254 };
31
- const espHsvColor_t HSV_CYAN = {127 , 254 , 254 };
32
- const espHsvColor_t HSV_BLUE = {169 , 254 , 254 };
26
+ const espHsvColor_t HSV_BLACK = {0 , 0 , 0 };
27
+ const espHsvColor_t HSV_WHITE = {0 , 0 , 254 };
28
+ const espHsvColor_t HSV_RED = {0 , 254 , 254 };
29
+ const espHsvColor_t HSV_YELLOW = {42 , 254 , 254 };
30
+ const espHsvColor_t HSV_GREEN = {84 , 254 , 254 };
31
+ const espHsvColor_t HSV_CYAN = {127 , 254 , 254 };
32
+ const espHsvColor_t HSV_BLUE = {169 , 254 , 254 };
33
33
const espHsvColor_t HSV_MAGENTA = {211 , 254 , 254 };
34
34
35
- const espRgbColor_t RGB_BLACK = {0 , 0 , 0 };
36
- const espRgbColor_t RGB_WHITE = {255 , 255 , 255 };
37
- const espRgbColor_t RGB_RED = {255 , 0 , 0 };
38
- const espRgbColor_t RGB_YELLOW = {255 , 255 , 0 };
39
- const espRgbColor_t RGB_GREEN = {0 , 255 , 0 };
40
- const espRgbColor_t RGB_CYAN = {0 , 255 , 255 };
41
- const espRgbColor_t RGB_BLUE = {0 , 0 , 255 };
42
- const espRgbColor_t RGB_MAGENTA = {255 , 0 , 255 };
35
+ const espRgbColor_t RGB_BLACK = {0 , 0 , 0 };
36
+ const espRgbColor_t RGB_WHITE = {255 , 255 , 255 };
37
+ const espRgbColor_t RGB_RED = {255 , 0 , 0 };
38
+ const espRgbColor_t RGB_YELLOW = {255 , 255 , 0 };
39
+ const espRgbColor_t RGB_GREEN = {0 , 255 , 0 };
40
+ const espRgbColor_t RGB_CYAN = {0 , 255 , 255 };
41
+ const espRgbColor_t RGB_BLUE = {0 , 0 , 255 };
42
+ const espRgbColor_t RGB_MAGENTA = {255 , 0 , 255 };
43
43
44
44
// main color temperature values
45
- const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE = { 142 };
46
- const espCtColor_t DAYLIGHT_WHITE_COLOR_TEMPERATURE = { 181 };
47
- const espCtColor_t WHITE_COLOR_TEMPERATURE = { 250 };
48
- const espCtColor_t SOFT_WHITE_COLOR_TEMPERATURE = { 370 };
49
- const espCtColor_t WARM_WHITE_COLOR_TEMPERATURE = { 454 };
45
+ const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE = {142 };
46
+ const espCtColor_t DAYLIGHT_WHITE_COLOR_TEMPERATURE = {181 };
47
+ const espCtColor_t WHITE_COLOR_TEMPERATURE = {250 };
48
+ const espCtColor_t SOFT_WHITE_COLOR_TEMPERATURE = {370 };
49
+ const espCtColor_t WARM_WHITE_COLOR_TEMPERATURE = {454 };
50
50
51
51
espRgbColor_t espHsvToRgbColor (uint16_t h , uint8_t s , uint8_t v ) {
52
52
espHsvColor_t hsv = {h , s , v };
@@ -56,44 +56,32 @@ espRgbColor_t espHsvToRgbColor(uint16_t h, uint8_t s, uint8_t v) {
56
56
espRgbColor_t espHsvColorToRgbColor (espHsvColor_t hsv ) {
57
57
espRgbColor_t rgb ;
58
58
59
- uint8_t region , p , q , t ;
60
- uint32_t h , s , v , remainder ;
59
+ uint8_t region , p , q , t ;
60
+ uint32_t h , s , v , remainder ;
61
61
62
- if (hsv .s == 0 ) {
63
- rgb .r = rgb .g = rgb .b = hsv .v ;
64
- } else {
65
- h = hsv .h ;
66
- s = hsv .s ;
67
- v = hsv .v ;
68
-
69
- region = h / 43 ;
70
- remainder = (h - (region * 43 )) * 6 ;
71
- p = (v * (255 - s )) >> 8 ;
72
- q = (v * (255 - ((s * remainder ) >> 8 ))) >> 8 ;
73
- t = (v * (255 - ((s * (255 - remainder )) >> 8 ))) >> 8 ;
74
- switch (region ) {
75
- case 0 :
76
- rgb .r = v , rgb .g = t , rgb .b = p ;
77
- break ;
78
- case 1 :
79
- rgb .r = q , rgb .g = v , rgb .b = p ;
80
- break ;
81
- case 2 :
82
- rgb .r = p , rgb .g = v , rgb .b = t ;
83
- break ;
84
- case 3 :
85
- rgb .r = p , rgb .g = q , rgb .b = v ;
86
- break ;
87
- case 4 :
88
- rgb .r = t , rgb .g = p , rgb .b = v ;
89
- break ;
90
- case 5 :
91
- default :
92
- rgb .r = v , rgb .g = p , rgb .b = q ;
93
- break ;
94
- }
62
+ if (hsv .s == 0 ) {
63
+ rgb .r = rgb .g = rgb .b = hsv .v ;
64
+ } else {
65
+ h = hsv .h ;
66
+ s = hsv .s ;
67
+ v = hsv .v ;
68
+
69
+ region = h / 43 ;
70
+ remainder = (h - (region * 43 )) * 6 ;
71
+ p = (v * (255 - s )) >> 8 ;
72
+ q = (v * (255 - ((s * remainder ) >> 8 ))) >> 8 ;
73
+ t = (v * (255 - ((s * (255 - remainder )) >> 8 ))) >> 8 ;
74
+ switch (region ) {
75
+ case 0 : rgb .r = v , rgb .g = t , rgb .b = p ; break ;
76
+ case 1 : rgb .r = q , rgb .g = v , rgb .b = p ; break ;
77
+ case 2 : rgb .r = p , rgb .g = v , rgb .b = t ; break ;
78
+ case 3 : rgb .r = p , rgb .g = q , rgb .b = v ; break ;
79
+ case 4 : rgb .r = t , rgb .g = p , rgb .b = v ; break ;
80
+ case 5 :
81
+ default : rgb .r = v , rgb .g = p , rgb .b = q ; break ;
95
82
}
96
- return rgb ;
83
+ }
84
+ return rgb ;
97
85
}
98
86
99
87
espHsvColor_t espRgbToHsvColor (uint8_t r , uint8_t g , uint8_t b ) {
@@ -110,22 +98,22 @@ espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb) {
110
98
111
99
hsv .v = rgbMax ;
112
100
if (hsv .v == 0 ) {
113
- hsv .h = 0 ;
114
- hsv .s = 0 ;
115
- return hsv ;
101
+ hsv .h = 0 ;
102
+ hsv .s = 0 ;
103
+ return hsv ;
116
104
}
117
105
118
106
hsv .s = 255 * (rgbMax - rgbMin ) / hsv .v ;
119
107
if (hsv .s == 0 ) {
120
- hsv .h = 0 ;
121
- return hsv ;
108
+ hsv .h = 0 ;
109
+ return hsv ;
122
110
}
123
111
if (rgbMax == rgb .r ) {
124
- hsv .h = 0 + 43 * (rgb .g - rgb .b ) / (rgbMax - rgbMin );
112
+ hsv .h = 0 + 43 * (rgb .g - rgb .b ) / (rgbMax - rgbMin );
125
113
} else if (rgbMax == rgb .g ) {
126
- hsv .h = 85 + 43 * (rgb .b - rgb .r ) / (rgbMax - rgbMin );
114
+ hsv .h = 85 + 43 * (rgb .b - rgb .r ) / (rgbMax - rgbMin );
127
115
} else {
128
- hsv .h = 171 + 43 * (rgb .r - rgb .g ) / (rgbMax - rgbMin );
116
+ hsv .h = 171 + 43 * (rgb .r - rgb .g ) / (rgbMax - rgbMin );
129
117
}
130
118
return hsv ;
131
119
}
@@ -194,7 +182,7 @@ espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t curren
194
182
return rgb ;
195
183
}
196
184
197
- espXyColor_t espRgbToXYColor (uint8_t r , uint8_t g , uint8_t b ){
185
+ espXyColor_t espRgbToXYColor (uint8_t r , uint8_t g , uint8_t b ) {
198
186
espRgbColor_t rgb = {r , g , b };
199
187
return espRgbColorToXYColor (rgb );
200
188
}
@@ -242,7 +230,7 @@ espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb) {
242
230
return xy ;
243
231
}
244
232
245
- espRgbColor_t espCTToRgbColor (uint16_t ct ){
233
+ espRgbColor_t espCTToRgbColor (uint16_t ct ) {
246
234
espCtColor_t ctColor = {ct };
247
235
return espCTColorToRgbColor (ctColor );
248
236
}
0 commit comments