Commit fd4a6765e6b708c62e7b8bf20082e27d251dc1f6

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 12912173

Rename Units to ColorUnits, use XYBrightness for color.

include/hueplusplus/Units.h renamed to include/hueplusplus/ColorUnits.h
1 /** 1 /**
2 - \file Units.h 2 + \file ColorUnits.h
3 Copyright Notice\n 3 Copyright Notice\n
4 Copyright (C) 2017 Jan Rogall - developer\n 4 Copyright (C) 2017 Jan Rogall - developer\n
5 Copyright (C) 2017 Moritz Wirger - developer\n 5 Copyright (C) 2017 Moritz Wirger - developer\n
@@ -55,6 +55,14 @@ struct ColorGamut @@ -55,6 +55,14 @@ struct ColorGamut
55 XY corrected(const XY& xy) const; 55 XY corrected(const XY& xy) const;
56 }; 56 };
57 57
  58 +namespace gamut
  59 +{
  60 +constexpr ColorGamut gamutA {{0.704f, 0.296f}, {0.2151f, 0.7106f}, {0.138f, 0.08f}};
  61 +constexpr ColorGamut gamutB {{0.675f, 0.322f}, {0.409f, 0.518f}, {0.167f, 0.04f}};
  62 +constexpr ColorGamut gamutC {{0.692f, 0.308f}, {0.17f, 0.7f}, {0.153f, 0.048f}};
  63 +constexpr ColorGamut maxGamut {{1.f, 0.f}, {0.f, 1.f}, {0.f, 0.f}};
  64 +} // namespace gamut
  65 +
58 struct RGB 66 struct RGB
59 { 67 {
60 uint8_t r; 68 uint8_t r;
include/hueplusplus/HueLight.h
@@ -195,6 +195,14 @@ public: @@ -195,6 +195,14 @@ public:
195 //! \return String containing the software version 195 //! \return String containing the software version
196 virtual std::string getSwVersion() const; 196 virtual std::string getSwVersion() const;
197 197
  198 +
  199 + //! \brief Const function that returns the color type of the light.
  200 + //!
  201 + //! \return ColorType containig the color type of the light
  202 + virtual ColorType getColorType() const;
  203 +
  204 + ColorGamut getColorGamut() const;
  205 +
198 ///@} 206 ///@}
199 //! \name Light state 207 //! \name Light state
200 ///@{ 208 ///@{
@@ -234,11 +242,6 @@ public: @@ -234,11 +242,6 @@ public:
234 //! \return Bool that is true, when the light is on and false, when off 242 //! \return Bool that is true, when the light is on and false, when off
235 virtual bool isOn() const; 243 virtual bool isOn() const;
236 244
237 - //! \brief Const function that returns the color type of the light.  
238 - //!  
239 - //! \return ColorType containig the color type of the light  
240 - virtual ColorType getColorType() const;  
241 -  
242 //! \brief Const function to check whether this light has brightness control 245 //! \brief Const function to check whether this light has brightness control
243 //! 246 //!
244 //! \return Bool that is true when the light has specified abilities and false 247 //! \return Bool that is true when the light has specified abilities and false
include/hueplusplus/Scene.h
@@ -30,7 +30,7 @@ @@ -30,7 +30,7 @@
30 30
31 #include "APICache.h" 31 #include "APICache.h"
32 #include "TimePattern.h" 32 #include "TimePattern.h"
33 -#include "Units.h" 33 +#include "ColorUnits.h"
34 34
35 namespace hueplusplus 35 namespace hueplusplus
36 { 36 {
@@ -49,7 +49,7 @@ public: @@ -49,7 +49,7 @@ public:
49 HueSaturation getHueSat() const; 49 HueSaturation getHueSat() const;
50 50
51 bool hasXY() const; 51 bool hasXY() const;
52 - XY getXY() const; 52 + XYBrightness getXY() const;
53 53
54 bool hasCt() const; 54 bool hasCt() const;
55 int getCt() const; 55 int getCt() const;
include/hueplusplus/StateTransaction.h
@@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
25 25
26 #include <string> 26 #include <string>
27 27
  28 +#include "ColorUnits.h"
28 #include "HueCommandAPI.h" 29 #include "HueCommandAPI.h"
29 #include "Schedule.h" 30 #include "Schedule.h"
30 31
@@ -99,6 +100,8 @@ public: @@ -99,6 +100,8 @@ public:
99 //! \note If this transaction is for a light, the light needs to have rgb color control. 100 //! \note If this transaction is for a light, the light needs to have rgb color control.
100 //! \note Will also turn on the light if nothing else is specified 101 //! \note Will also turn on the light if nothing else is specified
101 StateTransaction&& setColorXY(float x, float y) &&; 102 StateTransaction&& setColorXY(float x, float y) &&;
  103 +
  104 + StateTransaction&& setColorXY(const XYBrightness& xy) &&;
102 //! \brief Set light color temperature. 105 //! \brief Set light color temperature.
103 //! \param mired Color temperature in mired from 153 to 500 106 //! \param mired Color temperature in mired from 153 to 500
104 //! \returns This transaction for chaining calls 107 //! \returns This transaction for chaining calls
src/CMakeLists.txt
@@ -16,7 +16,7 @@ set(hueplusplus_SOURCES @@ -16,7 +16,7 @@ set(hueplusplus_SOURCES
16 SimpleColorTemperatureStrategy.cpp 16 SimpleColorTemperatureStrategy.cpp
17 StateTransaction.cpp 17 StateTransaction.cpp
18 TimePattern.cpp 18 TimePattern.cpp
19 - Units.cpp 19 + "ColorUnits.cpp"
20 UPnP.cpp 20 UPnP.cpp
21 Utils.cpp ) 21 Utils.cpp )
22 22
src/Units.cpp renamed to src/ColorUnits.cpp
  1 +/**
  2 + \file ColorUnits.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
1 #include <cmath> 22 #include <cmath>
2 23
3 -#include <hueplusplus/Units.h> 24 +#include <hueplusplus/ColorUnits.h>
4 25
5 namespace hueplusplus 26 namespace hueplusplus
6 { 27 {
@@ -129,8 +150,8 @@ RGB RGB::fromXY(const XYBrightness&amp; xy) @@ -129,8 +150,8 @@ RGB RGB::fromXY(const XYBrightness&amp; xy)
129 const float gammaG = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f; 150 const float gammaG = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f;
130 const float gammaB = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f; 151 const float gammaB = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f;
131 152
132 - return RGB {static_cast<int>(std::round(gammaR * 255.f)), static_cast<int>(std::round(gammaG * 255.f)),  
133 - static_cast<int>(std::round(gammaB * 255.f))}; 153 + return RGB {static_cast<uint8_t>(std::round(gammaR * 255.f)), static_cast<uint8_t>(std::round(gammaG * 255.f)),
  154 + static_cast<uint8_t>(std::round(gammaB * 255.f))};
134 } 155 }
135 156
136 RGB RGB::fromXY(const XYBrightness& xy, const ColorGamut& gamut) 157 RGB RGB::fromXY(const XYBrightness& xy, const ColorGamut& gamut)
src/HueLight.cpp
@@ -124,6 +124,35 @@ ColorType HueLight::getColorType() const @@ -124,6 +124,35 @@ ColorType HueLight::getColorType() const
124 return colorType; 124 return colorType;
125 } 125 }
126 126
  127 +ColorGamut HueLight::getColorGamut() const
  128 +{
  129 + switch (colorType)
  130 + {
  131 + case ColorType::GAMUT_A:
  132 + case ColorType::GAMUT_A_TEMPERATURE:
  133 + return gamut::gamutA;
  134 + case ColorType::GAMUT_B:
  135 + case ColorType::GAMUT_B_TEMPERATURE:
  136 + return gamut::gamutB;
  137 + case ColorType::GAMUT_C:
  138 + case ColorType::GAMUT_C_TEMPERATURE:
  139 + return gamut::gamutC;
  140 + default: {
  141 + const nlohmann::json& capabilitiesGamut
  142 + = utils::safeGetMember(state.getValue(), "capabilities", "control", "colorgamut");
  143 + if (capabilitiesGamut.is_array() && capabilitiesGamut.size() == 3)
  144 + {
  145 + // Other gamut
  146 + return ColorGamut {{capabilitiesGamut[0].at(0), capabilitiesGamut[0].at(1)},
  147 + {capabilitiesGamut[1].at(0), capabilitiesGamut[1].at(1)},
  148 + {capabilitiesGamut[2].at(0), capabilitiesGamut[2].at(1)}};
  149 + }
  150 + // Unknown or no color light
  151 + return gamut::maxGamut;
  152 + }
  153 + }
  154 +}
  155 +
127 unsigned int HueLight::KelvinToMired(unsigned int kelvin) const 156 unsigned int HueLight::KelvinToMired(unsigned int kelvin) const
128 { 157 {
129 return int(0.5f + (1000000 / kelvin)); 158 return int(0.5f + (1000000 / kelvin));
@@ -141,7 +170,8 @@ bool HueLight::alert() @@ -141,7 +170,8 @@ bool HueLight::alert()
141 170
142 StateTransaction HueLight::transaction() 171 StateTransaction HueLight::transaction()
143 { 172 {
144 - return StateTransaction(state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", state.getValue().at("state")); 173 + return StateTransaction(
  174 + state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", state.getValue().at("state"));
145 } 175 }
146 176
147 void HueLight::refresh() 177 void HueLight::refresh()
src/Scene.cpp
@@ -56,10 +56,10 @@ bool LightState::hasXY() const @@ -56,10 +56,10 @@ bool LightState::hasXY() const
56 return state.count("xy"); 56 return state.count("xy");
57 } 57 }
58 58
59 -XY LightState::getXY() const 59 +XYBrightness LightState::getXY() const
60 { 60 {
61 const nlohmann::json& xy = state.at("xy"); 61 const nlohmann::json& xy = state.at("xy");
62 - return XY {xy[0].get<float>(), xy[1].get<float>()}; 62 + return XYBrightness {{xy[0].get<float>(), xy[1].get<float>()}, state.at("bri").get<int>() / 255.f};
63 } 63 }
64 64
65 bool LightState::hasCt() const 65 bool LightState::hasCt() const
src/StateTransaction.cpp
@@ -108,6 +108,14 @@ StateTransaction&amp;&amp; StateTransaction::setColorXY(float x, float y) &amp;&amp; @@ -108,6 +108,14 @@ StateTransaction&amp;&amp; StateTransaction::setColorXY(float x, float y) &amp;&amp;
108 return std::move(*this); 108 return std::move(*this);
109 } 109 }
110 110
  111 +StateTransaction&& StateTransaction::setColorXY(const XYBrightness& xy)&&
  112 +{
  113 + request["xy"] = { xy.xy.x, xy.xy.y };
  114 + request["bri"] = static_cast<int>(std::round(xy.brightness * 255.f));
  115 +
  116 + return std::move(*this);
  117 +}
  118 +
111 StateTransaction&& StateTransaction::setColorTemperature(unsigned int mired) && 119 StateTransaction&& StateTransaction::setColorTemperature(unsigned int mired) &&
112 { 120 {
113 unsigned int clamped = std::max(153u, std::min(mired, 500u)); 121 unsigned int clamped = std::max(153u, std::min(mired, 500u));
test/test_Scene.cpp
@@ -66,11 +66,12 @@ TEST(LightState, XY) @@ -66,11 +66,12 @@ TEST(LightState, XY)
66 EXPECT_FALSE(LightState(nlohmann::json::object()).hasXY()); 66 EXPECT_FALSE(LightState(nlohmann::json::object()).hasXY());
67 const float x = 0.6f; 67 const float x = 0.6f;
68 const float y = 0.3f; 68 const float y = 0.3f;
69 - nlohmann::json json {{"xy", {x, y}}}; 69 + nlohmann::json json {{"xy", {x, y}}, {"bri", 255}};
70 const LightState state {json}; 70 const LightState state {json};
71 EXPECT_TRUE(state.hasXY()); 71 EXPECT_TRUE(state.hasXY());
72 - EXPECT_FLOAT_EQ(x, state.getXY().x);  
73 - EXPECT_FLOAT_EQ(y, state.getXY().y); 72 + EXPECT_FLOAT_EQ(x, state.getXY().xy.x);
  73 + EXPECT_FLOAT_EQ(y, state.getXY().xy.y);
  74 + EXPECT_FLOAT_EQ(1.f, state.getXY().brightness);
74 } 75 }
75 76
76 TEST(LightState, Ct) 77 TEST(LightState, Ct)
@@ -286,12 +287,12 @@ TEST_F(SceneTest, getVersion) @@ -286,12 +287,12 @@ TEST_F(SceneTest, getVersion)
286 287
287 TEST_F(SceneTest, getLightstates) 288 TEST_F(SceneTest, getLightstates)
288 { 289 {
289 - const std::string id = "125asav3"; 290 + const std::string id = "125asav3";
290 { 291 {
291 - const std::map<int, LightState> lightstates{ 292 + const std::map<int, LightState> lightstates {
292 {3, LightStateBuilder().setOn(false).setBrightness(100).setXY({0.3, 0.2}).create()}, 293 {3, LightStateBuilder().setOn(false).setBrightness(100).setXY({0.3, 0.2}).create()},
293 {4, LightStateBuilder().setOn(false).setBrightness(200).setXY({0.3, 0.2}).setColorloop(true).create()}, 294 {4, LightStateBuilder().setOn(false).setBrightness(200).setXY({0.3, 0.2}).setColorloop(true).create()},
294 - {5, LightStateBuilder().setOn(true).setBrightness(100).setXY({0.3, 0.2}).create()} }; 295 + {5, LightStateBuilder().setOn(true).setBrightness(100).setXY({0.3, 0.2}).create()}};
295 nlohmann::json lightstatesJson; 296 nlohmann::json lightstatesJson;
296 for (const auto& entry : lightstates) 297 for (const auto& entry : lightstates)
297 { 298 {