diff --git a/pom.xml b/pom.xml index 498e56a..9bc590f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4.5.6 + 4.5.6a jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java index 1585d45..2832fdf 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java @@ -1,21 +1,120 @@ package com.bwssystems.HABridge.hue; +import java.util.ArrayList; import java.util.List; -public class ColorDecode { +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; - public static String convertCIEtoRGB(List xy) { - double x; - double y; - double Y; +public class ColorDecode { + private static final Logger log = LoggerFactory.getLogger(ColorDecode.class); + private static final String COLOR_R = "${color.r}"; + private static final String COLOR_G = "${color.g}"; + private static final String COLOR_B = "${color.b}"; + + public static List convertCIEtoRGB(List xy, int brightness) { + List rgb; + double x = xy.get(0); // the given x value + double y = xy.get(1); // the given y value + double z = 1.0 - x - y; + double Y = (double)brightness/(double)254.00; // The given brightness value + double X = (Y / y) * x; + double Z = (Y / y) * z; + + double r = X * 1.656492 - Y * 0.354851 - Z * 0.255038; + double g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152; + double b = X * 0.051713 - Y * 0.121364 + Z * 1.011530; + + if (r > b && r > g && r > 1.0) { + + g = g / r; + b = b / r; + r = 1.0; + } + else if (g > b && g > r && g > 1.0) { + + r = r / g; + b = b / g; + g = 1.0; + } + else if (b > r && b > g && b > 1.0) { + + r = r / b; + g = g / b; + b = 1.0; + } + + + r = r <= 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math.pow(r, (1.0 / 2.4)) - 0.055; + g = g <= 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math.pow(g, (1.0 / 2.4)) - 0.055; + b = b <= 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math.pow(b, (1.0 / 2.4)) - 0.055; - x = xy.get(0) * 100; - y = xy.get(1) * 100; - Y= y; - double R = 3.240479*((x*Y)/y) + -1.537150*Y + -0.498535*(((1-x-y)*Y)/y); - double G = -0.969256*((x*Y)/y) + 1.875992*Y + 0.041556*(((1-x-y)*Y)/y); - double B = 0.055648*((x*Y)/y) + -0.204043*Y + 1.057311*(((1-x-y)*Y)/y); + if (r > b && r > g) { + // red is biggest + if (r > 1.0) { + g = g / r; + b = b / r; + r = 1.0; + } + } + else if (g > b && g > r) { + // green is biggest + if (g > 1.0) { + r = r / g; + b = b / g; + g = 1.0; + } + } + else if (b > r && b > g) { + // blue is biggest + if (b > 1.0) { + r = r / b; + g = g / b; + b = 1.0; + } + } + if(r < 0.0) + r = 0; + if(g < 0.0) + g = 0; + if(b < 0.0) + b = 0; + r = Math.round(r * 255); + g = Math.round(g * 255); + b = Math.round(b * 255); + rgb = new ArrayList(); + rgb.add(0, r); + rgb.add(1, g); + rgb.add(2, b); + return rgb; + } + + public static String replaceColorData(String request, List xy, int setIntensity) { + if (request == null) { + return null; + } + boolean notDone = true; + List rgb = convertCIEtoRGB(xy, setIntensity); - return null; + while(notDone) { + notDone = false; + if (request.contains(COLOR_R)) { + request = request.replace(COLOR_R, String.valueOf(rgb.get(0))); + notDone = true; + } + + if (request.contains(COLOR_G)) { + request = request.replace(COLOR_G, String.valueOf(rgb.get(1))); + notDone = true; + } + + if (request.contains(COLOR_B)) { + request = request.replace(COLOR_B, String.valueOf(rgb.get(2))); + notDone = true; + } + + log.debug("Request <<" + request + ">>, not done: " + notDone); + } + return request; } } diff --git a/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java b/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java index 4d148be..6791b46 100644 --- a/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java +++ b/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java @@ -2,6 +2,7 @@ package com.bwssystems.color.test; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -12,10 +13,14 @@ public class ConvertCIEColorTestCase { @Test public void testColorConversion() { - ArrayList xy = new ArrayList(Arrays.asList(new Double(0.3972), new Double(0.4564))); + ArrayList xy = new ArrayList(Arrays.asList(new Double(0.671254), new Double(0.303273))); - String colorDecode = ColorDecode.convertCIEtoRGB(xy); - Assert.assertEquals(colorDecode, null); + List colorDecode = ColorDecode.convertCIEtoRGB(xy, 254); + List assertDecode = new ArrayList(); + assertDecode.add(0, 255.0); + assertDecode.add(1, 47.0); + assertDecode.add(2, 43.0); + Assert.assertEquals(colorDecode, assertDecode); } }