diff --git a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java index 1b4bf99..13a7dad 100644 --- a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java +++ b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java @@ -75,6 +75,8 @@ public class DeviceRepository extends BackupHandler { nextId = Integer.decode(list[i].getId()); } } + + nextId = nextId + 1; } } diff --git a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java index 8ccaf7c..276b08a 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java @@ -26,7 +26,7 @@ public class BrightnessDecode { if (targetBri != null) { setIntensity = targetBri; } else if (targetBriInc != null) { - if ((setIntensity + targetBriInc) <= 0) + if ((setIntensity + targetBriInc) <= 1) setIntensity = targetBriInc; else if ((setIntensity + targetBriInc) > 254) setIntensity = targetBriInc; @@ -53,7 +53,7 @@ public class BrightnessDecode { String replaceValue = null; String replaceTarget = null; int percentBrightness = 0; - float decimalBrightness = (float) 0.0; + float decimalBrightness = (float) 1.0; Map variables = new HashMap(); String mathDescriptor = null; @@ -64,8 +64,8 @@ public class BrightnessDecode { else percentBrightness = (int) Math.round(intensity / 255.0 * 100); } else { - decimalBrightness = (float) 0.0; - percentBrightness = 0; + decimalBrightness = (float) 1.0; + percentBrightness = 1; } while(notDone) { diff --git a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java index 1144bd6..1284b8e 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java @@ -20,23 +20,101 @@ public class ColorDecode { private static final String COLOR_GX = "${color.gx}"; private static final String COLOR_BX = "${color.bx}"; private static final String COLOR_RGBX = "${color.rgbx}"; - private static final String COLOR_HSL = "${color.hsl}"; + private static final String COLOR_HSB = "${color.hsb}"; private static final String COLOR_H = "${color.h}"; private static final String COLOR_S = "${color.s}"; - private static final String COLOR_L = "${color.l}"; private static final String COLOR_XY = "${color.xy}"; private static final String COLOR_BRI = "${colorbri}"; private static final Pattern COLOR_MILIGHT = Pattern.compile("\\$\\{color.milight\\:([01234])\\}"); - public static List convertHSLtoRGB(HueSatBri hsl) { + public static List convertHSBtoRGB(HueSatBri hsb) { List rgb; - int[] rgbInt = ColorConverter.normalizeRGB(ColorConverter.HSLtoRGB((float)((float)hsl.getHue()/65535.0f), (float)((float)hsl.getSat()/254.0f), (float)((float)hsl.getBri()/254))); + Float hue = (Float)(hsb.getHue()*1.0f); + Float saturation = (Float)(hsb.getSat()*1.0f); + Float brightness = (Float)(hsb.getBri()*1.0f); + log.info("Hue = " + hue + ", Sat = " + saturation + ", Bri = " + brightness); + //Convert Hue into degrees for HSB + hue = hue / 182.04f; + //Bri and Sat must be values from 0-1 (~percentage) + brightness = brightness / 255.0f; + saturation = saturation / 255.0f; + + Float r = 0f; + Float g = 0f; + Float b = 0f; + + if (saturation == 0) + { + r = g = b = brightness; + } + else + { + // the color wheel consists of 6 sectors. + Float sectorPos = hue / 60.0f; + int sectorNumber = (int)(Math.floor(sectorPos)); + // get the fractional part of the sector + Float fractionalSector = sectorPos - sectorNumber; + + // calculate values for the three axes of the color. + Float p = brightness * (1.0f - saturation); + Float q = brightness * (1.0f - (saturation * fractionalSector)); + Float t = brightness * (1.0f - (saturation * (1f - fractionalSector))); + + // assign the fractional colors to r, g, and b based on the sector the angle is in. + switch (sectorNumber) + { + case 0: + r = brightness; + g = t; + b = p; + break; + case 1: + r = q; + g = brightness; + b = p; + break; + case 2: + r = p; + g = brightness; + b = t; + break; + case 3: + r = p; + g = q; + b = brightness; + break; + case 4: + r = t; + g = p; + b = brightness; + break; + case 5: + r = brightness; + g = p; + b = q; + break; + } + } + + //Check if any value is out of byte range + if (r < 0f) + { + r = 0f; + } + if (g < 0f) + { + g = 0f; + } + if (b < 0f) + { + b = 0f; + } + rgb = new ArrayList(); - rgb.add(rgbInt[0]); - rgb.add(rgbInt[1]); - rgb.add(rgbInt[2]); - - log.debug("Color change with HSL: " + hsl + ". Resulting RGB Values: " + rgb.get(0) + " " + rgb.get(1) + " " + rgb.add((int)Math.round(r*255)); + rgb.add((int)Math.round(g*255)); + rgb.add((int)Math.round(b*255)); + log.debug("Color change with HSB: " + hsb + ". Resulting RGB Values: " + rgb.get(0) + " " + rgb.get(1) + " " + rgb.get(2)); return rgb; } @@ -125,7 +203,7 @@ public class ColorDecode { } else if (colorMode == ColorData.ColorMode.CT) { rgb = convertCTtoRGB((Integer) colorData.getData()); } else if (colorMode == ColorData.ColorMode.HS) { - rgb = convertHSLtoRGB((HueSatBri) colorData.getData()); + rgb = convertHSBtoRGB((HueSatBri) colorData.getData()); } while (notDone) { @@ -206,23 +284,20 @@ public class ColorDecode { notDone = true; } - if (request.contains(COLOR_L)) { + if (request.contains(COLOR_BRI)) { if (colorMode == ColorData.ColorMode.HS) { HueSatBri hslData = (HueSatBri) colorData.getData(); - request = request.replace(COLOR_L, String.format("%d", hslData.getBri())); + request = request.replace(COLOR_BRI, String.format("%d", hslData.getBri())); } else { - float[] hsb = new float[3]; - Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb); - float bright = hsb[2] * (float) 100.0; - request = request.replace(COLOR_L, String.format("%f", bright)); + request = request.replace(COLOR_BRI, String.format("%f", setIntensity)); } notDone = true; } - if (request.contains(COLOR_HSL)) { + if (request.contains(COLOR_HSB)) { if (colorMode == ColorData.ColorMode.HS) { HueSatBri hslData = (HueSatBri) colorData.getData(); - request = request.replace(COLOR_HSL, + request = request.replace(COLOR_HSB, String.format("%d,%d,%d", hslData.getHue(), hslData.getSat(), hslData.getBri())); } else { float[] hsb = new float[3]; @@ -230,7 +305,7 @@ public class ColorDecode { float hue = hsb[0] * (float) 360.0; float sat = hsb[1] * (float) 100.0; float bright = hsb[2] * (float) 100.0; - request = request.replace(COLOR_HSL, String.format("%f,%f,%f", hue, sat, bright)); + request = request.replace(COLOR_HSB, String.format("%f,%f,%f", hue, sat, bright)); } notDone = true; } diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 7548471..254708f 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -473,9 +473,9 @@ public class HueMulator { if (deviceState != null) { deviceState.setOn(stateChanges.isOn()); if (!deviceState.isOn() && deviceState.getBri() == 254) - deviceState.setBri(0); + deviceState.setBri(1); if (!deviceState.isOn() && offState) - deviceState.setBri(0); + deviceState.setBri(1); } notFirstChange = true; } @@ -609,7 +609,7 @@ public class HueMulator { notFirstChange = true; } - if ((deviceState != null) && deviceState.isOn() && deviceState.getBri() <= 0) + if ((deviceState != null) && deviceState.isOn() && deviceState.getBri() <= 1) deviceState.setBri(254); // if((deviceState != null) && !deviceState.isOn() && (targetBri != null || @@ -1416,8 +1416,8 @@ public class HueMulator { int bri = 0; if (targetBriInc != null) { bri = state.getBri() - targetBriInc; - if (bri < 0) - bri = 0; + if (bri < 1) + bri = 1; } else if (targetBri != null) { bri = targetBri; } else { @@ -1440,8 +1440,8 @@ public class HueMulator { int bri = 0; if (targetBriInc != null) { bri = state.getBri() - targetBriInc; - if (bri < 0) - bri = 0; + if (bri < 1) + bri = 1; } else if (targetBri != null) { bri = targetBri; } else { diff --git a/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java b/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java index ec4a94e..af10115 100644 --- a/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java +++ b/src/test/java/com/bwssystems/color/test/ConvertCIEColorTestCase.java @@ -38,62 +38,6 @@ public class ConvertCIEColorTestCase { Assert.assertEquals(rgbDecode, assertDecode); } - @Test - public void testColorConverterHSLtoRGB() { - int[] rgb = ColorConverter.normalizeRGB(ColorConverter.HSLtoRGB(0.0f, 1.0f, 0.50000f)); - List rgbDecode = new ArrayList(); - rgbDecode.add(0, rgb[0]); - rgbDecode.add(1, rgb[1]); - rgbDecode.add(2, rgb[2]); - List assertDecode = new ArrayList(); - assertDecode.add(0, 255); - assertDecode.add(1, 0); - assertDecode.add(2, 0); - Assert.assertEquals(rgbDecode, assertDecode); - } - - @Test - public void testColorConverterRGBtoHSL() { - float[] rgb = ColorConverter.RGBtoHLS(255,0,0); - List rgbDecode = new ArrayList(); - rgbDecode.add(0, rgb[0]); - rgbDecode.add(1, rgb[1]); - rgbDecode.add(2, rgb[2]); - List assertDecode = new ArrayList(); - assertDecode.add(0, 0.0f); - assertDecode.add(1, 1.0f); - assertDecode.add(2, 0.50000f); - Assert.assertEquals(rgbDecode, assertDecode); - } - - @Test - public void testColorConverterHSLtoRGB2() { - int[] rgb = ColorConverter.normalizeRGB(ColorConverter.HSLtoRGB(0.57629f, 0.56299f, 0.50394f)); - List rgbDecode = new ArrayList(); - rgbDecode.add(0, rgb[0]); - rgbDecode.add(1, rgb[1]); - rgbDecode.add(2, rgb[2]); - List assertDecode = new ArrayList(); - assertDecode.add(0, 58); - assertDecode.add(1, 135); - assertDecode.add(2, 200); - Assert.assertEquals(rgbDecode, assertDecode); - } - - @Test - public void testColorConverterRGBtoHSL2() { - float[] rgb = ColorConverter.RGBtoHLS(58,135,200); - List rgbDecode = new ArrayList(); - rgbDecode.add(0, rgb[0]); - rgbDecode.add(1, rgb[1]); - rgbDecode.add(2, rgb[2]); - List assertDecode = new ArrayList(); - assertDecode.add(0, 0.57629f); - assertDecode.add(1, 0.56299f); - assertDecode.add(2, 0.50394f); - Assert.assertEquals(rgbDecode, assertDecode); - } - @Test public void testColorConversionXYtoRGB1() { ArrayList xy = new ArrayList(Arrays.asList(Double.parseDouble("0.671254"), Double.parseDouble("0.303273"))); @@ -127,32 +71,17 @@ public class ConvertCIEColorTestCase { } @Test - public void testColorConversionHSLtoRGB1() { - HueSatBri hsb = new HueSatBri(); - hsb.setHue(0); - hsb.setSat(254); - hsb.setBri((int)Math.round(0.50000 * 254)); - - List colorDecode = ColorDecode.convertHSLtoRGB(hsb); - List assertDecode = new ArrayList(); - assertDecode.add(0, 255); - assertDecode.add(1, 0); - assertDecode.add(2, 0); - Assert.assertEquals(colorDecode, assertDecode); - } - - @Test - public void testColorConversionHSLtoRGB2() { + public void testColorConversionHSBtoRGB1() { HueSatBri hsb = new HueSatBri(); hsb.setHue(37767); - hsb.setSat(143); + hsb.setSat(135); hsb.setBri(128); - List colorDecode = ColorDecode.convertHSLtoRGB(hsb); + List colorDecode = ColorDecode.convertHSBtoRGB(hsb); List assertDecode = new ArrayList(); - assertDecode.add(0, 58); - assertDecode.add(1, 135); - assertDecode.add(2, 200); + assertDecode.add(0, 60); + assertDecode.add(1, 97); + assertDecode.add(2, 128); Assert.assertEquals(colorDecode, assertDecode); }