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