ESPHome  2024.4.1
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() ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT
35  inline Color(uint8_t red, uint8_t green, uint8_t blue) 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) ALWAYS_INLINE : r(red),
38  g(green),
39  b(blue),
40  w(white) {}
41  inline explicit Color(uint32_t colorcode) 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() 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) ALWAYS_INLINE { return this->raw[x]; }
61  inline Color operator*(uint8_t scale) const 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*=(uint8_t scale) ALWAYS_INLINE {
66  this->red = esp_scale8(this->red, scale);
67  this->green = esp_scale8(this->green, scale);
68  this->blue = esp_scale8(this->blue, scale);
69  this->white = esp_scale8(this->white, scale);
70  return *this;
71  }
72  inline Color operator*(const Color &scale) const ALWAYS_INLINE {
73  return Color(esp_scale8(this->red, scale.red), esp_scale8(this->green, scale.green),
74  esp_scale8(this->blue, scale.blue), esp_scale8(this->white, scale.white));
75  }
76  inline Color &operator*=(const Color &scale) ALWAYS_INLINE {
77  this->red = esp_scale8(this->red, scale.red);
78  this->green = esp_scale8(this->green, scale.green);
79  this->blue = esp_scale8(this->blue, scale.blue);
80  this->white = esp_scale8(this->white, scale.white);
81  return *this;
82  }
83  inline Color operator+(const Color &add) const ALWAYS_INLINE {
84  Color ret;
85  if (uint8_t(add.r + this->r) < this->r)
86  ret.r = 255;
87  else
88  ret.r = this->r + add.r;
89  if (uint8_t(add.g + this->g) < this->g)
90  ret.g = 255;
91  else
92  ret.g = this->g + add.g;
93  if (uint8_t(add.b + this->b) < this->b)
94  ret.b = 255;
95  else
96  ret.b = this->b + add.b;
97  if (uint8_t(add.w + this->w) < this->w)
98  ret.w = 255;
99  else
100  ret.w = this->w + add.w;
101  return ret;
102  }
103  inline Color &operator+=(const Color &add) ALWAYS_INLINE { return *this = (*this) + add; }
104  inline Color operator+(uint8_t add) const ALWAYS_INLINE { return (*this) + Color(add, add, add, add); }
105  inline Color &operator+=(uint8_t add) ALWAYS_INLINE { return *this = (*this) + add; }
106  inline Color operator-(const Color &subtract) const ALWAYS_INLINE {
107  Color ret;
108  if (subtract.r > this->r)
109  ret.r = 0;
110  else
111  ret.r = this->r - subtract.r;
112  if (subtract.g > this->g)
113  ret.g = 0;
114  else
115  ret.g = this->g - subtract.g;
116  if (subtract.b > this->b)
117  ret.b = 0;
118  else
119  ret.b = this->b - subtract.b;
120  if (subtract.w > this->w)
121  ret.w = 0;
122  else
123  ret.w = this->w - subtract.w;
124  return ret;
125  }
126  inline Color &operator-=(const Color &subtract) ALWAYS_INLINE { return *this = (*this) - subtract; }
127  inline Color operator-(uint8_t subtract) const ALWAYS_INLINE {
128  return (*this) - Color(subtract, subtract, subtract, subtract);
129  }
130  inline Color &operator-=(uint8_t subtract) ALWAYS_INLINE { return *this = (*this) - subtract; }
131  static Color random_color() {
132  uint32_t rand = random_uint32();
133  uint8_t w = rand >> 24;
134  uint8_t r = rand >> 16;
135  uint8_t g = rand >> 8;
136  uint8_t b = rand >> 0;
137  const uint16_t max_rgb = std::max(r, std::max(g, b));
138  return Color(uint8_t((uint16_t(r) * 255U / max_rgb)), uint8_t((uint16_t(g) * 255U / max_rgb)),
139  uint8_t((uint16_t(b) * 255U / max_rgb)), w);
140  }
141 
142  Color gradient(const Color &to_color, uint8_t amnt) {
143  Color new_color;
144  float amnt_f = float(amnt) / 255.0f;
145  new_color.r = amnt_f * (to_color.r - (*this).r) + (*this).r;
146  new_color.g = amnt_f * (to_color.g - (*this).g) + (*this).g;
147  new_color.b = amnt_f * (to_color.b - (*this).b) + (*this).b;
148  new_color.w = amnt_f * (to_color.w - (*this).w) + (*this).w;
149  return new_color;
150  }
151  Color fade_to_white(uint8_t amnt) { return (*this).gradient(Color::WHITE, amnt); }
152  Color fade_to_black(uint8_t amnt) { return (*this).gradient(Color::BLACK, amnt); }
153 
154  Color lighten(uint8_t delta) { return *this + delta; }
155  Color darken(uint8_t delta) { return *this - delta; }
156 
157  static const Color BLACK;
158  static const Color WHITE;
159 };
160 
161 ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21")
162 extern const Color COLOR_BLACK;
163 ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21")
164 extern const Color COLOR_WHITE;
165 
166 } // namespace esphome
Color operator-(const Color &subtract) const ALWAYS_INLINE
Definition: color.h:106
const Color COLOR_BLACK(0, 0, 0, 0)
Color(uint32_t colorcode) ALWAYS_INLINE
Definition: color.h:41
uint8_t white
Definition: color.h:27
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition: helpers.cpp:193
Color & operator-=(uint8_t subtract) ALWAYS_INLINE
Definition: color.h:130
static const Color WHITE
Definition: color.h:158
uint16_t x
Definition: tt21100.cpp:17
Color fade_to_black(uint8_t amnt)
Definition: color.h:152
Color & operator+=(uint8_t add) ALWAYS_INLINE
Definition: color.h:105
Color fade_to_white(uint8_t amnt)
Definition: color.h:151
Color(uint8_t red, uint8_t green, uint8_t blue) ALWAYS_INLINE
Definition: color.h:35
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ALWAYS_INLINE
Definition: color.h:37
uint8_t raw[4]
Definition: color.h:30
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 lighten(uint8_t delta)
Definition: color.h:154
bool operator!=(const Color &rhs)
Definition: color.h:54
static Color random_color()
Definition: color.h:131
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
Color & operator*=(uint8_t scale) ALWAYS_INLINE
Definition: color.h:65
Color operator+(const Color &add) const ALWAYS_INLINE
Definition: color.h:83
Color operator+(uint8_t add) const ALWAYS_INLINE
Definition: color.h:104
Color & operator+=(const Color &add) ALWAYS_INLINE
Definition: color.h:103
const Color COLOR_WHITE(255, 255, 255, 255)
uint8_t green
Definition: color.h:19
Color operator*(const Color &scale) const ALWAYS_INLINE
Definition: color.h:72
Color operator-(uint8_t subtract) const ALWAYS_INLINE
Definition: color.h:127
Color gradient(const Color &to_color, uint8_t amnt)
Definition: color.h:142
Color darken(uint8_t delta)
Definition: color.h:155
Color() ALWAYS_INLINE
Definition: color.h:34
uint8_t w
Definition: color.h:26
uint8_t & operator[](uint8_t x) ALWAYS_INLINE
Definition: color.h:60
Color operator*(uint8_t scale) const ALWAYS_INLINE
Definition: color.h:61
uint8_t blue
Definition: color.h:23
uint8_t b
Definition: color.h:22
Color & operator-=(const Color &subtract) ALWAYS_INLINE
Definition: color.h:126
static const Color BLACK
Definition: color.h:157
Color & operator*=(const Color &scale) ALWAYS_INLINE
Definition: color.h:76
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
bool is_on() ALWAYS_INLINE
Definition: color.h:46
uint32_t raw_32
Definition: color.h:31
uint8_t red
Definition: color.h:15