This repository has been archived by the owner on May 7, 2020. It is now read-only.
Add XY conversions to HSBType #4959
Merged
kaikreuzer
merged 2 commits into
eclipse-archived:master
from
cdjackson:hsbtype_enhance
Mar 20, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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>(); | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?