ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#pragma once
2
3#include "defines.h"
4#include "component.h"
5#include "helpers.h"
6
7#ifdef USE_LVGL
9#endif // USE_LVGL
10
11namespace esphome {
12
13inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) {
14 return (uint16_t(i) * (1 + uint16_t(scale))) / 256;
15}
16
22inline static constexpr uint8_t esp_scale8_twice(uint8_t i, uint8_t scale1, uint8_t scale2) {
23 return (uint32_t(i) * (1 + uint32_t(scale1)) * (1 + uint32_t(scale2))) >> 16;
24}
25
26// Index of each channel into the raw color value array.
27static constexpr uint8_t COLOR_CHANNEL_R = 0;
28static constexpr uint8_t COLOR_CHANNEL_G = 1;
29static constexpr uint8_t COLOR_CHANNEL_B = 2;
30static constexpr uint8_t COLOR_CHANNEL_W = 3;
31
32struct Color {
33 union {
34 struct {
35 union {
36 uint8_t r;
37 uint8_t red;
38 };
39 union {
40 uint8_t g;
41 uint8_t green;
42 };
43 union {
44 uint8_t b;
45 uint8_t blue;
46 };
47 union {
48 uint8_t w;
49 uint8_t white;
50 };
51 };
52 uint8_t raw[4];
54 };
55
56#ifdef USE_LVGL
57 // convenience function for Color to get a lv_color_t representation
58 operator lv_color_t() const { return lv_color_make(this->r, this->g, this->b); }
59#endif
60
61 inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT
62 inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red),
63 g(green),
64 b(blue),
65 w(0) {}
66
67 inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
68 g(green),
69 b(blue),
70 w(white) {}
71 inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
72 g((colorcode >> 8) & 0xFF),
73 b((colorcode >> 0) & 0xFF),
74 w((colorcode >> 24) & 0xFF) {}
75
76 inline bool is_on() const ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; }
77
78 inline bool operator==(const Color &rhs) const { // NOLINT
79 return this->raw_32 == rhs.raw_32;
80 }
81 inline bool operator==(uint32_t colorcode) const { // NOLINT
82 return this->raw_32 == colorcode;
83 }
84 inline bool operator!=(const Color &rhs) const { // NOLINT
85 return this->raw_32 != rhs.raw_32;
86 }
87 inline bool operator!=(uint32_t colorcode) const { // NOLINT
88 return this->raw_32 != colorcode;
89 }
90 inline uint8_t &operator[](uint8_t x) ESPHOME_ALWAYS_INLINE { return this->raw[x]; }
91 inline const uint8_t &operator[](uint8_t x) const ESPHOME_ALWAYS_INLINE { return this->raw[x]; }
92 inline Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE {
93 return Color(esp_scale8(this->red, scale), esp_scale8(this->green, scale), esp_scale8(this->blue, scale),
94 esp_scale8(this->white, scale));
95 }
96 inline Color operator~() const ESPHOME_ALWAYS_INLINE {
97 return Color(255 - this->red, 255 - this->green, 255 - this->blue);
98 }
99 inline Color &operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE {
100 this->red = esp_scale8(this->red, scale);
101 this->green = esp_scale8(this->green, scale);
102 this->blue = esp_scale8(this->blue, scale);
103 this->white = esp_scale8(this->white, scale);
104 return *this;
105 }
106 inline Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE {
107 return Color(esp_scale8(this->red, scale.red), esp_scale8(this->green, scale.green),
108 esp_scale8(this->blue, scale.blue), esp_scale8(this->white, scale.white));
109 }
110 inline Color &operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE {
111 this->red = esp_scale8(this->red, scale.red);
112 this->green = esp_scale8(this->green, scale.green);
113 this->blue = esp_scale8(this->blue, scale.blue);
114 this->white = esp_scale8(this->white, scale.white);
115 return *this;
116 }
117 inline Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE {
118 Color ret;
119 if (uint8_t(add.r + this->r) < this->r) {
120 ret.r = 255;
121 } else {
122 ret.r = this->r + add.r;
123 }
124 if (uint8_t(add.g + this->g) < this->g) {
125 ret.g = 255;
126 } else {
127 ret.g = this->g + add.g;
128 }
129 if (uint8_t(add.b + this->b) < this->b) {
130 ret.b = 255;
131 } else {
132 ret.b = this->b + add.b;
133 }
134 if (uint8_t(add.w + this->w) < this->w) {
135 ret.w = 255;
136 } else {
137 ret.w = this->w + add.w;
138 }
139 return ret;
140 }
141 inline Color &operator+=(const Color &add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
142 inline Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
143 inline Color &operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
144 inline Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE {
145 Color ret;
146 if (subtract.r > this->r) {
147 ret.r = 0;
148 } else {
149 ret.r = this->r - subtract.r;
150 }
151 if (subtract.g > this->g) {
152 ret.g = 0;
153 } else {
154 ret.g = this->g - subtract.g;
155 }
156 if (subtract.b > this->b) {
157 ret.b = 0;
158 } else {
159 ret.b = this->b - subtract.b;
160 }
161 if (subtract.w > this->w) {
162 ret.w = 0;
163 } else {
164 ret.w = this->w - subtract.w;
165 }
166 return ret;
167 }
168 inline Color &operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
169 inline Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE {
170 return (*this) - Color(subtract, subtract, subtract, subtract);
171 }
172 inline Color &operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
174 uint32_t rand = random_uint32();
175 uint8_t w = rand >> 24;
176 uint8_t r = rand >> 16;
177 uint8_t g = rand >> 8;
178 uint8_t b = rand >> 0;
179 const uint16_t max_rgb = std::max({r, g, b, uint8_t(1)});
180 return Color(uint8_t((uint16_t(r) * 255U / max_rgb)), uint8_t((uint16_t(g) * 255U / max_rgb)),
181 uint8_t((uint16_t(b) * 255U / max_rgb)), w);
182 }
183
187 static inline uint8_t blend_channel(uint8_t from, uint8_t to, uint8_t amnt) ESPHOME_ALWAYS_INLINE {
188 return (uint16_t(from) * (255 - amnt) + uint16_t(to) * amnt) / 255;
189 }
190 Color gradient(const Color &to_color, uint8_t amnt) const;
191 Color fade_to_white(uint8_t amnt) const;
192 Color fade_to_black(uint8_t amnt) const;
193
194 Color lighten(uint8_t delta) { return *this + delta; }
195 Color darken(uint8_t delta) { return *this - delta; }
196
197 static const Color BLACK;
198 static const Color WHITE;
199};
200
201static_assert(offsetof(Color, raw) == COLOR_CHANNEL_R);
202static_assert(offsetof(Color, r) == COLOR_CHANNEL_R);
203static_assert(offsetof(Color, g) == COLOR_CHANNEL_G);
204static_assert(offsetof(Color, b) == COLOR_CHANNEL_B);
205static_assert(offsetof(Color, w) == COLOR_CHANNEL_W);
206
207} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
int ret
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:10
static void uint32_t
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Definition color.h:142
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
Definition color.h:143
bool operator!=(uint32_t colorcode) const
Definition color.h:87
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:144
constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
Definition color.h:71
bool is_on() const ESPHOME_ALWAYS_INLINE
Definition color.h:76
Color lighten(uint8_t delta)
Definition color.h:194
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:92
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
Definition color.h:99
static Color random_color()
Definition color.h:173
uint8_t red
Definition color.h:37
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:172
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:106
uint8_t w
Definition color.h:48
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
Definition color.h:117
bool operator==(const Color &rhs) const
Definition color.h:78
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:169
constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
Definition color.h:62
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Definition color.h:90
Color gradient(const Color &to_color, uint8_t amnt) const
Definition color.cpp:9
static uint8_t blend_channel(uint8_t from, uint8_t to, uint8_t amnt) ESPHOME_ALWAYS_INLINE
One channel of gradient(): from at amnt 0 to to at amnt 255.
Definition color.h:187
bool operator==(uint32_t colorcode) const
Definition color.h:81
uint8_t raw[4]
Definition color.h:52
uint8_t g
Definition color.h:40
Color darken(uint8_t delta)
Definition color.h:195
Color fade_to_white(uint8_t amnt) const
Definition color.cpp:14
Color fade_to_black(uint8_t amnt) const
Definition color.cpp:16
uint8_t white
Definition color.h:49
bool operator!=(const Color &rhs) const
Definition color.h:84
uint8_t green
Definition color.h:41
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:168
uint8_t b
Definition color.h:44
uint32_t raw_32
Definition color.h:53
constexpr Color() ESPHOME_ALWAYS_INLINE
Definition color.h:61
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
Definition color.h:110
Color operator~() const ESPHOME_ALWAYS_INLINE
Definition color.h:96
static const Color WHITE
Definition color.h:198
uint8_t r
Definition color.h:36
constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Definition color.h:67
static const Color BLACK
Definition color.h:197
uint8_t blue
Definition color.h:45
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
Definition color.h:141
const uint8_t & operator[](uint8_t x) const ESPHOME_ALWAYS_INLINE
Definition color.h:91
uint16_t x
Definition tt21100.cpp:5