Package org.openhab.core.util
Class ColorUtil
java.lang.Object
org.openhab.core.util.ColorUtil
The
ColorUtil
is responsible for converting different color formats.
The implementation of HSB/CIE conversion is based work from Erik Baauw for the
Homebridge
project.- Author:
- Jan N. Klug - Initial contribution, Holger Friedrich - Transfer RGB color conversion from HSBType, improve RGB conversion, restructuring, Chris Jackson - Added fromRGB (moved from HSBType), Andrew Fiddian-Green - Extensive revamp to fix bugs and improve accuracy, Cody Cutrer - Added xyToDuv
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
Color gamutstatic class
Class for points in the CIE xy color space -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]
static PercentType[]
hsbToRgbPercent
(HSBType hsb) static int[]
static PercentType[]
hsbToRgbwPercent
(HSBType hsb) static int
static double[]
static double[]
hsbToXY
(HSBType hsb, ColorUtil.Gamut gamut) static double[]
kelvinToXY
(double kelvin) Convert a colour temperature in Kelvin to a point in the CIE XY colour space.static HSBType
rgbToHsb
(int[] rgbw) static HSBType
rgbToHsb
(PercentType[] rgbw) static double
xyToDuv
(double[] xy) Calculate the Duv (Delta u,v) metric from a CIE 1931xy
format color.static HSBType
xyToHsb
(double[] xy) static HSBType
xyToHsb
(double[] xyY, ColorUtil.Gamut gamut) static double
xyToKelvin
(double[] xy) Convert a point in the CIE XY colour space to a colour temperature in Kelvin.
-
Field Details
-
DEFAULT_GAMUT
-
-
Method Details
-
hsbToRgb
Transform HSV basedHSBType
to sRGB. This function does rounding to integer valued components. It is the preferred way of doing HSB to RGB conversion. See also:hsbToRgbPercent(HSBType)
,hsbToRgbw(HSBType)
,hsbTosRgb(HSBType)
- Parameters:
hsb
- anHSBType
value.- Returns:
- array of three int with the RGB values in the range 0 to 255.
-
hsbToRgbw
Transform HSV basedHSBType
to sRGB. This function does rounding to integer valued components. It is the preferred way of doing HSB to RGBW conversion. See also:hsbToRgbPercent(HSBType)
,hsbToRgbwPercent(HSBType)
,hsbTosRgb(HSBType)
- Parameters:
hsb
- anHSBType
value.- Returns:
- array of four int with the RGBW values in the range 0 to 255.
-
hsbToRgbPercent
Transform HSV basedHSBType
to sRGB. This function does not round the components. For conversion to integer values in the range 0 to 255 usehsbToRgb(HSBType)
. See also:hsbToRgb(HSBType)
,hsbTosRgb(HSBType)
,hsbToRgbwPercent(HSBType)
- Parameters:
hsb
- anHSBType
value.- Returns:
- array of three
PercentType
with the RGB values in the range 0 to 100 percent.
-
hsbToRgbwPercent
Transform HSV basedHSBType
to RGBW. See Converting RGB to RGBW. This function does not round the components. For conversion to integer values in the range 0 to 255 usehsbToRgb(HSBType)
. See also:hsbToRgb(HSBType)
,hsbTosRgb(HSBType)
,hsbToRgbPercent(HSBType)
- Parameters:
hsb
- anHSBType
value.- Returns:
- array of four
PercentType
with the RGBW values in the range 0 to 100 percent.
-
hsbTosRgb
Transform HSV basedHSBType
to the RGB value representing the color in the default sRGB color model. (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue). See also:hsbToRgb(HSBType)
,hsbToRgbPercent(HSBType)
- Parameters:
hsb
- anHSBType
value.- Returns:
- the RGB value of the color in the default sRGB color model.
-
hsbToXY
- Parameters:
hsb
- anHSBType
value.- Returns:
- array of three double with the closest matching CIE 1931 x,y,Y in the range 0.0000 to 1.0000
-
hsbToXY
- Parameters:
hsb
- anHSBType
value.gamut
- the color Gamut supported by the light.- Returns:
- array of three or four double with the closest matching CIE 1931 x,y,Y in the range 0.0000 to 1.0000 - plus an optional extra empty element to flag if the xyY result has been forced inside the given Gamut.
-
rgbToHsb
- Parameters:
rgbw
- array of three or four int with the RGB(W) values in the range 0 to 255.- Returns:
- the corresponding
HSBType
. - Throws:
IllegalArgumentException
- when input array has wrong size or exceeds allowed value range.
-
rgbToHsb
- Parameters:
rgbw
- array of three or fourPercentType
with the RGB(W) values in the range 0 to 100 percent.- Returns:
- the corresponding
HSBType
. - Throws:
IllegalArgumentException
- when input array has wrong size or exceeds allowed value range.
-
xyToHsb
- Parameters:
xy
- array of double with CIE 1931 x,y[,Y] in the range 0.0000 to 1.0000Y
value is optional.- Returns:
- the corresponding
HSBType
. - Throws:
IllegalArgumentException
- when input array has wrong size or exceeds allowed value range.
-
xyToHsb
- Parameters:
xyY
- array of double with CIE 1931 x,y[,Y] in the range 0.0000 to 1.0000Y
value is optional.gamut
- the color Gamut supported by the light.- Returns:
- the corresponding
HSBType
. - Throws:
IllegalArgumentException
- when input array has wrong size or exceeds allowed value range
-
xyToDuv
Calculate the Duv (Delta u,v) metric from a CIE 1931xy
format color. Duv describes the distance of a color point from the black body curve. It's useful for calculating if a color is near to "white", at any color temperature.- Parameters:
xy
- array of double with CIE 1931 x,y in the range 0.0000 to 1.0000- Returns:
- the calculated Duv metric
- Throws:
IllegalArgumentException
- when input array has wrong size or exceeds allowed value range.
-
kelvinToXY
Convert a colour temperature in Kelvin to a point in the CIE XY colour space. Uses a lookup table as described here.- Parameters:
kelvin
- the colour temperature in K to be converted- Returns:
- an array with the found CIE colour XY values
- Throws:
IndexOutOfBoundsException
- if the colour temperature is out of range 2000K .. 6500K
-
xyToKelvin
Convert a point in the CIE XY colour space to a colour temperature in Kelvin. Uses McCamy's approximation as described here.Note that McCamy's approximation is accurate to better than 1% from 2000 K to 10000 K but below 2000 K the approximation error increases rapidly and exponentially.
- Parameters:
xy
- an array with the CIE colour XY values to be converted- Returns:
- the colour temperature in K
- Throws:
IllegalArgumentException
- if the wrong number of arguments is provided
-