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