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

Commit bb4573a

Browse files
author
Tim Niggemann
committed
Comments.
1 parent 7120af8 commit bb4573a

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
3636
// Get lamp.
3737
HueLamp& lamp = lamps.at(idx);
3838
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
39-
ColorPoint xy;
40-
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp, xy);
39+
ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp);
4140
// Write color if color has been changed.
4241
if (xy != lamp.color) {
4342
// Send adjust color command in JSON format.
@@ -192,7 +191,7 @@ float LedDevicePhilipsHue::getDistanceBetweenTwoPoints(ColorPoint p1, ColorPoint
192191
return sqrt(dx * dx + dy * dy);
193192
}
194193

195-
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, HueLamp lamp, ColorPoint& xy) {
194+
ColorPoint LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, HueLamp lamp) {
196195
// Apply gamma correction.
197196
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
198197
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
@@ -202,16 +201,15 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
202201
float Y = r * 0.234327f + g * 0.743075f + b * 0.022598f;
203202
float Z = r * 0.0000000f + g * 0.053077f + b * 1.035763f;
204203
// Convert to x,y space.
205-
float cx = X / (X + Y + Z + 0.0000001f);
206-
float cy = Y / (X + Y + Z + 0.0000001f);
204+
float cx = X / (X + Y + Z);
205+
float cy = Y / (X + Y + Z);
207206
if (isnan(cx)) {
208207
cx = 0.0f;
209208
}
210209
if (isnan(cy)) {
211210
cy = 0.0f;
212211
}
213-
xy.x = cx;
214-
xy.y = cy;
212+
ColorPoint xy = {cx, cy};
215213
// Check if the given XY value is within the color reach of our lamps.
216214
if (!isPointInLampsReach(lamp, xy)) {
217215
// It seems the color is out of reach let's find the closes colour we can produce with our lamp and send this XY value out.
@@ -242,11 +240,11 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
242240

243241
HueLamp::HueLamp(unsigned int id, QString originalState, QString modelId) :
244242
id(id), originalState(originalState) {
245-
/// Hue system model ids.
243+
// Hue system model ids.
246244
const std::set<QString> HUE_BULBS_MODEL_IDS = { "LCT001", "LCT002", "LCT003" };
247245
const std::set<QString> LIVING_COLORS_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC011", "LLC012",
248246
"LLC013", "LST001" };
249-
/// Find id in the sets and set the appropiate color space.
247+
// Find id in the sets and set the appropiate color space.
250248
if (HUE_BULBS_MODEL_IDS.find(modelId) != HUE_BULBS_MODEL_IDS.end()) {
251249
colorSpace.red = {0.675f, 0.322f};
252250
colorSpace.green = {0.4091f, 0.518f};
@@ -260,7 +258,7 @@ HueLamp::HueLamp(unsigned int id, QString originalState, QString modelId) :
260258
colorSpace.green = {0.0f, 1.0f};
261259
colorSpace.blue = {0.0f, 0.0f};
262260
}
263-
/// Initialize color with black
261+
// Initialize color with black
264262
color = {0.0f, 0.0f, 0.0f};
265263
}
266264

libsrc/leddevice/LedDevicePhilipsHue.h

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

15+
/**
16+
* A color point in the color space of the hue system.
17+
*/
1518
struct ColorPoint {
1619
float x;
1720
float y;
@@ -21,17 +24,32 @@ struct ColorPoint {
2124
bool operator==(ColorPoint p1, ColorPoint p2);
2225
bool operator!=(ColorPoint p1, ColorPoint p2);
2326

27+
/**
28+
* Color triangle to define an available color space for the hue lamps.
29+
*/
2430
struct ColorTriangle {
2531
ColorPoint red, green, blue;
2632
};
2733

34+
/**
35+
* Simple class to hold the id, the latest color, the color space and the original state.
36+
*/
2837
class HueLamp {
2938
public:
3039
unsigned int id;
3140
ColorPoint color;
3241
ColorTriangle colorSpace;
3342
QString originalState;
3443

44+
///
45+
/// Constructs the lamp.
46+
///
47+
/// @param id the light id
48+
///
49+
/// @param originalState the json string of the original state
50+
///
51+
/// @param modelId the model id of the hue lamp which is used to determine the color space
52+
///
3553
HueLamp(unsigned int id, QString originalState, QString modelId);
3654
};
3755

@@ -149,15 +167,50 @@ private slots:
149167
///
150168
/// @param blue the blue component in [0, 1]
151169
///
152-
/// @param xyPoint converted xy component
170+
/// @param lamp the hue lamp instance used for color space checks.
153171
///
154-
/// @param brightness converted brightness component
172+
/// @return color point
155173
///
156-
void rgbToXYBrightness(float red, float green, float blue, HueLamp lamp, ColorPoint& xy);
174+
ColorPoint rgbToXYBrightness(float red, float green, float blue, HueLamp lamp);
157175

176+
///
177+
/// @param p1 point one
178+
///
179+
/// @param p2 point tow
180+
///
181+
/// @return the cross product between p1 and p2
182+
///
158183
float crossProduct(ColorPoint p1, ColorPoint p2);
184+
185+
186+
///
187+
/// @param lamp the hue lamp instance
188+
///
189+
/// @param p the color point to check
190+
///
191+
/// @return true if the color point is covered by the lamp color space
192+
///
159193
bool isPointInLampsReach(HueLamp lamp, ColorPoint p);
194+
195+
196+
///
197+
/// @param a reference point one
198+
///
199+
/// @param b reference point two
200+
///
201+
/// @param p the point to which the closest point is to be found
202+
///
203+
/// @return the closest color point of p to a and b
204+
///
160205
ColorPoint getClosestPointToPoint(ColorPoint a, ColorPoint b, ColorPoint p);
206+
207+
///
208+
/// @param p1 point one
209+
///
210+
/// @param p2 point tow
211+
///
212+
/// @return the distance between the two points
213+
///
161214
float getDistanceBetweenTwoPoints(ColorPoint one, ColorPoint two);
162215

163216
};

0 commit comments

Comments
 (0)