Implemented color triangle calculations.

Former-commit-id: dbde6635077a82ace5f4ed1fdf89458a28e7bf05
This commit is contained in:
Tim Niggemann 2014-07-15 08:51:07 +02:00
parent 853d002894
commit f5a8174783
2 changed files with 143 additions and 29 deletions

View File

@ -10,6 +10,8 @@
#include <QHttpRequestHeader> #include <QHttpRequestHeader>
#include <QEventLoop> #include <QEventLoop>
#include <set>
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
host(output.c_str()), username("newdeveloper") { host(output.c_str()), username("newdeveloper") {
http = new QHttp(host); http = new QHttp(host);
@ -24,18 +26,21 @@ LedDevicePhilipsHue::~LedDevicePhilipsHue() {
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) { int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Save light states if not done before. // Save light states if not done before.
if (!statesSaved()) { if (!areStatesSaved()) {
saveStates(ledValues.size()); saveStates((unsigned int) ledValues.size());
switchOn(ledValues.size()); switchOn((unsigned int) ledValues.size());
} }
// Iterate through colors and set light states. // Iterate through colors and set light states.
unsigned int lightId = 1; unsigned int lightId = 1;
for (const ColorRgb& color : ledValues) { for (const ColorRgb& color : ledValues) {
float x, y, b; // Find triangle.
CGTriangle triangle = triangles.at(lightId - 1);
// Scale colors from [0, 255] to [0, 1] and convert to xy space. // Scale colors from [0, 255] to [0, 1] and convert to xy space.
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, x, y, b); CGPoint xy;
float b;
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, triangle, xy, b);
// Send adjust color command in JSON format. // Send adjust color command in JSON format.
put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(x).arg(y)); put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(xy.x).arg(xy.y));
// Send brightness color command in JSON format. // Send brightness color command in JSON format.
put(getStateRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f))); put(getStateRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f)));
// Next light id. // Next light id.
@ -48,7 +53,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
int LedDevicePhilipsHue::switchOff() { int LedDevicePhilipsHue::switchOff() {
timer.stop(); timer.stop();
// If light states have been saved before, ... // If light states have been saved before, ...
if (statesSaved()) { if (areStatesSaved()) {
// ... restore them. // ... restore them.
restoreStates(); restoreStates();
} }
@ -93,6 +98,27 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
return QString("lights/%1").arg(lightId); return QString("lights/%1").arg(lightId);
} }
CGTriangle LedDevicePhilipsHue::getTriangle(QString modelId) {
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" };
CGTriangle triangle;
if (HUE_BULBS_MODEL_IDS.find(modelId) != HUE_BULBS_MODEL_IDS.end()) {
triangle.red = {0.675f, 0.322f};
triangle.green = {0.4091f, 0.518f};
triangle.blue = {0.167f, 0.04f};
} else if (LIVING_COLORS_MODEL_IDS.find(modelId) != LIVING_COLORS_MODEL_IDS.end()) {
triangle.red = {0.703f, 0.296f};
triangle.green = {0.214f, 0.709f};
triangle.blue = {0.139f, 0.081f};
} else {
triangle.red = {1.0f, 0.0f};
triangle.green = {0.0f, 1.0f};
triangle.blue = {0.0f, 0.0f};
}
return triangle;
}
void LedDevicePhilipsHue::saveStates(unsigned int nLights) { void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Clear saved light states. // Clear saved light states.
states.clear(); states.clear();
@ -116,8 +142,12 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
state["xy"] = json["state"]["xy"]; state["xy"] = json["state"]["xy"];
state["bri"] = json["state"]["bri"]; state["bri"] = json["state"]["bri"];
} }
// Save id.
ids.push_back(QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", ""));
// Save state object. // Save state object.
states.push_back(QString(writer.write(state).c_str()).trimmed()); states.push_back(QString(writer.write(state).c_str()).trimmed());
// Determine triangle.
triangles.push_back(getTriangle(ids.back()));
} }
} }
@ -137,27 +167,94 @@ void LedDevicePhilipsHue::restoreStates() {
states.clear(); states.clear();
} }
bool LedDevicePhilipsHue::statesSaved() { bool LedDevicePhilipsHue::areStatesSaved() {
return !states.empty(); return !states.empty();
} }
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness) { float LedDevicePhilipsHue::crossProduct(CGPoint p1, CGPoint p2) {
// Apply gamma correction. return p1.x * p2.y - p1.y * p2.x;
red = (red > 0.04045f) ? qPow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f); }
green = (green > 0.04045f) ? qPow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
blue = (blue > 0.04045f) ? qPow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f); bool LedDevicePhilipsHue::isPointInLampsReach(CGTriangle triangle, CGPoint p) {
// Convert to XYZ space. CGPoint v1 = { triangle.green.x - triangle.red.x, triangle.green.y - triangle.red.y };
float X = red * 0.649926f + green * 0.103455f + blue * 0.197109f; CGPoint v2 = { triangle.blue.x - triangle.red.x, triangle.blue.y - triangle.red.y };
float Y = red * 0.234327f + green * 0.743075f + blue * 0.022598f; CGPoint q = { p.x - triangle.red.x, p.y - triangle.red.y };
float Z = red * 0.0000000f + green * 0.053077f + blue * 1.035763f; float s = crossProduct(q, v2) / crossProduct(v1, v2);
// Convert to x,y space. float t = crossProduct(v1, q) / crossProduct(v1, v2);
x = X / (X + Y + Z); if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f)) {
y = Y / (X + Y + Z); return true;
if (isnan(x)) { } else {
x = 0.0f; return false;
} }
if (isnan(y)) { }
y = 0.0f;
CGPoint LedDevicePhilipsHue::getClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P) {
CGPoint AP = { P.x - A.x, P.y - A.y };
CGPoint AB = { B.x - A.x, B.y - A.y };
float ab2 = AB.x * AB.x + AB.y * AB.y;
float ap_ab = AP.x * AB.x + AP.y * AB.y;
float t = ap_ab / ab2;
if (t < 0.0f) {
t = 0.0f;
} else if (t > 1.0f) {
t = 1.0f;
}
return {A.x + AB.x * t, A.y + AB.y * t};
}
float LedDevicePhilipsHue::getDistanceBetweenTwoPoints(CGPoint one, CGPoint two) {
// Horizontal difference.
float dx = one.x - two.x;
// Vertical difference.
float dy = one.y - two.y;
float dist = sqrt(dx * dx + dy * dy);
return dist;
}
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGTriangle triangle, CGPoint& xyPoint,
float& brightness) {
// 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);
float b = (blue > 0.04045f) ? powf((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
// Convert to XYZ space.
float X = r * 0.649926f + g * 0.103455f + b * 0.197109f;
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);
if (isnan(cx)) {
cx = 0.0f;
}
if (isnan(cy)) {
cy = 0.0f;
}
xyPoint.x = cx;
xyPoint.y = cy;
// Check if the given XY value is within the colourreach of our lamps.
if (!isPointInLampsReach(triangle, xyPoint)) {
// 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.
CGPoint pAB = getClosestPointToPoint(triangle.red, triangle.green, xyPoint);
CGPoint pAC = getClosestPointToPoint(triangle.blue, triangle.red, xyPoint);
CGPoint pBC = getClosestPointToPoint(triangle.green, triangle.blue, xyPoint);
// Get the distances per point and see which point is closer to our Point.
float dAB = getDistanceBetweenTwoPoints(xyPoint, pAB);
float dAC = getDistanceBetweenTwoPoints(xyPoint, pAC);
float dBC = getDistanceBetweenTwoPoints(xyPoint, pBC);
float lowest = dAB;
CGPoint closestPoint = pAB;
if (dAC < lowest) {
lowest = dAC;
closestPoint = pAC;
}
if (dBC < lowest) {
lowest = dBC;
closestPoint = pBC;
}
// Change the xy value to a value which is within the reach of the lamp.
xyPoint.x = closestPoint.x;
xyPoint.y = closestPoint.y;
} }
// Brightness is simply Y in the XYZ space. // Brightness is simply Y in the XYZ space.
brightness = Y; brightness = Y;

View File

@ -12,6 +12,15 @@
// Leddevice includes // Leddevice includes
#include <leddevice/LedDevice.h> #include <leddevice/LedDevice.h>
struct CGPoint {
float x;
float y;
};
struct CGTriangle {
CGPoint red, green, blue;
};
/** /**
* Implementation for the Philips Hue system. * Implementation for the Philips Hue system.
* *
@ -56,6 +65,10 @@ private slots:
private: private:
/// Array to save the light states. /// Array to save the light states.
std::vector<QString> states; std::vector<QString> states;
/// Array to save model ids.
std::vector<QString> ids;
/// Color triangles.
std::vector<CGTriangle> triangles;
/// Ip address of the bridge /// Ip address of the bridge
QString host; QString host;
/// User name for the API ("newdeveloper") /// User name for the API ("newdeveloper")
@ -114,7 +127,7 @@ private:
/// ///
/// @return true if light states have been saved. /// @return true if light states have been saved.
/// ///
bool statesSaved(); bool areStatesSaved();
/// ///
/// Converts an RGB color to the Hue xy color space and brightness /// Converts an RGB color to the Hue xy color space and brightness
@ -126,12 +139,16 @@ private:
/// ///
/// @param blue the blue component in [0, 1] /// @param blue the blue component in [0, 1]
/// ///
/// @param x converted x component /// @param xyPoint converted xy component
///
/// @param y converted y component
/// ///
/// @param brightness converted brightness component /// @param brightness converted brightness component
/// ///
void rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness); void rgbToXYBrightness(float red, float green, float blue, CGTriangle triangle, CGPoint& xyPoint, float& brightness);
CGTriangle getTriangle(QString modelId);
float crossProduct(CGPoint p1, CGPoint p2);
bool isPointInLampsReach(CGTriangle triangle, CGPoint p);
CGPoint getClosestPointToPoint(CGPoint a, CGPoint b, CGPoint p);
float getDistanceBetweenTwoPoints(CGPoint one, CGPoint two);
}; };