Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit dbde663

Browse files
author
Tim Niggemann
committed
Implemented color triangle calculations.
1 parent 04959ea commit dbde663

File tree

2 files changed

+140
-26
lines changed

2 files changed

+140
-26
lines changed

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 118 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <QHttpRequestHeader>
1111
#include <QEventLoop>
1212

13+
#include <set>
14+
1315
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
1416
host(output.c_str()), username("newdeveloper") {
1517
http = new QHttp(host);
@@ -24,18 +26,21 @@ LedDevicePhilipsHue::~LedDevicePhilipsHue() {
2426

2527
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
2628
// Save light states if not done before.
27-
if (!statesSaved()) {
28-
saveStates(ledValues.size());
29-
switchOn(ledValues.size());
29+
if (!areStatesSaved()) {
30+
saveStates((unsigned int) ledValues.size());
31+
switchOn((unsigned int) ledValues.size());
3032
}
3133
// Iterate through colors and set light states.
3234
unsigned int lightId = 1;
3335
for (const ColorRgb& color : ledValues) {
34-
float x, y, b;
36+
// Find triangle.
37+
CGTriangle triangle = triangles.at(lightId - 1);
3538
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
36-
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, x, y, b);
39+
CGPoint xy;
40+
float b;
41+
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, triangle, xy, b);
3742
// Send adjust color command in JSON format.
38-
put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(x).arg(y));
43+
put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(xy.x).arg(xy.y));
3944
// Send brightness color command in JSON format.
4045
put(getStateRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f)));
4146
// Next light id.
@@ -48,7 +53,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
4853
int LedDevicePhilipsHue::switchOff() {
4954
timer.stop();
5055
// If light states have been saved before, ...
51-
if (statesSaved()) {
56+
if (areStatesSaved()) {
5257
// ... restore them.
5358
restoreStates();
5459
}
@@ -93,6 +98,27 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
9398
return QString("lights/%1").arg(lightId);
9499
}
95100

101+
CGTriangle LedDevicePhilipsHue::getTriangle(QString modelId) {
102+
const std::set<QString> HUE_BULBS_MODEL_IDS = { "LCT001", "LCT002", "LCT003" };
103+
const std::set<QString> LIVING_COLORS_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC011", "LLC012",
104+
"LLC013", "LST001" };
105+
CGTriangle triangle;
106+
if (HUE_BULBS_MODEL_IDS.find(modelId) != HUE_BULBS_MODEL_IDS.end()) {
107+
triangle.red = {0.675f, 0.322f};
108+
triangle.green = {0.4091f, 0.518f};
109+
triangle.blue = {0.167f, 0.04f};
110+
} else if (LIVING_COLORS_MODEL_IDS.find(modelId) != LIVING_COLORS_MODEL_IDS.end()) {
111+
triangle.red = {0.703f, 0.296f};
112+
triangle.green = {0.214f, 0.709f};
113+
triangle.blue = {0.139f, 0.081f};
114+
} else {
115+
triangle.red = {1.0f, 0.0f};
116+
triangle.green = {0.0f, 1.0f};
117+
triangle.blue = {0.0f, 0.0f};
118+
}
119+
return triangle;
120+
}
121+
96122
void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
97123
// Clear saved light states.
98124
states.clear();
@@ -116,8 +142,12 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
116142
state["xy"] = json["state"]["xy"];
117143
state["bri"] = json["state"]["bri"];
118144
}
145+
// Save id.
146+
ids.push_back(QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", ""));
119147
// Save state object.
120148
states.push_back(QString(writer.write(state).c_str()).trimmed());
149+
// Determine triangle.
150+
triangles.push_back(getTriangle(ids.back()));
121151
}
122152
}
123153

@@ -137,27 +167,94 @@ void LedDevicePhilipsHue::restoreStates() {
137167
states.clear();
138168
}
139169

