public static HSBType valueOf(String value) { return new HSBType(value); }
@Override public <T extends State> @Nullable T as(@Nullable Class<T> target) { if (target == OnOffType.class) { return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON); } else if (target == PercentType.class) { return target.cast(new PercentType(toBigDecimal().multiply(BigDecimal.valueOf(100)))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 0) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 0) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(this.toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else { return defaultConversion(target); } }
@Override public <T extends State> @Nullable T as(@Nullable Class<T> target) { if (target == OnOffType.class) { return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON); } else if (target == DecimalType.class) { return target.cast(new DecimalType(toBigDecimal().divide(BigDecimal.valueOf(100), 8, RoundingMode.UP))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (equals(HUNDRED)) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (equals(HUNDRED)) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, this)); } else if (target == QuantityType.class) { return target.cast(new QuantityType<>(toBigDecimal().doubleValue(), SmartHomeUnits.PERCENT)); } else { return defaultConversion(target); } }
return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(this.toBigDecimal().multiply(HUNDRED)))); } else if (target == PercentType.class) {
applyState(new HSBType(hue, saturation, PercentType.ZERO)); } else if (state == OnOffType.ON) { applyState(new HSBType(hue, saturation, PercentType.HUNDRED)); } else if (state instanceof PercentType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, (PercentType) state)); } else if (state instanceof DecimalType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, new PercentType(((DecimalType) state).toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else {
/** * Returns the xyY values representing this object's color in CIE XY color model. * Conversion from sRGB to CIE XY using D65 reference white * xy pair contains color information * Y represents relative luminance * * @param HSBType color object * @return PercentType[x, y, Y] values in the CIE XY color model */ public PercentType[] toXY() { // This makes sure we keep color information even if brightness is zero PercentType sRGB[] = new HSBType(getHue(), getSaturation(), PercentType.HUNDRED).toRGB(); float r = gammaDecompress(sRGB[0].floatValue() / 100.0f); float g = gammaDecompress(sRGB[1].floatValue() / 100.0f); float b = gammaDecompress(sRGB[2].floatValue() / 100.0f); float X = r * Rgb2Xy[0][0] + g * Rgb2Xy[0][1] + b * Rgb2Xy[0][2]; float Y = r * Rgb2Xy[1][0] + g * Rgb2Xy[1][1] + b * Rgb2Xy[1][2]; float Z = r * Rgb2Xy[2][0] + g * Rgb2Xy[2][1] + b * Rgb2Xy[2][2]; float x = X / (X + Y + Z); float y = Y / (X + Y + Z); return new PercentType[] { new PercentType(Float.valueOf(x * 100.0f).toString()), new PercentType(Float.valueOf(y * 100.0f).toString()), new PercentType(Float.valueOf(Y * getBrightness().floatValue()).toString()) }; }
return new HSBType(new DecimalType((int) tmpHue), new PercentType((int) tmpSaturation), new PercentType((int) tmpBrightness));
public static HSBType valueOf(String value) { return new HSBType(value); }
private void updateColorHSB(DecimalType hue, PercentType saturation) { // Extra temp variable to avoid thread sync concurrency issues on currentHSB HSBType oldHSB = currentHSB; HSBType newHSB = new HSBType(hue, saturation, oldHSB.getBrightness()); currentHSB = newHSB; if (currentState.get() && lastColorMode != ColorModeEnum.COLORTEMPERATURE) { updateChannelState(newHSB); } }
private void updateBrightness(PercentType brightness) { // Extra temp variable to avoid thread sync concurrency issues on currentHSB HSBType oldHSB = currentHSB; HSBType newHSB = new HSBType(oldHSB.getHue(), oldHSB.getSaturation(), brightness); currentHSB = newHSB; lastBrightness = brightness; if (currentState.get() && lastColorMode != ColorModeEnum.COLORTEMPERATURE) { updateChannelState(newHSB); } }
@Override public <T extends State> @Nullable T as(@Nullable Class<T> target) { if (target == OnOffType.class) { return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON); } else if (target == PercentType.class) { return target.cast(new PercentType(toBigDecimal().multiply(BigDecimal.valueOf(100)))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 0) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 0) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(this.toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else { return defaultConversion(target); } }
@Override public <T extends State> @Nullable T as(@Nullable Class<T> target) { if (target == OnOffType.class) { return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON); } else if (target == DecimalType.class) { return target.cast(new DecimalType(toBigDecimal().divide(BigDecimal.valueOf(100), 8, RoundingMode.UP))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (equals(HUNDRED)) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (equals(HUNDRED)) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, this)); } else if (target == QuantityType.class) { return target.cast(new QuantityType<>(toBigDecimal().doubleValue(), SmartHomeUnits.PERCENT)); } else { return defaultConversion(target); } }
private void changeOnOff(OnOffType onoff) throws InterruptedException, ExecutionException { boolean on = onoff == OnOffType.ON; PercentType brightness = on ? PercentType.HUNDRED : PercentType.ZERO; if (clusterLevelControl != null) { changeBrightness(brightness); return; } if (clusterOnOff == null) { logger.warn("{}: ignoring on/off command", endpoint.getIeeeAddress()); return; } HSBType oldHSB = currentHSB; currentHSB = new HSBType(oldHSB.getHue(), oldHSB.getSaturation(), brightness); lastBrightness = brightness; if (on) { clusterOnOff.onCommand().get(); } else { clusterOnOff.offCommand().get(); } }
return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(this.toBigDecimal().multiply(HUNDRED)))); } else if (target == PercentType.class) {
applyState(new HSBType(hue, saturation, PercentType.ZERO)); } else if (state == OnOffType.ON) { applyState(new HSBType(hue, saturation, PercentType.HUNDRED)); } else if (state instanceof PercentType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, (PercentType) state)); } else if (state instanceof DecimalType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, new PercentType(((DecimalType) state).toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else {
/** * Returns the xyY values representing this object's color in CIE XY color model. * Conversion from sRGB to CIE XY using D65 reference white * xy pair contains color information * Y represents relative luminance * * @param HSBType color object * @return PercentType[x, y, Y] values in the CIE XY color model */ public PercentType[] toXY() { // This makes sure we keep color information even if brightness is zero PercentType sRGB[] = new HSBType(getHue(), getSaturation(), PercentType.HUNDRED).toRGB(); float r = gammaDecompress(sRGB[0].floatValue() / 100.0f); float g = gammaDecompress(sRGB[1].floatValue() / 100.0f); float b = gammaDecompress(sRGB[2].floatValue() / 100.0f); float X = r * Rgb2Xy[0][0] + g * Rgb2Xy[0][1] + b * Rgb2Xy[0][2]; float Y = r * Rgb2Xy[1][0] + g * Rgb2Xy[1][1] + b * Rgb2Xy[1][2]; float Z = r * Rgb2Xy[2][0] + g * Rgb2Xy[2][1] + b * Rgb2Xy[2][2]; float x = X / (X + Y + Z); float y = Y / (X + Y + Z); return new PercentType[] { new PercentType(Float.valueOf(x * 100.0f).toString()), new PercentType(Float.valueOf(y * 100.0f).toString()), new PercentType(Float.valueOf(Y * getBrightness().floatValue()).toString()) }; }
private void changeColorHueSaturation(HSBType color) throws InterruptedException, ExecutionException { HSBType oldHSB = currentHSB; currentHSB = new HSBType(color.getHue(), color.getSaturation(), oldHSB.getBrightness()); int hue = (int) (color.getHue().floatValue() * 254.0f / 360.0f + 0.5f); int saturation = percentToLevel(color.getSaturation()); clusterColorControl .moveToHueAndSaturationCommand(hue, saturation, configLevelControl.getDefaultTransitionTime()).get(); }
private void changeColorXY(HSBType color) throws InterruptedException, ExecutionException { PercentType xy[] = color.toXY(); HSBType oldHSB = currentHSB; currentHSB = new HSBType(color.getHue(), color.getSaturation(), oldHSB.getBrightness()); logger.debug("{}: Change Color HSV ({}, {}, {}) -> XY ({}, {})", endpoint.getIeeeAddress(), color.getHue(), color.getSaturation(), oldHSB.getBrightness(), xy[0], xy[1]); int x = (int) (xy[0].floatValue() / 100.0f * 65536.0f + 0.5f); // up to 65279 int y = (int) (xy[1].floatValue() / 100.0f * 65536.0f + 0.5f); // up to 65279 clusterColorControl.moveToColorCommand(x, y, configLevelControl.getDefaultTransitionTime()).get(); }
private void changeBrightness(PercentType brightness) throws InterruptedException, ExecutionException { if (clusterLevelControl == null) { if (clusterOnOff != null) { changeOnOff(brightness.intValue() == 0 ? OnOffType.OFF : OnOffType.ON); } else { logger.warn("{}: ignoring brightness command", endpoint.getIeeeAddress()); } return; } HSBType oldHSB = currentHSB; currentHSB = new HSBType(oldHSB.getHue(), oldHSB.getSaturation(), brightness); lastBrightness = brightness; int level = percentToLevel(brightness); if (clusterOnOff != null) { if (brightness.equals(PercentType.ZERO)) { clusterOnOff.offCommand(); } else { clusterLevelControl.moveToLevelWithOnOffCommand(level, configLevelControl.getDefaultTransitionTime()) .get(); } } else { clusterLevelControl.moveToLevelCommand(level, configLevelControl.getDefaultTransitionTime()).get(); } }
return new HSBType(new DecimalType((int) tmpHue), new PercentType((int) tmpSaturation), new PercentType((int) tmpBrightness));