[hue] Changed default color mode for color commands to XY (#10608)
* Changed default color mode for color commands to XY Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de> * Incorporated comments from review Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
f652e329f7
commit
ea4315adaf
@ -68,8 +68,10 @@ public class LightStateConverter {
|
|||||||
* @return light state representing the {@link HSBType}.
|
* @return light state representing the {@link HSBType}.
|
||||||
*/
|
*/
|
||||||
public static StateUpdate toColorLightState(HSBType hsbType, State lightState) {
|
public static StateUpdate toColorLightState(HSBType hsbType, State lightState) {
|
||||||
StateUpdate stateUpdate = ColorMode.XY.equals(lightState.getColorMode()) ? toXYColorLightState(hsbType)
|
// XY color is the implicit default: Use XY color mode if i) no color mode is set or ii) if the bulb is in
|
||||||
: toHSBColorLightState(hsbType);
|
// CT mode or iii) already in XY mode. Only if the bulb is in HS mode, use this one.
|
||||||
|
StateUpdate stateUpdate = ColorMode.HS.equals(lightState.getColorMode()) ? toHSBColorLightState(hsbType)
|
||||||
|
: toXYColorLightState(hsbType);
|
||||||
|
|
||||||
int brightness = (int) Math.floor(hsbType.getBrightness().doubleValue() * BRIGHTNESS_FACTOR);
|
int brightness = (int) Math.floor(hsbType.getBrightness().doubleValue() * BRIGHTNESS_FACTOR);
|
||||||
if (brightness > 0) {
|
if (brightness > 0) {
|
||||||
|
|||||||
@ -70,7 +70,7 @@ public class LightStateConverterTest {
|
|||||||
final State lightState = new State();
|
final State lightState = new State();
|
||||||
// 0 percent should not be sent to the Hue interface
|
// 0 percent should not be sent to the Hue interface
|
||||||
StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(PercentType.ZERO);
|
StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(PercentType.ZERO);
|
||||||
assertThat(stateUpdate.commands.size(), is(1));
|
assertThat(stateUpdate.commands, hasSize(1));
|
||||||
// a brightness of 0 should result in 0 percent
|
// a brightness of 0 should result in 0 percent
|
||||||
lightState.bri = 0;
|
lightState.bri = 0;
|
||||||
assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
|
assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
|
||||||
@ -81,7 +81,7 @@ public class LightStateConverterTest {
|
|||||||
final State lightState = new State();
|
final State lightState = new State();
|
||||||
for (int percent = 1; percent <= 100; ++percent) {
|
for (int percent = 1; percent <= 100; ++percent) {
|
||||||
StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(new PercentType(percent));
|
StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(new PercentType(percent));
|
||||||
assertThat(stateUpdate.commands.size(), is(2));
|
assertThat(stateUpdate.commands, hasSize(2));
|
||||||
assertThat(stateUpdate.commands.get(1).key, is("bri"));
|
assertThat(stateUpdate.commands.get(1).key, is("bri"));
|
||||||
lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
|
lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
|
||||||
assertThat(LightStateConverter.toBrightnessPercentType(lightState).intValue(), is(percent));
|
assertThat(LightStateConverter.toBrightnessPercentType(lightState).intValue(), is(percent));
|
||||||
@ -105,7 +105,7 @@ public class LightStateConverterTest {
|
|||||||
// 0 percent should not be sent to the Hue interface
|
// 0 percent should not be sent to the Hue interface
|
||||||
final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, PercentType.ZERO);
|
final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, PercentType.ZERO);
|
||||||
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
assertThat(stateUpdate.commands.size(), is(2));
|
assertThat(stateUpdate.commands, hasSize(1));
|
||||||
// a brightness of 0 should result in 0 percent
|
// a brightness of 0 should result in 0 percent
|
||||||
lightState.bri = 0;
|
lightState.bri = 0;
|
||||||
assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
|
assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
|
||||||
@ -118,9 +118,9 @@ public class LightStateConverterTest {
|
|||||||
for (int percent = 1; percent <= 100; ++percent) {
|
for (int percent = 1; percent <= 100; ++percent) {
|
||||||
final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(percent));
|
final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(percent));
|
||||||
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
assertThat(stateUpdate.commands.size(), is(3));
|
assertThat(stateUpdate.commands, hasSize(2));
|
||||||
assertThat(stateUpdate.commands.get(2).key, is("bri"));
|
assertThat(stateUpdate.commands.get(1).key, is("bri"));
|
||||||
lightState.bri = Integer.parseInt(stateUpdate.commands.get(2).value.toString());
|
lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
|
||||||
assertThat(LightStateConverter.toHSBType(lightState).getBrightness().intValue(), is(percent));
|
assertThat(LightStateConverter.toHSBType(lightState).getBrightness().intValue(), is(percent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,11 +149,11 @@ public class LightStateConverterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void colorLightStateConverterForSaturationConversionIsBijective() {
|
public void colorLightStateConverterForSaturationConversionIsBijective() {
|
||||||
final State lightState = new State();
|
final State lightState = new State();
|
||||||
lightState.colormode = ColorMode.CT.toString();
|
lightState.colormode = ColorMode.HS.toString();
|
||||||
for (int percent = 0; percent <= 100; ++percent) {
|
for (int percent = 0; percent <= 100; ++percent) {
|
||||||
final HSBType hsbType = new HSBType(DecimalType.ZERO, new PercentType(percent), PercentType.HUNDRED);
|
final HSBType hsbType = new HSBType(DecimalType.ZERO, new PercentType(percent), PercentType.HUNDRED);
|
||||||
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
assertThat(stateUpdate.commands.size(), is(3));
|
assertThat(stateUpdate.commands, hasSize(3));
|
||||||
assertThat(stateUpdate.commands.get(1).key, is("sat"));
|
assertThat(stateUpdate.commands.get(1).key, is("sat"));
|
||||||
lightState.sat = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
|
lightState.sat = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
|
||||||
assertThat(LightStateConverter.toHSBType(lightState).getSaturation().intValue(), is(percent));
|
assertThat(LightStateConverter.toHSBType(lightState).getSaturation().intValue(), is(percent));
|
||||||
@ -163,16 +163,43 @@ public class LightStateConverterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void colorLightStateConverterForHueConversionIsBijective() {
|
public void colorLightStateConverterForHueConversionIsBijective() {
|
||||||
final State lightState = new State();
|
final State lightState = new State();
|
||||||
|
lightState.colormode = ColorMode.HS.toString();
|
||||||
for (int hue = 0; hue < 360; ++hue) {
|
for (int hue = 0; hue < 360; ++hue) {
|
||||||
final HSBType hsbType = new HSBType(new DecimalType(hue), PercentType.HUNDRED, PercentType.HUNDRED);
|
final HSBType hsbType = new HSBType(new DecimalType(hue), PercentType.HUNDRED, PercentType.HUNDRED);
|
||||||
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
assertThat(stateUpdate.commands.size(), is(3));
|
assertThat(stateUpdate.commands, hasSize(3));
|
||||||
assertThat(stateUpdate.commands.get(0).key, is("hue"));
|
assertThat(stateUpdate.commands.get(0).key, is("hue"));
|
||||||
lightState.hue = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
|
lightState.hue = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
|
||||||
assertThat(LightStateConverter.toHSBType(lightState).getHue().intValue(), is(hue));
|
assertThat(LightStateConverter.toHSBType(lightState).getHue().intValue(), is(hue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void colorLightStateConverterColorModeSelection() {
|
||||||
|
final State lightState = new State();
|
||||||
|
final HSBType hsbType = new HSBType(PercentType.HUNDRED, PercentType.HUNDRED, PercentType.HUNDRED);
|
||||||
|
|
||||||
|
lightState.colormode = null;
|
||||||
|
StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
|
assertThat(stateUpdate.commands, hasSize(2));
|
||||||
|
assertThat(stateUpdate.commands.get(0).key, is("xy"));
|
||||||
|
|
||||||
|
lightState.colormode = ColorMode.CT.toString();
|
||||||
|
stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
|
assertThat(stateUpdate.commands, hasSize(2));
|
||||||
|
assertThat(stateUpdate.commands.get(0).key, is("xy"));
|
||||||
|
|
||||||
|
lightState.colormode = ColorMode.HS.toString();
|
||||||
|
stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
|
assertThat(stateUpdate.commands, hasSize(3));
|
||||||
|
assertThat(stateUpdate.commands.get(0).key, is("hue"));
|
||||||
|
|
||||||
|
lightState.colormode = ColorMode.XY.toString();
|
||||||
|
stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
|
||||||
|
assertThat(stateUpdate.commands, hasSize(2));
|
||||||
|
assertThat(stateUpdate.commands.get(0).key, is("xy"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void hsbSaturationAlwaysGreaterThanZero() {
|
public void hsbSaturationAlwaysGreaterThanZero() {
|
||||||
final State lightState = new State();
|
final State lightState = new State();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user