ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
esp_hsv_color.cpp
Go to the documentation of this file.
1#include "esp_hsv_color.h"
2
3namespace esphome::light {
4
6 // based on FastLED's hsv rainbow to rgb
7 const uint8_t hue = this->hue;
8 const uint8_t sat = this->saturation;
9 const uint8_t val = this->value;
10 // upper 3 hue bits are for branch selection, lower 5 are for values
11 const uint8_t offset8 = (hue & 0x1F) << 3; // 0..248
12 // third of the offset, 255/3 = 85 (actually only up to 82; 164)
13 const uint8_t third = esp_scale8(offset8, 85);
14 const uint8_t two_thirds = esp_scale8(offset8, 170);
15 Color rgb(255, 255, 255, 0);
16 switch (hue >> 5) {
17 case 0b000:
18 rgb.r = 255 - third;
19 rgb.g = third;
20 rgb.b = 0;
21 break;
22 case 0b001:
23 rgb.r = 171;
24 rgb.g = 85 + third;
25 rgb.b = 0;
26 break;
27 case 0b010:
28 rgb.r = 171 - two_thirds;
29 rgb.g = 170 + third;
30 rgb.b = 0;
31 break;
32 case 0b011:
33 rgb.r = 0;
34 rgb.g = 255 - third;
35 rgb.b = third;
36 break;
37 case 0b100:
38 rgb.r = 0;
39 rgb.g = 171 - two_thirds;
40 rgb.b = 85 + two_thirds;
41 break;
42 case 0b101:
43 rgb.r = third;
44 rgb.g = 0;
45 rgb.b = 255 - third;
46 break;
47 case 0b110:
48 rgb.r = 85 + third;
49 rgb.g = 0;
50 rgb.b = 171 - third;
51 break;
52 case 0b111:
53 rgb.r = 170 + third;
54 rgb.g = 0;
55 rgb.b = 85 - third;
56 break;
57 default:
58 break;
59 }
60 // low saturation -> add uniform color to orig. hue
61 // high saturation -> use hue directly
62 // scales with square of saturation
63 // (r,g,b) = (r,g,b) * sat + (1 - sat)^2
64 rgb *= sat;
65 const uint8_t desat = 255 - sat;
66 rgb += esp_scale8(desat, desat);
67 // (r,g,b) = (r,g,b) * val
68 rgb *= val;
69 return rgb;
70}
71
72} // namespace esphome::light
mopeka_std_values val[4]
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30