8 inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) {
return (uint16_t(i) * (1 + uint16_t(scale))) / 256; }
34 inline Color() ESPHOME_ALWAYS_INLINE :
r(0),
g(0),
b(0),
w(0) {}
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) {}
46 inline bool is_on() ESPHOME_ALWAYS_INLINE {
return this->
raw_32 != 0; }
52 return this->
raw_32 == colorcode;
58 return this->
raw_32 != colorcode;
60 inline uint8_t &
operator[](uint8_t
x) ESPHOME_ALWAYS_INLINE {
return this->
raw[
x]; }
62 return Color(esp_scale8(this->
red, scale), esp_scale8(this->
green, scale), esp_scale8(this->
blue, scale),
63 esp_scale8(this->
white, scale));
69 this->
red = esp_scale8(this->
red, scale);
71 this->
blue = esp_scale8(this->
blue, scale);
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));
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);
88 if (uint8_t(add.r + this->r) < this->r) {
91 ret.
r = this->
r + add.r;
93 if (uint8_t(add.g + this->g) < this->g) {
96 ret.
g = this->
g + add.g;
98 if (uint8_t(add.b + this->b) < this->b) {
101 ret.
b = this->
b + add.b;
103 if (uint8_t(add.w + this->w) < this->w) {
106 ret.
w = this->
w + add.w;
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; }
115 if (subtract.r > this->r) {
118 ret.
r = this->
r - subtract.r;
120 if (subtract.g > this->g) {
123 ret.
g = this->
g - subtract.g;
125 if (subtract.b > this->b) {
128 ret.
b = this->
b - subtract.b;
130 if (subtract.w > this->w) {
133 ret.
w = this->
w - subtract.w;
139 return (*
this) -
Color(subtract, subtract, subtract, subtract);
141 inline Color &
operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE {
return *
this = (*this) - subtract; }
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);
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;
172 ESPDEPRECATED(
"Use Color::BLACK instead of COLOR_BLACK",
"v1.21")
175 extern const
Color COLOR_WHITE;
Color() ESPHOME_ALWAYS_INLINE
const Color COLOR_BLACK(0, 0, 0, 0)
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Color fade_to_black(uint8_t amnt)
Color fade_to_white(uint8_t amnt)
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
bool is_on() ESPHOME_ALWAYS_INLINE
bool operator==(uint32_t colorcode)
bool operator!=(uint32_t colorcode)
bool operator==(const Color &rhs)
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Color lighten(uint8_t delta)
bool operator!=(const Color &rhs)
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
static Color random_color()
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
const Color COLOR_WHITE(255, 255, 255, 255)
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
Color gradient(const Color &to_color, uint8_t amnt)
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Color darken(uint8_t delta)
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
Implementation of SPI Controller mode.
Color operator~() const ESPHOME_ALWAYS_INLINE