diff --git a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java index b9f26a2..3d39362 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java +++ b/src/main/java/com/bwssystems/HABridge/hue/ColorDecode.java @@ -255,4 +255,42 @@ public class ColorDecode { return "40" + String.format("%02X", milight) + "55"; } } + + @SuppressWarnings("unchecked") + public static int getIntRGB(ColorData colorData, int setIntensity) { + ColorData.ColorMode colorMode = colorData.getColorMode(); + List rgb = null; + if (colorMode == ColorData.ColorMode.XY) { + rgb = convertCIEtoRGB((List) colorData.getData(), setIntensity); + } else if (colorMode == ColorData.ColorMode.CT) { + rgb = convertCTtoRGB((Integer) colorData.getData()); + } + + double r = (double) rgb.get(0); + double g = (double) rgb.get(1); + double b = (double) rgb.get(2); + r /= (double) 0xFF; + g /= (double) 0xFF; + b /= (double) 0xFF; + double max = Math.max(Math.max(r, g), b), min = Math.min(Math.min(r, g), b); + double h = 0; + double d = max - min; + + if (max == min) { + h = 0; + } else { + if (max == r) { + h = ((g - b) / d + (g < b ? 6 : 0)); + } else if (max == g) { + h = ((b - r) / d + 2); + } else if (max == b) { + h = ((r - g) / d + 4); + } + h = Math.round(h * 60); + } + int rgbIntVal = (int) ((256 + 176 - Math.floor(h / 360.0 * 255.0)) % 256); + log.debug("Convert RGB to int. Result: " + rgbIntVal + " RGB Values: " + rgb.get(0) + " " + rgb.get(1) + " " + + rgb.get(2)); + return rgbIntVal; + } } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/lifx/LifxHome.java b/src/main/java/com/bwssystems/HABridge/plugins/lifx/LifxHome.java index 252e32a..cf3be27 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/lifx/LifxHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/lifx/LifxHome.java @@ -20,6 +20,7 @@ import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.dao.DeviceDescriptor; import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.ColorData; +import com.bwssystems.HABridge.hue.ColorDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.github.besherman.lifx.LFXClient; import com.github.besherman.lifx.LFXGroup; @@ -200,6 +201,10 @@ public class LifxHome implements Home { theValue = (float)0.99; theLight.setBrightness(theValue); } + if(colorData != null) { + int rgbVal = ColorDecode.getIntRGB(colorData, intensity); + theLight.setColor(rgbVal); + } } else if (theDevice.getType().equals(LifxDevice.GROUP_TYPE)) { LFXGroup theGroup = (LFXGroup)theDevice.getLifxObject(); if(body.contains("true"))