Color brightness fixes (#2008)

This commit is contained in:
Oxan van Leeuwen 2021-07-13 02:28:29 +02:00 committed by GitHub
parent 7dd16df846
commit fb8ec79a52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 12 deletions

View file

@ -32,19 +32,26 @@ LightCall &LightCall::parse_color_json(JsonObject &root) {
if (root.containsKey("color")) {
JsonObject &color = root["color"];
// HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
float max_rgb = 0.0f;
if (color.containsKey("r")) {
this->set_red(float(color["r"]) / 255.0f);
float r = float(color["r"]) / 255.0f;
max_rgb = fmaxf(max_rgb, r);
this->set_red(r);
}
if (color.containsKey("g")) {
this->set_green(float(color["g"]) / 255.0f);
float g = float(color["g"]) / 255.0f;
max_rgb = fmaxf(max_rgb, g);
this->set_green(g);
}
if (color.containsKey("b")) {
this->set_blue(float(color["b"]) / 255.0f);
float b = float(color["b"]) / 255.0f;
max_rgb = fmaxf(max_rgb, b);
this->set_blue(b);
}
if (color.containsKey("r") || color.containsKey("g") || color.containsKey("b")) {
this->set_color_brightness(max_rgb);
}
}
if (root.containsKey("color_brightness")) {
this->set_color_brightness(float(root["color_brightness"]) / 255.0f);
}
if (root.containsKey("white_value")) {
@ -418,7 +425,7 @@ LightCall &LightCall::set_brightness_if_supported(float brightness) {
}
LightCall &LightCall::set_color_brightness_if_supported(float brightness) {
if (this->parent_->get_traits().get_supports_rgb_white_value())
this->set_brightness(brightness);
this->set_color_brightness(brightness);
return *this;
}
LightCall &LightCall::set_red_if_supported(float red) {

View file

@ -135,12 +135,11 @@ class LightColorValues {
root["brightness"] = uint8_t(this->get_brightness() * 255);
if (traits.get_supports_rgb()) {
JsonObject &color = root.createNestedObject("color");
color["r"] = uint8_t(this->get_red() * 255);
color["g"] = uint8_t(this->get_green() * 255);
color["b"] = uint8_t(this->get_blue() * 255);
color["r"] = uint8_t(this->get_color_brightness() * this->get_red() * 255);
color["g"] = uint8_t(this->get_color_brightness() * this->get_green() * 255);
color["b"] = uint8_t(this->get_color_brightness() * this->get_blue() * 255);
}
if (traits.get_supports_rgb_white_value()) {
root["color_brightness"] = uint8_t(this->get_color_brightness() * 255);
root["white_value"] = uint8_t(this->get_white() * 255);
}
if (traits.get_supports_color_temperature())

View file

@ -18,6 +18,7 @@ LightCall LightState::make_call() { return LightCall(this); }
struct LightStateRTCState {
bool state{false};
float brightness{1.0f};
float color_brightness{1.0f};
float red{1.0f};
float green{1.0f};
float blue{1.0f};
@ -65,6 +66,7 @@ void LightState::setup() {
call.set_state(recovered.state);
call.set_brightness_if_supported(recovered.brightness);
call.set_color_brightness_if_supported(recovered.color_brightness);
call.set_red_if_supported(recovered.red);
call.set_green_if_supported(recovered.green);
call.set_blue_if_supported(recovered.blue);
@ -244,6 +246,7 @@ void LightState::save_remote_values_() {
LightStateRTCState saved;
saved.state = this->remote_values.is_on();
saved.brightness = this->remote_values.get_brightness();
saved.color_brightness = this->remote_values.get_color_brightness();
saved.red = this->remote_values.get_red();
saved.green = this->remote_values.get_green();
saved.blue = this->remote_values.get_blue();