ESPHome 2025.12.0-dev
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
26struct Color {
27 union {
28 struct {
29 union {
30 uint8_t r;
31 uint8_t red;
32 };
33 union {
34 uint8_t g;
35 uint8_t green;
36 };
37 union {
38 uint8_t b;
39 uint8_t blue;
40 };
41 union {
42 uint8_t w;
43 uint8_t white;
44 };
45 };
46 uint8_t raw[4];
47 uint32_t raw_32;
48 };
49
50#ifdef USE_LVGL
51 // convenience function for Color to get a lv_color_t representation
52 operator lv_color_t() const { return lv_color_make(this->r, this->g, this->b); }
53#endif
54
55 inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT
56 inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red),
57 g(green),
58 b(blue),
59 w(0) {}
60
61 inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
62 g(green),
63 b(blue),
64 w(white) {}
65 inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
66 g((colorcode >> 8) & 0xFF),
67 b((colorcode >> 0) & 0xFF),
68 w((colorcode >> 24) & 0xFF) {}
69
70 inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; }
71
72 inline bool operator==(const Color &rhs) { // NOLINT
73 return this->raw_32 == rhs.raw_32;
74 }
75 inline bool operator==(uint32_t colorcode) { // NOLINT
76 return this->raw_32 == colorcode;
77 }
78 inline bool operator!=(const Color &rhs) { // NOLINT
79 return this->raw_32 != rhs.raw_32;
80 }
81 inline bool operator!=(uint32_t colorcode) { // NOLINT
82 return this->raw_32 != colorcode;
83 }
84 inline uint8_t &operator[](uint8_t x) ESPHOME_ALWAYS_INLINE { return this->raw[x]; }
85 inline Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE {
86 return Color(esp_scale8(this->red, scale), esp_scale8(this->green, scale), esp_scale8(this->blue, scale),
87 esp_scale8(this->white, scale));
88 }
89 inline Color operator~() const ESPHOME_ALWAYS_INLINE {
90 return Color(255 - this->red, 255 - this->green, 255 - this->blue);
91 }
92 inline Color &operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE {
93 this->red = esp_scale8(this->red, scale);
94 this->green = esp_scale8(this->green, scale);
95 this->blue = esp_scale8(this->blue, scale);
96 this->white = esp_scale8(this->white, scale);
97 return *this;
98 }
99 inline Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE {
100 return Color(esp_scale8(this->red, scale.red), esp_scale8(this->green, scale.green),
101 esp_scale8(this->blue, scale.blue), esp_scale8(this->white, scale.white));
102 }
103 inline Color &operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE {
104 this->red = esp_scale8(this->red, scale.red);
105 this->green = esp_scale8(this->green, scale.green);
106 this->blue = esp_scale8(this->blue, scale.blue);
107 this->white = esp_scale8(this->white, scale.white);
108 return *this;
109 }
110 inline Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE {
111 Color ret;
112 if (uint8_t(add.r + this->r) < this->r) {
113 ret.r = 255;
114 } else {
115 ret.r = this->r + add.r;
116 }
117 if (uint8_t(add.g + this->g) < this->g) {
118 ret.g = 255;
119 } else {
120 ret.g = this->g + add.g;
121 }
122 if (uint8_t(add.b + this->b) < this->b) {
123 ret.b = 255;
124 } else {
125 ret.b = this->b + add.b;
126 }
127 if (uint8_t(add.w + this->w) < this->w) {
128 ret.w = 255;
129 } else {
130 ret.w = this->w + add.w;
131 }
132 return ret;
133 }
134 inline Color &operator+=(const Color &add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
135 inline Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
136 inline Color &operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
137 inline Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE {
138 Color ret;
139 if (subtract.r > this->r) {
140 ret.r = 0;
141 } else {
142 ret.r = this->r - subtract.r;
143 }
144 if (subtract.g > this->g) {
145 ret.g = 0;
146 } else {
147 ret.g = this->g - subtract.g;
148 }
149 if (subtract.b > this->b) {
150 ret.b = 0;
151 } else {
152 ret.b = this->b - subtract.b;
153 }
154 if (subtract.w > this->w) {
155 ret.w = 0;
156 } else {
157 ret.w = this->w - subtract.w;
158 }
159 return ret;
160 }
161 inline Color &operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
162 inline Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE {
163 return (*this) - Color(subtract, subtract, subtract, subtract);
164 }
165 inline Color &operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
167 uint32_t rand = random_uint32();
168 uint8_t w = rand >> 24;
169 uint8_t r = rand >> 16;
170 uint8_t g = rand >> 8;
171 uint8_t b = rand >> 0;
172 const uint16_t max_rgb = std::max(r, std::max(g, b));
173 return Color(uint8_t((uint16_t(r) * 255U / max_rgb)), uint8_t((uint16_t(g) * 255U / max_rgb)),
174 uint8_t((uint16_t(b) * 255U / max_rgb)), w);
175 }
176
177 Color gradient(const Color &to_color, uint8_t amnt) {
178 Color new_color;
179 float amnt_f = float(amnt) / 255.0f;
180 new_color.r = amnt_f * (to_color.r - (*this).r) + (*this).r;
181 new_color.g = amnt_f * (to_color.g - (*this).g) + (*this).g;
182 new_color.b = amnt_f * (to_color.b - (*this).b) + (*this).b;
183 new_color.w = amnt_f * (to_color.w - (*this).w) + (*this).w;
184 return new_color;
185 }
186 Color fade_to_white(uint8_t amnt) { return (*this).gradient(Color::WHITE, amnt); }
187 Color fade_to_black(uint8_t amnt) { return (*this).gradient(Color::BLACK, amnt); }
188
189 Color lighten(uint8_t delta) { return *this + delta; }
190 Color darken(uint8_t delta) { return *this - delta; }
191
192 static const Color BLACK;
193 static const Color WHITE;
194};
195
196} // namespace esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Definition color.h:135
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
Definition color.h:136
bool operator!=(const Color &rhs)
Definition color.h:78
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:137
constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
Definition color.h:65
Color fade_to_white(uint8_t amnt)
Definition color.h:186
Color lighten(uint8_t delta)
Definition color.h:189
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:85
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
Definition color.h:92
static Color random_color()
Definition color.h:166
uint8_t red
Definition color.h:31
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:165
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:99
uint8_t w
Definition color.h:42
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
Definition color.h:110
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:162
constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
Definition color.h:56
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Definition color.h:84
bool operator!=(uint32_t colorcode)
Definition color.h:81
bool operator==(uint32_t colorcode)
Definition color.h:75
uint8_t raw[4]
Definition color.h:46
uint8_t g
Definition color.h:34
Color darken(uint8_t delta)
Definition color.h:190
uint8_t white
Definition color.h:43
uint8_t green
Definition color.h:35
Color fade_to_black(uint8_t amnt)
Definition color.h:187
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:161
Color gradient(const Color &to_color, uint8_t amnt)
Definition color.h:177
uint8_t b
Definition color.h:38
uint32_t raw_32
Definition color.h:47
constexpr Color() ESPHOME_ALWAYS_INLINE
Definition color.h:55
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
Definition color.h:103
Color operator~() const ESPHOME_ALWAYS_INLINE
Definition color.h:89
static const Color WHITE
Definition color.h:193
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:70
uint8_t r
Definition color.h:30
constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Definition color.h:61
static const Color BLACK
Definition color.h:192
bool operator==(const Color &rhs)
Definition color.h:72
uint8_t blue
Definition color.h:39
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
Definition color.h:134
uint16_t x
Definition tt21100.cpp:5