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