Commit b4e3087beb75cd022c31f9ba23ad4177b5b83e00

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 74b51fb2

Handle black color conversion and fix transaction with brightness 0 flickering.

src/ColorUnits.cpp
@@ -107,6 +107,10 @@ XY ColorGamut::corrected(const XY& xy) const @@ -107,6 +107,10 @@ XY ColorGamut::corrected(const XY& xy) const
107 107
108 XYBrightness RGB::toXY() const 108 XYBrightness RGB::toXY() const
109 { 109 {
  110 + if (r == 0 && g == 0 && b == 0)
  111 + {
  112 + return XYBrightness {XY {0.f, 0.f}, 0.f};
  113 + }
110 const float red = r / 255.f; 114 const float red = r / 255.f;
111 const float green = g / 255.f; 115 const float green = g / 255.f;
112 const float blue = b / 255.f; 116 const float blue = b / 255.f;
src/StateTransaction.cpp
@@ -39,8 +39,8 @@ bool StateTransaction::commit(bool trimRequest) @@ -39,8 +39,8 @@ bool StateTransaction::commit(bool trimRequest)
39 // Check this before request is trimmed 39 // Check this before request is trimmed
40 if (!request.count("on")) 40 if (!request.count("on"))
41 { 41 {
42 - if (!stateJson.value("on", false)  
43 - && (request.value("bri", 0) != 0 || request.count("effect") || request.count("hue") || request.count("sat") 42 + if (!stateJson.value("on", false) && request.value("bri", 254) != 0
  43 + && (request.count("bri") || request.count("effect") || request.count("hue") || request.count("sat")
44 || request.count("xy") || request.count("ct"))) 44 || request.count("xy") || request.count("ct")))
45 { 45 {
46 // Turn on if it was turned off 46 // Turn on if it was turned off
test/test_ColorUnits.cpp
@@ -105,6 +105,13 @@ TEST(RGB, toXY) @@ -105,6 +105,13 @@ TEST(RGB, toXY)
105 EXPECT_FLOAT_EQ(xy.xy.y, 0.32902291f); 105 EXPECT_FLOAT_EQ(xy.xy.y, 0.32902291f);
106 EXPECT_FLOAT_EQ(xy.brightness, 0.99999905f); 106 EXPECT_FLOAT_EQ(xy.brightness, 0.99999905f);
107 } 107 }
  108 + {
  109 + const RGB black{ 0,0,0 };
  110 + XYBrightness xy = black.toXY(gamut::maxGamut);
  111 + EXPECT_FLOAT_EQ(xy.xy.x, 0.0f);
  112 + EXPECT_FLOAT_EQ(xy.xy.y, 0.0f);
  113 + EXPECT_FLOAT_EQ(xy.brightness, 0.0f);
  114 + }
108 } 115 }
109 116
110 TEST(RGB, fromXY) 117 TEST(RGB, fromXY)
test/test_StateTransaction.cpp
@@ -154,6 +154,16 @@ TEST(StateTransaction, setBrightness) @@ -154,6 +154,16 @@ TEST(StateTransaction, setBrightness)
154 EXPECT_TRUE(StateTransaction(commands, "/path", &state).setBrightness(bri).commit()); 154 EXPECT_TRUE(StateTransaction(commands, "/path", &state).setBrightness(bri).commit());
155 Mock::VerifyAndClearExpectations(handler.get()); 155 Mock::VerifyAndClearExpectations(handler.get());
156 } 156 }
  157 + // Fixed unexpected on/off flickering
  158 + {
  159 + const int hue = 32;
  160 + nlohmann::json state = {{"on", false}, {"bri", 20}};
  161 + nlohmann::json request = { {"hue", hue}, {"bri", 0} };
  162 + nlohmann::json response = {{{"success", {{"/path/hue", hue}}}}};
  163 + EXPECT_CALL(*handler, PUTJson(requestPath, request, getBridgeIp(), getBridgePort())).WillOnce(Return(response));
  164 + EXPECT_TRUE(StateTransaction(commands, "/path", &state).setBrightness(0).setColorHue(hue).commit());
  165 + Mock::VerifyAndClearExpectations(handler.get());
  166 + }
157 } 167 }
158 168
159 TEST(StateTransaction, setColorHue) 169 TEST(StateTransaction, setColorHue)
@@ -303,7 +313,7 @@ TEST(StateTransaction, setColorXY) @@ -303,7 +313,7 @@ TEST(StateTransaction, setColorXY)
303 }}, 313 }},
304 {"colormode", "hs"}}; 314 {"colormode", "hs"}};
305 EXPECT_CALL(*handler, PUTJson(requestPath, request, getBridgeIp(), getBridgePort())).WillOnce(Return(response)); 315 EXPECT_CALL(*handler, PUTJson(requestPath, request, getBridgeIp(), getBridgePort())).WillOnce(Return(response));
306 - EXPECT_TRUE(StateTransaction(commands, "/path", &state).setColor(XY{ x, y }).commit()); 316 + EXPECT_TRUE(StateTransaction(commands, "/path", &state).setColor(XY {x, y}).commit());
307 Mock::VerifyAndClearExpectations(handler.get()); 317 Mock::VerifyAndClearExpectations(handler.get());
308 } 318 }
309 // No request 319 // No request
@@ -318,7 +328,7 @@ TEST(StateTransaction, setColorXY) @@ -318,7 +328,7 @@ TEST(StateTransaction, setColorXY)
318 }}, 328 }},
319 {"colormode", "xy"}}; 329 {"colormode", "xy"}};
320 EXPECT_CALL(*handler, PUTJson(_, _, getBridgeIp(), getBridgePort())).Times(0); 330 EXPECT_CALL(*handler, PUTJson(_, _, getBridgeIp(), getBridgePort())).Times(0);
321 - EXPECT_TRUE(StateTransaction(commands, "/path", &state).setColor(XY{ x, y }).commit()); 331 + EXPECT_TRUE(StateTransaction(commands, "/path", &state).setColor(XY {x, y}).commit());
322 Mock::VerifyAndClearExpectations(handler.get()); 332 Mock::VerifyAndClearExpectations(handler.get());
323 } 333 }
324 } 334 }