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

Commit

Permalink
[Tradfri] Fix bug in color space conversion
Browse files Browse the repository at this point in the history
Ensure that calculated RGB values are not negative.

Fixes: #4349

Signed-off-by: Holger Reichert <mail@h0lger.de>
  • Loading branch information
hreichert committed Oct 2, 2017
1 parent 8bc1a0f commit d61ac8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Expand Up @@ -51,7 +51,7 @@ public void testFromCieKnownGood2() {
assertEquals(89, color.hsbType.getSaturation().intValue());
assertEquals(34, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromCieKnownGood3() {
TradfriColor color = TradfriColor.fromCie(19983, 37417, 1);
Expand All @@ -68,6 +68,22 @@ public void testFromCieKnownGood3() {
assertEquals(1, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromCieKnownGood4() {
TradfriColor color = TradfriColor.fromCie(11469, 3277, 181);
assertNotNull(color);
assertEquals(12, (int) color.rgbR);
assertEquals(0, (int) color.rgbG);
assertEquals(183, (int) color.rgbB);
assertEquals(11469, (int) color.xyX);
assertEquals(3277, (int) color.xyY);
assertEquals(181, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(245, color.hsbType.getHue().intValue());
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(72, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromHSBTypeKnownGood1() {
TradfriColor color = TradfriColor.fromHSBType(HSBType.RED);
Expand All @@ -83,7 +99,7 @@ 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"));
Expand Down
Expand Up @@ -124,6 +124,11 @@ public static TradfriColor fromCie(int xyX, int xyY, int xyBrightness) {
// green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, (1.0 / 2.4)) - 0.055;
// blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, (1.0 / 2.4)) - 0.055;

// calculated values can be slightly negative, so cap them to minimum 0.0
red = Math.max(0.0, red);
green = Math.max(0.0, green);
blue = Math.max(0.0, blue);

int redRounded = (int) Math.round(red * 255.0);
int greenRounded = (int) Math.round(green * 255.0);
int blueRounded = (int) Math.round(blue * 255.0);
Expand Down Expand Up @@ -162,7 +167,7 @@ public static TradfriColor fromHSBType(HSBType hsbType) {
// red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
// green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
// blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);

// Wide RGB D65 conversion
// math inspiration: http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
double X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
Expand Down Expand Up @@ -279,8 +284,7 @@ public static PercentType calculateColorTemperature(int xyX, int xyY) {
}
return new PercentType((int) Math.round(value * 100.0));
}



/**
* Converts the xyBrightness value to PercentType
*
Expand Down

0 comments on commit d61ac8d

Please sign in to comment.