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