140-
bool LedDevicePhilipsHue::statesSaved() {
170+
bool LedDevicePhilipsHue::areStatesSaved() {
141171
return !states.empty();
142172
}
143173

144-
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness) {
174+
float LedDevicePhilipsHue::crossProduct(CGPoint p1, CGPoint p2) {
175+
return p1.x * p2.y - p1.y * p2.x;
176+
}
177+
178+
bool LedDevicePhilipsHue::isPointInLampsReach(CGTriangle triangle, CGPoint p) {
179+
CGPoint v1 = { triangle.green.x - triangle.red.x, triangle.green.y - triangle.red.y };
180+
CGPoint v2 = { triangle.blue.x - triangle.red.x, triangle.blue.y - triangle.red.y };
181+
CGPoint q = { p.x - triangle.red.x, p.y - triangle.red.y };
182+
float s = crossProduct(q, v2) / crossProduct(v1, v2);
183+
float t = crossProduct(v1, q) / crossProduct(v1, v2);
184+
if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f)) {
185+
return true;
186+
} else {
187+
return false;
188+
}
189+
}
190+
191+
CGPoint LedDevicePhilipsHue::getClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P) {
192+
CGPoint AP = { P.x - A.x, P.y - A.y };
193+
CGPoint AB = { B.x - A.x, B.y - A.y };
194+
float ab2 = AB.x * AB.x + AB.y * AB.y;
195+
float ap_ab = AP.x * AB.x + AP.y * AB.y;
196+
float t = ap_ab / ab2;
197+
if (t < 0.0f) {
198+
t = 0.0f;
199+
} else if (t > 1.0f) {
200+
t = 1.0f;
201+
}
202+
return {A.x + AB.x * t, A.y + AB.y * t};
203+
}
204+
205+
float LedDevicePhilipsHue::getDistanceBetweenTwoPoints(CGPoint one, CGPoint two) {
206+
// Horizontal difference.
207+
float dx = one.x - two.x;
208+
// Vertical difference.
209+
float dy = one.y - two.y;
210+
float dist = sqrt(dx * dx + dy * dy);
211+
return dist;
212+
}
213+
214+
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGTriangle triangle, CGPoint& xyPoint,
215+
float& brightness) {
145216
// Apply gamma correction.
146-
red = (red > 0.04045f) ? qPow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
147-
green = (green > 0.04045f) ? qPow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
148-
blue = (blue > 0.04045f) ? qPow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
217+
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
218+
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
219+
float b = (blue > 0.04045f) ? powf((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
149220
// Convert to XYZ space.
150-
float X = red * 0.649926f + green * 0.103455f + blue * 0.197109f;
151-
float Y = red * 0.234327f + green * 0.743075f + blue * 0.022598f;
152-
float Z = red * 0.0000000f + green * 0.053077f + blue * 1.035763f;
221+
float X = r * 0.649926f + g * 0.103455f + b * 0.197109f;
222+
float Y = r * 0.234327f + g * 0.743075f + b * 0.022598f;
223+
float Z = r * 0.0000000f + g * 0.053077f + b * 1.035763f;
153224
// Convert to x,y space.
154-
x = X / (X + Y + Z);
155-
y = Y / (X + Y + Z);
156-
if (isnan(x)) {
157-
x = 0.0f;
225+
float cx = X / (X + Y + Z + 0.0000001f);
226+
float cy = Y / (X + Y + Z + 0.0000001f);
227+
if (isnan(cx)) {
228+
cx = 0.0f;
229+
}
230+
if (isnan(cy)) {
231+
cy = 0.0f;
158232
}
159-
if (isnan(y)) {
160-
y = 0.0f;
233+
xyPoint.x = cx;
234+
xyPoint.y = cy;
235+
// Check if the given XY value is within the colourreach of our lamps.
236+
if (!isPointInLampsReach(triangle, xyPoint)) {
237+
// It seems the colour is out of reach let's find the closes colour we can produce with our lamp and send this XY value out.
238+
CGPoint pAB = getClosestPointToPoint(triangle.red, triangle.green, xyPoint);
239+
CGPoint pAC = getClosestPointToPoint(triangle.blue, triangle.red, xyPoint);
240+
CGPoint pBC = getClosestPointToPoint(triangle.green, triangle.blue, xyPoint);
241+
// Get the distances per point and see which point is closer to our Point.
242+
float dAB = getDistanceBetweenTwoPoints(xyPoint, pAB);
243+
float dAC = getDistanceBetweenTwoPoints(xyPoint, pAC);
244+
float dBC = getDistanceBetweenTwoPoints(xyPoint, pBC);
245+
float lowest = dAB;
246+
CGPoint closestPoint = pAB;
247+
if (dAC < lowest) {
248+
lowest = dAC;
249+
closestPoint = pAC;
250+
}
251+
if (dBC < lowest) {
252+
lowest = dBC;
253+
closestPoint = pBC;
254+
}
255+
// Change the xy value to a value which is within the reach of the lamp.
256+
xyPoint.x = closestPoint.x;
257+
xyPoint.y = closestPoint.y;
161258
}
162259
// Brightness is simply Y in the XYZ space.
163260
brightness = Y;

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
// Leddevice includes
1313
#include <leddevice/LedDevice.h>
1414

15+
struct CGPoint {
16+
float x;
17+
float y;
18+
};
19+
20+
struct CGTriangle {
21+
CGPoint red, green, blue;
22+
};
23+
1524
/**
1625
* Implementation for the Philips Hue system.
1726
*
@@ -56,6 +65,10 @@ private slots:
5665
private:
5766
/// Array to save the light states.
5867
std::vector<QString> states;
68+
/// Array to save model ids.
69+
std::vector<QString> ids;
70+
/// Color triangles.
71+
std::vector<CGTriangle> triangles;
5972
/// Ip address of the bridge
6073
QString host;
6174
/// User name for the API ("newdeveloper")
@@ -114,7 +127,7 @@ private slots:
114127
///
115128
/// @return true if light states have been saved.
116129
///
117-
bool statesSaved();
130+
bool areStatesSaved();
118131

119132
///
120133
/// Converts an RGB color to the Hue xy color space and brightness
@@ -126,12 +139,16 @@ private slots:
126139
///
127140
/// @param blue the blue component in [0, 1]
128141
///
129-
/// @param x converted x component
130-
///
131-
/// @param y converted y component
142+
/// @param xyPoint converted xy component
132143
///
133144
/// @param brightness converted brightness component
134145
///
135-
void rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness);
146+
void rgbToXYBrightness(float red, float green, float blue, CGTriangle triangle, CGPoint& xyPoint, float& brightness);
147+
148+
CGTriangle getTriangle(QString modelId);
149+
float crossProduct(CGPoint p1, CGPoint p2);
150+
bool isPointInLampsReach(CGTriangle triangle, CGPoint p);
151+
CGPoint getClosestPointToPoint(CGPoint a, CGPoint b, CGPoint p);
152+
float getDistanceBetweenTwoPoints(CGPoint one, CGPoint two);
136153

137154
};

0 commit comments

Comments
 (0)