13inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) {
14 return (uint16_t(i) * (1 + uint16_t(scale))) / 256;
22inline static constexpr uint8_t esp_scale8_twice(uint8_t i, uint8_t scale1, uint8_t scale2) {
27static constexpr uint8_t COLOR_CHANNEL_R = 0;
28static constexpr uint8_t COLOR_CHANNEL_G = 1;
29static constexpr uint8_t COLOR_CHANNEL_B = 2;
30static constexpr uint8_t COLOR_CHANNEL_W = 3;
58 operator lv_color_t()
const {
return lv_color_make(this->
r, this->
g, this->
b); }
71 inline explicit constexpr Color(
uint32_t colorcode) ESPHOME_ALWAYS_INLINE :
r((colorcode >> 16) & 0xFF),
72 g((colorcode >> 8) & 0xFF),
73 b((colorcode >> 0) & 0xFF),
74 w((colorcode >> 24) & 0xFF) {}
76 inline bool is_on() const ESPHOME_ALWAYS_INLINE {
return this->
raw_32 != 0; }
82 return this->
raw_32 == colorcode;
88 return this->
raw_32 != colorcode;
90 inline uint8_t &
operator[](uint8_t
x) ESPHOME_ALWAYS_INLINE {
return this->
raw[
x]; }
91 inline const uint8_t &
operator[](uint8_t
x)
const ESPHOME_ALWAYS_INLINE {
return this->
raw[
x]; }
93 return Color(esp_scale8(this->
red, scale), esp_scale8(this->
green, scale), esp_scale8(this->
blue, scale),
94 esp_scale8(this->
white, scale));
100 this->
red = esp_scale8(this->
red, scale);
102 this->
blue = esp_scale8(this->
blue, scale);
107 return Color(esp_scale8(this->
red, scale.red), esp_scale8(this->
green, scale.green),
108 esp_scale8(this->
blue, scale.blue), esp_scale8(this->
white, scale.white));
111 this->
red = esp_scale8(this->
red, scale.red);
112 this->
green = esp_scale8(this->
green, scale.green);
113 this->
blue = esp_scale8(this->
blue, scale.blue);
114 this->
white = esp_scale8(this->
white, scale.white);
119 if (uint8_t(add.r + this->r) < this->r) {
122 ret.r = this->
r + add.r;
124 if (uint8_t(add.g + this->g) < this->g) {
127 ret.g = this->
g + add.g;
129 if (uint8_t(add.b + this->b) < this->b) {
132 ret.b = this->
b + add.b;
134 if (uint8_t(add.w + this->w) < this->w) {
137 ret.w = this->
w + add.w;
142 inline Color operator+(uint8_t add)
const ESPHOME_ALWAYS_INLINE {
return (*
this) +
Color(add, add, add, add); }
143 inline Color &
operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE {
return *
this = (*this) + add; }
146 if (subtract.r > this->r) {
149 ret.r = this->
r - subtract.r;
151 if (subtract.g > this->g) {
154 ret.g = this->
g - subtract.g;
156 if (subtract.b > this->b) {
159 ret.b = this->
b - subtract.b;
161 if (subtract.w > this->w) {
164 ret.w = this->
w - subtract.w;
170 return (*
this) -
Color(subtract, subtract, subtract, subtract);
172 inline Color &
operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE {
return *
this = (*this) - subtract; }
175 uint8_t
w = rand >> 24;
176 uint8_t
r = rand >> 16;
177 uint8_t
g = rand >> 8;
178 uint8_t
b = rand >> 0;
179 const uint16_t max_rgb = std::max({
r,
g,
b, uint8_t(1)});
180 return Color(uint8_t((uint16_t(
r) * 255U / max_rgb)), uint8_t((uint16_t(
g) * 255U / max_rgb)),
181 uint8_t((uint16_t(
b) * 255U / max_rgb)),
w);
187 static inline uint8_t
blend_channel(uint8_t from, uint8_t to, uint8_t amnt) ESPHOME_ALWAYS_INLINE {
188 return (uint16_t(from) * (255 - amnt) + uint16_t(to) * amnt) / 255;
201static_assert(offsetof(
Color,
raw) == COLOR_CHANNEL_R);
202static_assert(offsetof(
Color, r) == COLOR_CHANNEL_R);
203static_assert(offsetof(
Color, g) == COLOR_CHANNEL_G);
204static_assert(offsetof(
Color, b) == COLOR_CHANNEL_B);
205static_assert(offsetof(
Color, w) == COLOR_CHANNEL_W);
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
bool operator!=(uint32_t colorcode) const
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
bool is_on() const ESPHOME_ALWAYS_INLINE
Color lighten(uint8_t delta)
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
static Color random_color()
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
bool operator==(const Color &rhs) const
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Color gradient(const Color &to_color, uint8_t amnt) const
static uint8_t blend_channel(uint8_t from, uint8_t to, uint8_t amnt) ESPHOME_ALWAYS_INLINE
One channel of gradient(): from at amnt 0 to to at amnt 255.
bool operator==(uint32_t colorcode) const
Color darken(uint8_t delta)
Color fade_to_white(uint8_t amnt) const
Color fade_to_black(uint8_t amnt) const
bool operator!=(const Color &rhs) const
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
constexpr Color() ESPHOME_ALWAYS_INLINE
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
Color operator~() const ESPHOME_ALWAYS_INLINE
constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
const uint8_t & operator[](uint8_t x) const ESPHOME_ALWAYS_INLINE