Updating Color calculation

This commit is contained in:
Admin
2017-06-27 16:12:47 -05:00
parent 3207b6b76e
commit 86371c03b2
3 changed files with 120 additions and 16 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>4.5.6</version>
<version>4.5.6a</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -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<Double> 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<Double> convertCIEtoRGB(List<Double> xy, int brightness) {
List<Double> 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<Double>();
rgb.add(0, r);
rgb.add(1, g);
rgb.add(2, b);
return rgb;
}
public static String replaceColorData(String request, List<Double> xy, int setIntensity) {
if (request == null) {
return null;
}
boolean notDone = true;
List<Double> 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;
}
}

View File

@@ -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<Double> xy = new ArrayList<Double>(Arrays.asList(new Double(0.3972), new Double(0.4564)));
ArrayList<Double> xy = new ArrayList<Double>(Arrays.asList(new Double(0.671254), new Double(0.303273)));
String colorDecode = ColorDecode.convertCIEtoRGB(xy);
Assert.assertEquals(colorDecode, null);
List<Double> colorDecode = ColorDecode.convertCIEtoRGB(xy, 254);
List<Double> assertDecode = new ArrayList<Double>();
assertDecode.add(0, 255.0);
assertDecode.add(1, 47.0);
assertDecode.add(2, 43.0);
Assert.assertEquals(colorDecode, assertDecode);
}
}