ESPHome 2025.5.0
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#pragma once
2
3#include "component.h"
4#include "helpers.h"
5
6namespace esphome {
7
8inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; }
9
10struct Color {
11 union {
12 struct {
13 union {
14 uint8_t r;
15 uint8_t red;
16 };
17 union {
18 uint8_t g;
19 uint8_t green;
20 };
21 union {
22 uint8_t b;
23 uint8_t blue;
24 };
25 union {
26 uint8_t w;
27 uint8_t white;
28 };
29 };
30 uint8_t raw[4];
31 uint32_t raw_32;
32 };
33
34 inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT
35 inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {}
36
37 inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
38 g(green),
39 b(blue),
40 w(white) {}
41 inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
42 g((colorcode >> 8) & 0xFF),
43 b((colorcode >> 0) & 0xFF),
44 w((colorcode >> 24) & 0xFF) {}
45
46 inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; }
47
48 inline bool operator==(const Color &rhs) { // NOLINT
49 return this->raw_32 == rhs.raw_32;
50 }
51 inline bool operator==(uint32_t colorcode) { // NOLINT
52 return this->raw_32 == colorcode;
53 }
54 inline bool operator!=(const Color &rhs) { // NOLINT
55 return this->raw_32 != rhs.raw_32;
56 }
57 inline bool operator!=(uint32_t colorcode) { // NOLINT
58 return this->raw_32 != colorcode;
59 }
60 inline uint8_t &operator[](uint8_t x) ESPHOME_ALWAYS_INLINE { return this->raw[x]; }
61 inline Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE {
62 return Color(esp_scale8(this->red, scale), esp_scale8(this->green, scale), esp_scale8(this->blue, scale),
63 esp_scale8(this->white, scale));
64 }
65 inline Color operator~() const ESPHOME_ALWAYS_INLINE {
66 return Color(255 - this->red, 255 - this->green, 255 - this->blue);
67 }
68 inline Color &operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE {
69 this->red = esp_scale8(this->red, scale);
70 this->green = esp_scale8(this->green, scale);
71 this->blue = esp_scale8(this->blue, scale);
72 this->white = esp_scale8(this->white, scale);
73 return *this;
74 }
75 inline Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE {
76 return Color(esp_scale8(this->red, scale.red), esp_scale8(this->green, scale.green),
77 esp_scale8(this->blue, scale.blue), esp_scale8(this->white, scale.white));
78 }
79 inline Color &operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE {
80 this->red = esp_scale8(this->red, scale.red);
81 this->green = esp_scale8(this->green, scale.green);
82 this->blue = esp_scale8(this->blue, scale.blue);
83 this->white = esp_scale8(this->white, scale.white);
84 return *this;
85 }
86 inline Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE {
87 Color ret;
88 if (uint8_t(add.r + this->r) < this->r) {
89 ret.r = 255;
90 } else {
91 ret.r = this->r + add.r;
92 }
93 if (uint8_t(add.g + this->g) < this->g) {
94 ret.g = 255;
95 } else {
96 ret.g = this->g + add.g;
97 }
98 if (uint8_t(add.b + this->b) < this->b) {
99 ret.b = 255;
100 } else {
101 ret.b = this->b + add.b;
102 }
103 if (uint8_t(add.w + this->w) < this->w) {
104 ret.w = 255;
105 } else {
106 ret.w = this->w + add.w;
107 }
108 return ret;
109 }
110 inline Color &operator+=(const Color &add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
111 inline Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
112 inline Color &operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
113 inline Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE {
114 Color ret;
115 if (subtract.r > this->r) {
116 ret.r = 0;
117 } else {
118 ret.r = this->r - subtract.r;
119 }
120 if (subtract.g > this->g) {
121 ret.g = 0;
122 } else {
123 ret.g = this->g - subtract.g;
124 }
125 if (subtract.b > this->b) {
126 ret.b = 0;
127 } else {
128 ret.b = this->b - subtract.b;
129 }
130 if (subtract.w > this->w) {
131 ret.w = 0;
132 } else {
133 ret.w = this->w - subtract.w;
134 }
135 return ret;
136 }
137 inline Color &operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
138 inline Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE {
139 return (*this) - Color(subtract, subtract, subtract, subtract);
140 }
141 inline Color &operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
143 uint32_t rand = random_uint32();
144 uint8_t w = rand >> 24;
145 uint8_t r = rand >> 16;
146 uint8_t g = rand >> 8;
147 uint8_t b = rand >> 0;
148 const uint16_t max_rgb = std::max(r, std::max(g, b));
149 return Color(uint8_t((uint16_t(r) * 255U / max_rgb)), uint8_t((uint16_t(g) * 255U / max_rgb)),
150 uint8_t((uint16_t(b) * 255U / max_rgb)), w);
151 }
152
153 Color gradient(const Color &to_color, uint8_t amnt) {
154 Color new_color;
155 float amnt_f = float(amnt) / 255.0f;
156 new_color.r = amnt_f * (to_color.r - (*this).r) + (*this).r;
157 new_color.g = amnt_f * (to_color.g - (*this).g) + (*this).g;
158 new_color.b = amnt_f * (to_color.b - (*this).b) + (*this).b;
159 new_color.w = amnt_f * (to_color.w - (*this).w) + (*this).w;
160 return new_color;
161 }
162 Color fade_to_white(uint8_t amnt) { return (*this).gradient(Color::WHITE, amnt); }
163 Color fade_to_black(uint8_t amnt) { return (*this).gradient(Color::BLACK, amnt); }
164
165 Color lighten(uint8_t delta) { return *this + delta; }
166 Color darken(uint8_t delta) { return *this - delta; }
167
168 static const Color BLACK;
169 static const Color WHITE;
170};
171
172ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21")
173extern const Color COLOR_BLACK;
174ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21")
175extern const Color COLOR_WHITE;
176
177} // namespace esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const Color COLOR_WHITE(255, 255, 255, 255)
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
const Color COLOR_BLACK(0, 0, 0, 0)
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:196
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Definition color.h:111
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
Definition color.h:112
bool operator!=(const Color &rhs)
Definition color.h:54
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:113
static const Color WHITE
Definition color.h:169
Color fade_to_white(uint8_t amnt)
Definition color.h:162
Color lighten(uint8_t delta)
Definition color.h:165
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:61
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
Definition color.h:68
static Color random_color()
Definition color.h:142
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Definition color.h:37
uint8_t red
Definition color.h:15
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:141
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
Definition color.h:75
uint8_t w
Definition color.h:26
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
Definition color.h:86
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
Definition color.h:138
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Definition color.h:60
bool operator!=(uint32_t colorcode)
Definition color.h:57
bool operator==(uint32_t colorcode)
Definition color.h:51
static const Color BLACK
Definition color.h:168
uint8_t raw[4]
Definition color.h:30
uint8_t g
Definition color.h:18
Color darken(uint8_t delta)
Definition color.h:166
uint8_t white
Definition color.h:27
Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
Definition color.h:35
uint8_t green
Definition color.h:19
Color fade_to_black(uint8_t amnt)
Definition color.h:163
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
Definition color.h:137
Color gradient(const Color &to_color, uint8_t amnt)
Definition color.h:153
uint8_t b
Definition color.h:22
uint32_t raw_32
Definition color.h:31
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
Definition color.h:79
Color operator~() const ESPHOME_ALWAYS_INLINE
Definition color.h:65
Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
Definition color.h:41
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:46
uint8_t r
Definition color.h:14
Color() ESPHOME_ALWAYS_INLINE
Definition color.h:34
bool operator==(const Color &rhs)
Definition color.h:48
uint8_t blue
Definition color.h:23
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
Definition color.h:110
uint16_t x
Definition tt21100.cpp:5