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