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
Always cap calculated RGB values to the range 0 to 255.

Fixes: #4349

Signed-off-by: Holger Reichert <mail@h0lger.de>
  • Loading branch information
hreichert committed Sep 26, 2017
1 parent bb9f6f9 commit 0f74e56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Expand Up @@ -52,6 +52,22 @@ public void testFromCieKnownGood2() {
assertEquals(33, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromCieKnownGood3() {
TradfriColor color = TradfriColor.fromCie(11469, 3277, 181);
assertNotNull(color);
assertEquals(12, (int) color.rgbR); //
assertEquals(0, (int) color.rgbG);
assertEquals(181, (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(71, color.hsbType.getBrightness().intValue());
}

@Test
public void testFromHSBTypeKnownGood1() {
TradfriColor color = TradfriColor.fromHSBType(HSBType.RED);
Expand Down
Expand Up @@ -124,9 +124,9 @@ 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;

int redRounded = (int) Math.round(red * 255.0);
int greenRounded = (int) Math.round(green * 255.0);
int blueRounded = (int) Math.round(blue * 255.0);
int redRounded = ensureRgbRange((int) Math.round(red * 255.0));
int greenRounded = ensureRgbRange((int) Math.round(green * 255.0));
int blueRounded = ensureRgbRange((int) Math.round(blue * 255.0));

// construct hsbType from RGB values
// this hsbType has corrected values for RGB based on the hue/saturation/brightness values
Expand Down Expand Up @@ -162,7 +162,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 @@ -280,4 +280,16 @@ public static PercentType calculateColorTemperature(int xyX, int xyY) {
return new PercentType((int) Math.round(value * 100.0));
}

/**
* Ensure that the given value is in the RGB range (0 to 255).
* Negative values get capped to 0. Values over 255 get capped to 255.
*
* @param rgbValue the rgb value
* @return capped rgb value
*/
private static int ensureRgbRange(int rgbValue) {
// minimum rgb = 0, maximum rgb = 255
return Math.min(Math.max(rgbValue, 0), 255);
}

}

0 comments on commit 0f74e56

Please sign in to comment.