ESPHome  2024.5.2
color.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "component.h"
4 #include "helpers.h"
5 
6 namespace esphome {
7 
8 inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; }
9 
10 struct 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  if (uint8_t(add.g + this->g) < this->g)
93  ret.g = 255;
94  else
95  ret.g = this->g + add.g;
96  if (uint8_t(add.b + this->b) < this->b)
97  ret.b = 255;
98  else
99  ret.b = this->b + add.b;
100  if (uint8_t(add.w + this->w) < this->w)
101  ret.w = 255;
102  else
103  ret.w = this->w + add.w;
104  return ret;
105  }
106  inline Color &operator+=(const Color &add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
107  inline Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
108  inline Color &operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE { return *this = (*this) + add; }
109  inline Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE {
110  Color ret;
111  if (subtract.r > this->r)
112  ret.r = 0;
113  else
114  ret.r = this->r - subtract.r;
115  if (subtract.g > this->g)
116  ret.g = 0;
117  else
118  ret.g = this->g - subtract.g;
119  if (subtract.b > this->b)
120  ret.b = 0;
121  else
122  ret.b = this->b - subtract.b;
123  if (subtract.w > this->w)
124  ret.w = 0;
125  else
126  ret.w = this->w - subtract.w;
127  return ret;
128  }
129  inline Color &operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
130  inline Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE {
131  return (*this) - Color(subtract, subtract, subtract, subtract);
132  }
133  inline Color &operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE { return *this = (*this) - subtract; }
134  static Color random_color() {
135  uint32_t rand = random_uint32();
136  uint8_t w = rand >> 24;
137  uint8_t r = rand >> 16;
138  uint8_t g = rand >> 8;
139  uint8_t b = rand >> 0;
140  const uint16_t max_rgb = std::max(r, std::max(g, b));
141  return Color(uint8_t((uint16_t(r) * 255U / max_rgb)), uint8_t((uint16_t(g) * 255U / max_rgb)),
142  uint8_t((uint16_t(b) * 255U / max_rgb)), w);
143  }
144 
145  Color gradient(const Color &to_color, uint8_t amnt) {
146  Color new_color;
147  float amnt_f = float(amnt) / 255.0f;
148  new_color.r = amnt_f * (to_color.r - (*this).r) + (*this).r;
149  new_color.g = amnt_f * (to_color.g - (*this).g) + (*this).g;
150  new_color.b = amnt_f * (to_color.b - (*this).b) + (*this).b;
151  new_color.w = amnt_f * (to_color.w - (*this).w) + (*this).w;
152  return new_color;
153  }
154  Color fade_to_white(uint8_t amnt) { return (*this).gradient(Color::WHITE, amnt); }
155  Color fade_to_black(uint8_t amnt) { return (*this).gradient(Color::BLACK, amnt); }
156 
157  Color lighten(uint8_t delta) { return *this + delta; }
158  Color darken(uint8_t delta) { return *this - delta; }
159 
160  static const Color BLACK;
161  static const Color WHITE;
162 };
163 
164 ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21")
165 extern const Color COLOR_BLACK;
166 ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21")
167 extern const Color COLOR_WHITE;
168 
169 } // namespace esphome
Color() ESPHOME_ALWAYS_INLINE
Definition: color.h:34
const Color COLOR_BLACK(0, 0, 0, 0)
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE
Definition: color.h:37
uint8_t white
Definition: color.h:27
Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE
Definition: color.h:35
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition: helpers.cpp:193
static const Color WHITE
Definition: color.h:161
uint16_t x
Definition: tt21100.cpp:17
Color fade_to_black(uint8_t amnt)
Definition: color.h:155
Color fade_to_white(uint8_t amnt)
Definition: color.h:154
Color & operator-=(const Color &subtract) ESPHOME_ALWAYS_INLINE
Definition: color.h:129
Color operator-(uint8_t subtract) const ESPHOME_ALWAYS_INLINE
Definition: color.h:130
Color & operator+=(const Color &add) ESPHOME_ALWAYS_INLINE
Definition: color.h:106
uint8_t raw[4]
Definition: color.h:30
bool is_on() ESPHOME_ALWAYS_INLINE
Definition: color.h:46
uint8_t g
Definition: color.h:18
bool operator==(uint32_t colorcode)
Definition: color.h:51
bool operator!=(uint32_t colorcode)
Definition: color.h:57
bool operator==(const Color &rhs)
Definition: color.h:48
Color operator*(uint8_t scale) const ESPHOME_ALWAYS_INLINE
Definition: color.h:61
Color lighten(uint8_t delta)
Definition: color.h:157
bool operator!=(const Color &rhs)
Definition: color.h:54
Color & operator-=(uint8_t subtract) ESPHOME_ALWAYS_INLINE
Definition: color.h:133
static Color random_color()
Definition: color.h:134
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
Color operator-(const Color &subtract) const ESPHOME_ALWAYS_INLINE
Definition: color.h:109
Color operator*(const Color &scale) const ESPHOME_ALWAYS_INLINE
Definition: color.h:75
const Color COLOR_WHITE(255, 255, 255, 255)
Color & operator*=(uint8_t scale) ESPHOME_ALWAYS_INLINE
Definition: color.h:68
uint8_t green
Definition: color.h:19
Color operator+(const Color &add) const ESPHOME_ALWAYS_INLINE
Definition: color.h:86
Color gradient(const Color &to_color, uint8_t amnt)
Definition: color.h:145
Color & operator*=(const Color &scale) ESPHOME_ALWAYS_INLINE
Definition: color.h:79
uint8_t & operator[](uint8_t x) ESPHOME_ALWAYS_INLINE
Definition: color.h:60
Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE
Definition: color.h:41
Color operator+(uint8_t add) const ESPHOME_ALWAYS_INLINE
Definition: color.h:107
Color darken(uint8_t delta)
Definition: color.h:158
uint8_t w
Definition: color.h:26
uint8_t blue
Definition: color.h:23
uint8_t b
Definition: color.h:22
Color & operator+=(uint8_t add) ESPHOME_ALWAYS_INLINE
Definition: color.h:108
static const Color BLACK
Definition: color.h:160
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
uint8_t r
Definition: color.h:14
Color operator~() const ESPHOME_ALWAYS_INLINE
Definition: color.h:65
uint32_t raw_32
Definition: color.h:31
uint8_t red
Definition: color.h:15