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

Add XY conversions to HSBType #4959

Merged
merged 2 commits into from Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -113,4 +113,17 @@ public void testConversionToPointType() {
assertEquals(null, new HSBType("100,100,100").as(PointType.class));
}

@Test
public void testConversionToXY() {
HSBType hsb = new HSBType("220,90,50");
PercentType[] xy = hsb.toXY();
assertEquals(new PercentType("16.969364"), xy[0]);
assertEquals(new PercentType("12.379659"), xy[1]);
}

@Test
public void testCreateFromXY() {
HSBType hsb = HSBType.fromXY(5f, 3f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it make sense to use the same values as above in order to demonstrate that they are actually inverse operations?

assertEquals(new HSBType("11,100,100"), hsb);
}
}
Expand Up @@ -46,6 +46,14 @@ public class HSBType extends PercentType implements ComplexType, State, Command
public static final HSBType GREEN = new HSBType("120,100,100");
public static final HSBType BLUE = new HSBType("240,100,100");

// 1931 CIE XYZ to sRGB (D65 reference white)
private static float Xy2Rgb[][] = { { 3.2406f, -1.5372f, -0.4986f }, { -0.9689f, 1.8758f, 0.0415f },
{ 0.0557f, -0.2040f, 1.0570f } };

// sRGB to 1931 CIE XYZ (D65 reference white)
private static float Rgb2Xy[][] = { { 0.4124f, 0.3576f, 0.1805f }, { 0.2126f, 0.7152f, 0.0722f },
{ 0.0193f, 0.1192f, 0.9505f } };

protected BigDecimal hue;
protected BigDecimal saturation;

Expand Down Expand Up @@ -120,6 +128,36 @@ public static HSBType fromRGB(int r, int g, int b) {
new PercentType((int) tmpBrightness));
}

/**
* Returns a HSBType object representing the provided xy color values in CIE XY color model.
* Conversion from CIE XY color model to sRGB using D65 reference white
* Returned color is set to full brightness
*
* @param x, y color information 0.0 - 1.0
*
* @return new HSBType object representing the given CIE XY color, full brightness
*/
public static HSBType fromXY(float x, float y) {
float Yo = 1.0f;
float X = (Yo / y) * x;
float Z = (Yo / y) * (1.0f - x - y);

float r = X * Xy2Rgb[0][0] + Yo * Xy2Rgb[0][1] + Z * Xy2Rgb[0][2];
float g = X * Xy2Rgb[1][0] + Yo * Xy2Rgb[1][1] + Z * Xy2Rgb[1][2];
float b = X * Xy2Rgb[2][0] + Yo * Xy2Rgb[2][1] + Z * Xy2Rgb[2][2];

float max = r > g ? r : g;
if (b > max) {
max = b;
}

r = gammaCompress(r / max);
g = gammaCompress(g / max);
b = gammaCompress(b / max);

return HSBType.fromRGB((int) (r * 255.0f + 0.5f), (int) (g * 255.0f + 0.5f), (int) (b * 255.0f + 0.5f));
}

