Comments.

Former-commit-id: bb4573afa8072bf03a3ae7c1b8ece721c7ea91ff
This commit is contained in:
Tim Niggemann 2014-07-15 12:06:26 +02:00
parent d2542142be
commit 7e049273a8
2 changed files with 64 additions and 13 deletions

View File

@ -36,8 +36,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Get lamp.
HueLamp& lamp = lamps.at(idx);
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
ColorPoint xy;
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp, xy);
ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp);
// Write color if color has been changed.
if (xy != lamp.color) {
// Send adjust color command in JSON format.
@ -192,7 +191,7 @@ float LedDevicePhilipsHue::getDistanceBetweenTwoPoints(ColorPoint p1, ColorPoint
return sqrt(dx * dx + dy * dy);
}
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, HueLamp lamp, ColorPoint& xy) {
ColorPoint LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, HueLamp lamp) {
// Apply gamma correction.
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
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,
float Y = r * 0.234327f + g * 0.743075f + b * 0.022598f;
float Z = r * 0.0000000f + g * 0.053077f + b * 1.035763f;
// Convert to x,y space.
float cx = X / (X + Y + Z + 0.0000001f);
float cy = Y / (X + Y + Z + 0.0000001f);
float cx = X / (X + Y + Z);
float cy = Y / (X + Y + Z);
if (isnan(cx)) {
cx = 0.0f;
}
if (isnan(cy)) {
cy = 0.0f;
}
xy.x = cx;
xy.y = cy;
ColorPoint xy = {cx, cy};
// Check if the given XY value is within the color reach of our lamps.
if (!isPointInLampsReach(lamp, xy)) {
// 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,
HueLamp::HueLamp(unsigned int id, QString originalState, QString modelId) :
id(id), originalState(originalState) {
/// Hue system model ids.
// Hue system model ids.
const std::set<QString> HUE_BULBS_MODEL_IDS = { "LCT001", "LCT002", "LCT003" };
const std::set<QString> LIVING_COLORS_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC011", "LLC012",
"LLC013", "LST001" };
/// Find id in the sets and set the appropiate color space.
// Find id in the sets and set the appropiate color space.
if (HUE_BULBS_MODEL_IDS.find(modelId) != HUE_BULBS_MODEL_IDS.end()) {
colorSpace.red = {0.675f, 0.322f};
colorSpace.green = {0.4091f, 0.518f};
@ -260,7 +258,7 @@ HueLamp::HueLamp(unsigned int id, QString originalState, QString modelId) :
colorSpace.green = {0.0f, 1.0f};
colorSpace.blue = {0.0f, 0.0f};
}
/// Initialize color with black
// Initialize color with black
color = {0.0f, 0.0f, 0.0f};
}

View File

@ -12,6 +12,9 @@
// Leddevice includes
#include <leddevice/LedDevice.h>
/**
* A color point in the color space of the hue system.
*/
struct ColorPoint {
float x;
float y;
@ -21,10 +24,16 @@ struct ColorPoint {
bool operator==(ColorPoint p1, ColorPoint p2);
bool operator!=(ColorPoint p1, ColorPoint p2);
/**
* Color triangle to define an available color space for the hue lamps.
*/
struct ColorTriangle {
ColorPoint red, green, blue;
};
/**
* Simple class to hold the id, the latest color, the color space and the original state.
*/
class HueLamp {
public:
unsigned int id;
@ -32,6 +41,15 @@ public:
ColorTriangle colorSpace;
QString originalState;
///
/// Constructs the lamp.
///
/// @param id the light id
///
/// @param originalState the json string of the original state
///
/// @param modelId the model id of the hue lamp which is used to determine the color space
///
HueLamp(unsigned int id, QString originalState, QString modelId);
};
@ -149,15 +167,50 @@ private:
///
/// @param blue the blue component in [0, 1]
///
/// @param xyPoint converted xy component
/// @param lamp the hue lamp instance used for color space checks.
///
/// @param brightness converted brightness component
/// @return color point
///
void rgbToXYBrightness(float red, float green, float blue, HueLamp lamp, ColorPoint& xy);
ColorPoint rgbToXYBrightness(float red, float green, float blue, HueLamp lamp);
///
/// @param p1 point one
///
/// @param p2 point tow
///
/// @return the cross product between p1 and p2
///
float crossProduct(ColorPoint p1, ColorPoint p2);
///
/// @param lamp the hue lamp instance
///
/// @param p the color point to check
///
/// @return true if the color point is covered by the lamp color space
///
bool isPointInLampsReach(HueLamp lamp, ColorPoint p);
///
/// @param a reference point one
///
/// @param b reference point two
///
/// @param p the point to which the closest point is to be found
///
/// @return the closest color point of p to a and b
///
ColorPoint getClosestPointToPoint(ColorPoint a, ColorPoint b, ColorPoint p);
///
/// @param p1 point one
///
/// @param p2 point tow
///
/// @return the distance between the two points
///
float getDistanceBetweenTwoPoints(ColorPoint one, ColorPoint two);
};