ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
light_color_values.h
Go to the documentation of this file.
1#pragma once
2
4#include "color_mode.h"
5#include <cmath>
6
7namespace esphome {
8namespace light {
9
10inline static uint8_t to_uint8_scale(float x) { return static_cast<uint8_t>(roundf(x * 255.0f)); }
11
46 public:
49 : state_(0.0f),
50 brightness_(1.0f),
52 red_(1.0f),
53 green_(1.0f),
54 blue_(1.0f),
55 white_(1.0f),
57 cold_white_{1.0f},
58 warm_white_{1.0f},
60
61 LightColorValues(ColorMode color_mode, float state, float brightness, float color_brightness, float red, float green,
62 float blue, float white, float color_temperature, float cold_white, float warm_white) {
63 this->set_color_mode(color_mode);
64 this->set_state(state);
65 this->set_brightness(brightness);
66 this->set_color_brightness(color_brightness);
67 this->set_red(red);
68 this->set_green(green);
69 this->set_blue(blue);
70 this->set_white(white);
71 this->set_color_temperature(color_temperature);
72 this->set_cold_white(cold_white);
73 this->set_warm_white(warm_white);
74 }
75
86 static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion) {
87 // Directly interpolate the raw values to avoid getter/setter overhead.
88 // This is safe because:
89 // - All LightColorValues have their values clamped when set via the setters
90 // - std::lerp guarantees output is in the same range as inputs
91 // - Therefore the output doesn't need clamping, so we can skip the setters
93 v.color_mode_ = end.color_mode_;
94 v.state_ = std::lerp(start.state_, end.state_, completion);
95 v.brightness_ = std::lerp(start.brightness_, end.brightness_, completion);
96 v.color_brightness_ = std::lerp(start.color_brightness_, end.color_brightness_, completion);
97 v.red_ = std::lerp(start.red_, end.red_, completion);
98 v.green_ = std::lerp(start.green_, end.green_, completion);
99 v.blue_ = std::lerp(start.blue_, end.blue_, completion);
100 v.white_ = std::lerp(start.white_, end.white_, completion);
101 v.color_temperature_ = std::lerp(start.color_temperature_, end.color_temperature_, completion);
102 v.cold_white_ = std::lerp(start.cold_white_, end.cold_white_, completion);
103 v.warm_white_ = std::lerp(start.warm_white_, end.warm_white_, completion);
104 return v;
105 }
106
117 if (this->color_mode_ & ColorCapability::RGB) {
118 float max_value = fmaxf(this->get_red(), fmaxf(this->get_green(), this->get_blue()));
119 if (max_value == 0.0f) {
120 this->set_red(1.0f);
121 this->set_green(1.0f);
122 this->set_blue(1.0f);
123 } else {
124 this->set_red(this->get_red() / max_value);
125 this->set_green(this->get_green() / max_value);
126 this->set_blue(this->get_blue() / max_value);
127 }
128 }
129 }
130
131 // Note that method signature of as_* methods is kept as-is for compatibility reasons, so not all parameters
132 // are always used or necessary. Methods will be deprecated later.
133
135 void as_binary(bool *binary) const { *binary = this->state_ == 1.0f; }
136
138 void as_brightness(float *brightness, float gamma = 0) const {
139 *brightness = gamma_correct(this->state_ * this->brightness_, gamma);
140 }
141
143 void as_rgb(float *red, float *green, float *blue, float gamma = 0, bool color_interlock = false) const {
144 if (this->color_mode_ & ColorCapability::RGB) {
145 float brightness = this->state_ * this->brightness_ * this->color_brightness_;
146 *red = gamma_correct(brightness * this->red_, gamma);
147 *green = gamma_correct(brightness * this->green_, gamma);
148 *blue = gamma_correct(brightness * this->blue_, gamma);
149 } else {
150 *red = *green = *blue = 0;
151 }
152 }
153
155 void as_rgbw(float *red, float *green, float *blue, float *white, float gamma = 0,
156 bool color_interlock = false) const {
157 this->as_rgb(red, green, blue, gamma);
159 *white = gamma_correct(this->state_ * this->brightness_ * this->white_, gamma);
160 } else {
161 *white = 0;
162 }
163 }
164
166 void as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, float gamma = 0,
167 bool constant_brightness = false) const {
168 this->as_rgb(red, green, blue, gamma);
169 this->as_cwww(cold_white, warm_white, gamma, constant_brightness);
170 }
171
173 void as_rgbct(float color_temperature_cw, float color_temperature_ww, float *red, float *green, float *blue,
174 float *color_temperature, float *white_brightness, float gamma = 0) const {
175 this->as_rgb(red, green, blue, gamma);
176 this->as_ct(color_temperature_cw, color_temperature_ww, color_temperature, white_brightness, gamma);
177 }
178
180 void as_cwww(float *cold_white, float *warm_white, float gamma = 0, bool constant_brightness = false) const {
182 const float cw_level = gamma_correct(this->cold_white_, gamma);
183 const float ww_level = gamma_correct(this->warm_white_, gamma);
184 const float white_level = gamma_correct(this->state_ * this->brightness_, gamma);
185 if (!constant_brightness) {
186 *cold_white = white_level * cw_level;
187 *warm_white = white_level * ww_level;
188 } else {
189 // Just multiplying by cw_level / (cw_level + ww_level) would divide out the brightness information from the
190 // cold_white and warm_white settings (i.e. cw=0.8, ww=0.4 would be identical to cw=0.4, ww=0.2), which breaks
191 // transitions. Use the highest value as the brightness for the white channels (the alternative, using cw+ww/2,
192 // reduces to cw/2 and ww/2, which would still limit brightness to 100% of a single channel, but isn't very
193 // useful in all other aspects -- that behaviour can also be achieved by limiting the output power).
194 const float sum = cw_level > 0 || ww_level > 0 ? cw_level + ww_level : 1; // Don't divide by zero.
195 *cold_white = white_level * std::max(cw_level, ww_level) * cw_level / sum;
196 *warm_white = white_level * std::max(cw_level, ww_level) * ww_level / sum;
197 }
198 } else {
199 *cold_white = *warm_white = 0;
200 }
201 }
202
204 void as_ct(float color_temperature_cw, float color_temperature_ww, float *color_temperature, float *white_brightness,
205 float gamma = 0) const {
206 const float white_level = this->color_mode_ & ColorCapability::RGB ? this->white_ : 1;
208 *color_temperature =
209 (this->color_temperature_ - color_temperature_cw) / (color_temperature_ww - color_temperature_cw);
210 *white_brightness = gamma_correct(this->state_ * this->brightness_ * white_level, gamma);
211 } else { // Probably won't get here but put this here anyway.
212 *white_brightness = 0;
213 }
214 }
215
217 bool operator==(const LightColorValues &rhs) const {
218 return color_mode_ == rhs.color_mode_ && state_ == rhs.state_ && brightness_ == rhs.brightness_ &&
219 color_brightness_ == rhs.color_brightness_ && red_ == rhs.red_ && green_ == rhs.green_ &&
220 blue_ == rhs.blue_ && white_ == rhs.white_ && color_temperature_ == rhs.color_temperature_ &&
222 }
223 bool operator!=(const LightColorValues &rhs) const { return !(rhs == *this); }
224
226 ColorMode get_color_mode() const { return this->color_mode_; }
228 void set_color_mode(ColorMode color_mode) { this->color_mode_ = color_mode; }
229
231 float get_state() const { return this->state_; }
233 bool is_on() const { return this->get_state() != 0.0f; }
235 void set_state(float state) { this->state_ = clamp(state, 0.0f, 1.0f); }
237 void set_state(bool state) { this->state_ = state ? 1.0f : 0.0f; }
238
240 float get_brightness() const { return this->brightness_; }
242 void set_brightness(float brightness) { this->brightness_ = clamp(brightness, 0.0f, 1.0f); }
243
245 float get_color_brightness() const { return this->color_brightness_; }
247 void set_color_brightness(float brightness) { this->color_brightness_ = clamp(brightness, 0.0f, 1.0f); }
248
250 float get_red() const { return this->red_; }
252 void set_red(float red) { this->red_ = clamp(red, 0.0f, 1.0f); }
253
255 float get_green() const { return this->green_; }
257 void set_green(float green) { this->green_ = clamp(green, 0.0f, 1.0f); }
258
260 float get_blue() const { return this->blue_; }
262 void set_blue(float blue) { this->blue_ = clamp(blue, 0.0f, 1.0f); }
263
265 float get_white() const { return white_; }
267 void set_white(float white) { this->white_ = clamp(white, 0.0f, 1.0f); }
268
270 float get_color_temperature() const { return this->color_temperature_; }
272 void set_color_temperature(float color_temperature) { this->color_temperature_ = color_temperature; }
273
276 if (this->color_temperature_ <= 0) {
277 return this->color_temperature_;
278 }
279 return 1000000.0 / this->color_temperature_;
280 }
282 void set_color_temperature_kelvin(float color_temperature) {
283 if (color_temperature <= 0) {
284 return;
285 }
286 this->color_temperature_ = 1000000.0 / color_temperature;
287 }
288
290 float get_cold_white() const { return this->cold_white_; }
292 void set_cold_white(float cold_white) { this->cold_white_ = clamp(cold_white, 0.0f, 1.0f); }
293
295 float get_warm_white() const { return this->warm_white_; }
297 void set_warm_white(float warm_white) { this->warm_white_ = clamp(warm_white, 0.0f, 1.0f); }
298
299 protected:
300 float state_;
303 float red_;
304 float green_;
305 float blue_;
306 float white_;
311};
312
313} // namespace light
314} // namespace esphome
This class represents the color state for a light object.
float get_state() const
Get the state of these light color values. In range from 0.0 (off) to 1.0 (on)
void set_color_mode(ColorMode color_mode)
Set the color mode of these light color values.
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
void as_ct(float color_temperature_cw, float color_temperature_ww, float *color_temperature, float *white_brightness, float gamma=0) const
Convert these light color values to a CT+BR representation with the given parameters.
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
float state_
ON / OFF, float for transition.
float get_color_temperature() const
Get the color temperature property of these light color values in mired.
void as_rgb(float *red, float *green, float *blue, float gamma=0, bool color_interlock=false) const
Convert these light color values to an RGB representation and write them to red, green,...
bool operator!=(const LightColorValues &rhs) const
void set_state(bool state)
Set the state of these light color values as a binary true/false.
static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion)
Linearly interpolate between the values in start to the values in end.
void set_brightness(float brightness)
Set the brightness property of these light color values. In range 0.0 to 1.0.
float get_cold_white() const
Get the cold white property of these light color values. In range 0.0 to 1.0.
void set_blue(float blue)
Set the blue property of these light color values. In range 0.0 to 1.0.
void set_cold_white(float cold_white)
Set the cold white property of these light color values. In range 0.0 to 1.0.
void set_color_brightness(float brightness)
Set the color brightness property of these light color values. In range 0.0 to 1.0.
void set_color_temperature_kelvin(float color_temperature)
Set the color temperature property of these light color values in kelvin.
bool operator==(const LightColorValues &rhs) const
Compare this LightColorValues to rhs, return true if and only if all attributes match.
void set_warm_white(float warm_white)
Set the warm white property of these light color values. In range 0.0 to 1.0.
void as_cwww(float *cold_white, float *warm_white, float gamma=0, bool constant_brightness=false) const
Convert these light color values to an CWWW representation with the given parameters.
bool is_on() const
Get the binary true/false state of these light color values.
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
void set_state(float state)
Set the state of these light color values. In range from 0.0 (off) to 1.0 (on)
void set_color_temperature(float color_temperature)
Set the color temperature property of these light color values in mired.
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
LightColorValues()
Construct the LightColorValues with all attributes enabled, but state set to off.
void as_binary(bool *binary) const
Convert these light color values to a binary representation and write them to binary.
float color_temperature_
Color Temperature in Mired.
LightColorValues(ColorMode color_mode, float state, float brightness, float color_brightness, float red, float green, float blue, float white, float color_temperature, float cold_white, float warm_white)
void as_rgbw(float *red, float *green, float *blue, float *white, float gamma=0, bool color_interlock=false) const
Convert these light color values to an RGBW representation and write them to red, green,...
ColorMode get_color_mode() const
Get the color mode of these light color values.
void set_white(float white)
Set the white property of these light color values. In range 0.0 to 1.0.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
void set_green(float green)
Set the green property of these light color values. In range 0.0 to 1.0.
void set_red(float red)
Set the red property of these light color values. In range 0.0 to 1.0.
void normalize_color()
Normalize the color (RGB/W) component.
float get_color_brightness() const
Get the color brightness property of these light color values. In range 0.0 to 1.0.
void as_brightness(float *brightness, float gamma=0) const
Convert these light color values to a brightness-only representation and write them to brightness.
void as_rgbct(float color_temperature_cw, float color_temperature_ww, float *red, float *green, float *blue, float *color_temperature, float *white_brightness, float gamma=0) const
Convert these light color values to an RGB+CT+BR representation with the given parameters.
float get_color_temperature_kelvin() const
Get the color temperature property of these light color values in kelvin.
void as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, float gamma=0, bool constant_brightness=false) const
Convert these light color values to an RGBWW representation with the given parameters.
bool state
Definition fan.h:0
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:49
@ UNKNOWN
No color mode configured (cannot be a supported mode, only active when light is off).
@ RGB
Color can be controlled using RGB format (includes a brightness control for the color).
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ WHITE
Brightness of white channel can be controlled separately from other channels.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition helpers.cpp:482
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5