@Override
public SortedMap<String, PrimitiveType> getConstituents() {
TreeMap<String, PrimitiveType> map = new TreeMap<String, PrimitiveType>();
Expand Down Expand Up @@ -265,6 +303,57 @@ public PercentType[] toRGB() {
return new PercentType[] { red, green, blue };
}

// Gamma compression (sRGB) for a single component, in the 0.0 - 1.0 range
private static float gammaCompress(float c) {
if (c < 0.0f) {
c = 0.0f;
} else if (c > 1.0f) {
c = 1.0f;
}

return c <= 0.0031308f ? 19.92f * c : (1.0f + 0.055f) * (float) Math.pow(c, 1.0f / 2.4f) - 0.055f;
}

// Gamma decompression (sRGB) for a single component, in the 0.0 - 1.0 range
private static float gammaDecompress(float c) {
if (c < 0.0f) {
c = 0.0f;
} else if (c > 1.0f) {
c = 1.0f;
}

return c <= 0.04045f ? c / 19.92f : (float) Math.pow((c + 0.055f) / (1.0f + 0.055f), 2.4f);
}

/**
* 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fromXY(...) method above takes float arguments, while toXY(...) here returns a PrecentType array. Is this asymmetry intended?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemeted this trying to be coherent with the existing fromRGB / toRGB couple of methods already in HSBtype (fromRGB takes ints, while toRGB returns Percentypes). Other than that, there is no particular reason. I would prefer to use only floats in the conversions, but it is like this just for consistency

// 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 int convertPercentToByte(PercentType percent) {
return percent.value.multiply(BigDecimal.valueOf(255))
.divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).intValue();
Expand Down
Expand Up @@ -27,144 +27,120 @@ public class TradfriColorTest {

@Test
public void testFromCieKnownGood1() {
TradfriColor color = TradfriColor.fromCie(29577, 12294, 354);
assertNotNull(color);
assertEquals(254, (int) color.rgbR);
assertEquals(2, (int) color.rgbG);
assertEquals(158, (int) color.rgbB);
TradfriColor color = new TradfriColor(29577, 12294, 354);
assertEquals(29577, (int) color.xyX);
assertEquals(12294, (int) color.xyY);
assertEquals(254, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(323, color.hsbType.getHue().intValue());
assertEquals(99, color.hsbType.getSaturation().intValue());
assertEquals(100, color.hsbType.getBrightness().intValue());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(321, hsbType.getHue().intValue());
assertEquals(100, hsbType.getSaturation().intValue());
assertEquals(100, hsbType.getBrightness().intValue());
}

@Test
public void testFromCieKnownGood2() {
TradfriColor color = TradfriColor.fromCie(19983, 37417, 84);
assertNotNull(color);
assertEquals(30, (int) color.rgbR);
assertEquals(86, (int) color.rgbG);
assertEquals(7, (int) color.rgbB);
TradfriColor color = new TradfriColor(19983, 37417, 84);
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(34, color.hsbType.getBrightness().intValue());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(115, hsbType.getHue().intValue());
assertEquals(77, hsbType.getSaturation().intValue());
assertEquals(34, 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);
TradfriColor color = new TradfriColor(19983, 37417, 1);
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());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(115, hsbType.getHue().intValue());
assertEquals(77, hsbType.getSaturation().intValue());
assertEquals(1, 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);
TradfriColor color = new TradfriColor(11413, 31334, 181);
assertEquals(11413, (int) color.xyX);
assertEquals(31334, (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());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(158, hsbType.getHue().intValue());
assertEquals(100, hsbType.getSaturation().intValue());
assertEquals(72, hsbType.getBrightness().intValue());
}

@Test
public void testFromHSBTypeKnownGood1() {
TradfriColor color = TradfriColor.fromHSBType(HSBType.RED);
assertNotNull(color);
assertEquals(254, (int) color.rgbR);
assertEquals(0, (int) color.rgbG);
assertEquals(0, (int) color.rgbB);
assertEquals(45914, (int) color.xyX);
assertEquals(19615, (int) color.xyY);
TradfriColor color = new TradfriColor(HSBType.RED);
assertEquals(41947, (int) color.xyX);
assertEquals(21625, (int) color.xyY);
assertEquals(254, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(0, color.hsbType.getHue().intValue());
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(100, color.hsbType.getBrightness().intValue());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(0, hsbType.getHue().intValue());
assertEquals(100, hsbType.getSaturation().intValue());
assertEquals(100, 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);
TradfriColor color = new TradfriColor(new HSBType("0,100,1"));
assertEquals(41947, (int) color.xyX);
assertEquals(21625, (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());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(0, hsbType.getHue().intValue());
assertEquals(100, hsbType.getSaturation().intValue());
assertEquals(1, hsbType.getBrightness().intValue());
}

@Test
public void testConversionReverse() {
// convert from HSBType
TradfriColor color = TradfriColor.fromHSBType(HSBType.GREEN);
assertNotNull(color);
assertEquals(0, (int) color.rgbR);
assertEquals(254, (int) color.rgbG); // 254 instead of 255 - only approximated calculation
assertEquals(0, (int) color.rgbB);
assertEquals(11299, (int) color.xyX);
assertEquals(48941, (int) color.xyY);
TradfriColor color = new TradfriColor(HSBType.GREEN);
assertEquals(19660, (int) color.xyX);
assertEquals(39321, (int) color.xyY);
assertEquals(254, (int) color.brightness);
assertNotNull(color.hsbType);
assertEquals(120, color.hsbType.getHue().intValue());
assertEquals(100, color.hsbType.getSaturation().intValue());
assertEquals(100, color.hsbType.getBrightness().intValue());
HSBType hsbType = color.getHSB();
assertNotNull(hsbType);
assertEquals(120, hsbType.getHue().intValue());
assertEquals(100, hsbType.getSaturation().intValue());
assertEquals(100, hsbType.getBrightness().intValue());
// convert the result again based on the XY values
TradfriColor reverse = TradfriColor.fromCie(color.xyX, color.xyY, color.brightness);
assertNotNull(reverse);
assertEquals(0, (int) reverse.rgbR);
assertEquals(254, (int) reverse.rgbG);
assertEquals(0, (int) reverse.rgbB);
assertEquals(11299, (int) reverse.xyX);
assertEquals(48941, (int) reverse.xyY);
TradfriColor reverse = new TradfriColor(color.xyX, color.xyY, color.brightness);
assertEquals(19660, (int) reverse.xyX);
assertEquals(39321, (int) reverse.xyY);
assertEquals(254, (int) reverse.brightness);
assertNotNull(reverse.hsbType);
assertEquals(120, reverse.hsbType.getHue().intValue());
assertEquals(100, reverse.hsbType.getSaturation().intValue());
assertEquals(100, reverse.hsbType.getBrightness().intValue());
HSBType hsbTypeReverse = color.getHSB();
assertNotNull(hsbTypeReverse);
assertEquals(120, hsbTypeReverse.getHue().intValue());
assertEquals(100, hsbTypeReverse.getSaturation().intValue());
assertEquals(100, hsbTypeReverse.getBrightness().intValue());
}

@Test
public void testFromColorTemperatureMinMiddleMax() {
// coldest color temperature -> preset 1
TradfriColor colorMin = TradfriColor.fromColorTemperature(PercentType.ZERO);
TradfriColor colorMin = new TradfriColor(PercentType.ZERO);
assertNotNull(colorMin);
assertEquals(24933, (int) colorMin.xyX);
assertEquals(24691, (int) colorMin.xyY);
// middle color temperature -> preset 2
TradfriColor colorMiddle = TradfriColor.fromColorTemperature(new PercentType(50));
TradfriColor colorMiddle = new TradfriColor(new PercentType(50));
assertNotNull(colorMiddle);
assertEquals(30138, (int) colorMiddle.xyX);
assertEquals(26909, (int) colorMiddle.xyY);
// warmest color temperature -> preset 3
TradfriColor colorMax = TradfriColor.fromColorTemperature(PercentType.HUNDRED);
TradfriColor colorMax = new TradfriColor(PercentType.HUNDRED);
assertNotNull(colorMax);
assertEquals(33137, (int) colorMax.xyX);
assertEquals(27211, (int) colorMax.xyY);
Expand All @@ -173,12 +149,12 @@ public void testFromColorTemperatureMinMiddleMax() {
@Test
public void testFromColorTemperatureInbetween() {
// 30 percent must be between preset 1 and 2
TradfriColor color2 = TradfriColor.fromColorTemperature(new PercentType(30));
TradfriColor color2 = new TradfriColor(new PercentType(30));
assertNotNull(color2);
assertEquals(28056, (int) color2.xyX);
assertEquals(26022, (int) color2.xyY);
// 70 percent must be between preset 2 and 3
TradfriColor color3 = TradfriColor.fromColorTemperature(new PercentType(70));
TradfriColor color3 = new TradfriColor(new PercentType(70));
assertNotNull(color3);
assertEquals(31338, (int) color3.xyX);
assertEquals(27030, (int) color3.xyY);
Expand All @@ -187,25 +163,25 @@ public void testFromColorTemperatureInbetween() {
@Test
public void testCalculateColorTemperature() {
// preset 1 -> coldest -> 0 percent
PercentType preset1 = TradfriColor.calculateColorTemperature(24933, 24691);
PercentType preset1 = new TradfriColor(24933, 24691, null).getColorTemperature();
assertEquals(0, preset1.intValue());
// preset 2 -> middle -> 50 percent
PercentType preset2 = TradfriColor.calculateColorTemperature(30138, 26909);
PercentType preset2 = new TradfriColor(30138, 26909, null).getColorTemperature();
assertEquals(50, preset2.intValue());
// preset 3 -> warmest -> 100 percent
PercentType preset3 = TradfriColor.calculateColorTemperature(33137, 27211);
PercentType preset3 = new TradfriColor(33137, 27211, null).getColorTemperature();
assertEquals(100, preset3.intValue());
// preset 3 -> warmest -> 100 percent
PercentType colder = TradfriColor.calculateColorTemperature(22222, 23333);
PercentType colder = new TradfriColor(22222, 23333, null).getColorTemperature();
assertEquals(0, colder.intValue());
// preset 3 -> warmest -> 100 percent
PercentType temp3 = TradfriColor.calculateColorTemperature(34000, 34000);
PercentType temp3 = new TradfriColor(34000, 34000, null).getColorTemperature();
assertEquals(100, temp3.intValue());
// mixed case 1
PercentType mixed1 = TradfriColor.calculateColorTemperature(0, 1000000);
PercentType mixed1 = new TradfriColor(0, 1000000, null).getColorTemperature();
assertEquals(0, mixed1.intValue());
// mixed case 1
PercentType mixed2 = TradfriColor.calculateColorTemperature(1000000, 0);
PercentType mixed2 = new TradfriColor(1000000, 0, null).getColorTemperature();
assertEquals(100, mixed2.intValue());
}

Expand Down