Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Improve handling of brightness value for Color Lights (#4344)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivaylo Ivanov <ivivanov.bg@gmail.com>
  • Loading branch information
ivivanov-bg authored and kaikreuzer committed Sep 28, 2017
1 parent dbf4bf8 commit 21f9b2b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
Expand Up @@ -22,7 +22,7 @@ public class TradfriColorTest {

@Test
public void testFromCieKnownGood1() {
TradfriColor color = TradfriColor.fromCie(29577, 12294, 254);
TradfriColor color = TradfriColor.fromCie(29577, 12294, 354);
assertNotNull(color);
assertEquals(254, (int) color.rgbR);
assertEquals(2, (int) color.rgbG);
Expand All @@ -41,15 +41,31 @@ public void testFromCieKnownGood2() {
TradfriColor color = TradfriColor.fromCie(19983, 37417, 84);
assertNotNull(color);
assertEquals(30, (int) color.rgbR);
assertEquals(84, (int) color.rgbG);
assertEquals(86, (int) color.rgbG);
assertEquals(7, (int) color.rgbB);
assertEquals(19983, (int) color.xyX);
assertEquals(37417, (int) color.xyY);
assertEquals(84, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(102, color.hsbType.getHue().intValue());
assertEquals(89, color.hsbType.getSaturation().intValue());
assertEquals(33, color.hsbType.getBrightness().intValue());
assertEquals(34, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromCieKnownGood3() {
TradfriColor color = TradfriColor.fromCie(19983, 37417, 1);
assertNotNull(color);
assertEquals(0, (int) color.rgbR);
assertEquals(2, (int) color.rgbG);
assertEquals(0, (int) color.rgbB);
assertEquals(19983, (int) color.xyX);
assertEquals(37417, (int) color.xyY);
assertEquals(1, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(120, color.hsbType.getHue().intValue());
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(1, color.hsbType.getBrightness().intValue());
}

@Test
Expand All @@ -67,6 +83,22 @@ public void testFromHSBTypeKnownGood1() {
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(100, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromHSBTypeKnownGood2() {
TradfriColor color = TradfriColor.fromHSBType(new HSBType("0,100,1"));
assertNotNull(color);
assertEquals(2, (int) color.rgbR);
assertEquals(0, (int) color.rgbG);
assertEquals(0, (int) color.rgbB);
assertEquals(45914, (int) color.xyX);
assertEquals(19615, (int) color.xyY);
assertEquals(2, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(0, color.hsbType.getHue().intValue());
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(1, color.hsbType.getBrightness().intValue());
}

@Test
public void testConversionReverse() {
Expand Down
Expand Up @@ -346,16 +346,14 @@ public LightData setBrightness(PercentType brightness) {
}

public PercentType getBrightness() {
PercentType result = null;

JsonElement dimmer = attributes.get(DIMMER);
if (dimmer != null) {
int b = dimmer.getAsInt();
if (b == 1) {
return new PercentType(1);
}
return new PercentType((int) Math.round(b / 2.54));
} else {
return null;
}
result = TradfriColor.xyBrightnessToPercentType(dimmer.getAsInt());
}

return result;
}

public boolean getReachabilityStatus() {
Expand Down
Expand Up @@ -245,8 +245,8 @@ private static HSBType constructHsbTypeFromRgbWithBrightnessPercent(int rgbR, in
int xyBrightness) {
// construct HSBType from RGB values
HSBType hsbFullBright = HSBType.fromRGB(rgbR, rgbG, rgbB);
// get hue and saturation from HSBType and construct new HSBType based on these values with the given brightbess
PercentType brightnessPercent = new PercentType((int) (xyBrightness / 2.54));
// get hue and saturation from HSBType and construct new HSBType based on these values with the given brightness
PercentType brightnessPercent = xyBrightnessToPercentType(xyBrightness);
HSBType hsb = new HSBType(hsbFullBright.getHue(), hsbFullBright.getSaturation(), brightnessPercent);
return hsb;
}
Expand Down Expand Up @@ -279,5 +279,21 @@ public static PercentType calculateColorTemperature(int xyX, int xyY) {
}
return new PercentType((int) Math.round(value * 100.0));
}


/**
* Converts the xyBrightness value to PercentType
*
* @param xyBrightness xy brightness level 0 to 254
* @return {@link PercentType} with brightness level (0 = light is off, 1 = lowest, 100 = highest)
*/
public static PercentType xyBrightnessToPercentType(int xyBrightness) {
if (xyBrightness > 254) {
xyBrightness = 254;
} else if (xyBrightness < 0) {
xyBrightness = 0;
}
return new PercentType((int) Math.ceil(xyBrightness / 2.54));
}

}

0 comments on commit 21f9b2b

Please sign in to comment.