This implementation uses Java's ColorModel and ColorSpace classes
to transform colors from CIEXYZ color coordinates to those of the current
color model. The color models can be obtained by calling the
getColorModel() method that are provided by the classes such as
BufferedImage
, Graph
,
and OutputStreamGraphics
. To get a color
model from a Graphics2D
, use the method
Graphics2D.getDeviceConfiguration()
and call its
GraphicsConfiguration.getColorModel()
method.
The methods for this class are static methods. These create a color that matches
- a specific wavelength.
- a spectrum (the spectral radiance in terms of wavelength).
- a CSS specification.
-
Method Summary
Modifier and TypeMethodDescriptionstatic RealValuedFunction
blackbodySpectrum
(double T) Get the blackbody spectrum for a specified temperature.static Color
getColorByCSS
(String spec) Get a color given a CSS specification.static Color
getColorByHSL
(double h, double s, double l) Get a color given HSL values.static Color
getColorByHSL
(double h, double s, double l, double a) Get a color given HSLA values.static Color
getColorBySpectrum
(ColorModel cm, RealValuedFunctOps spectrum, double intensity, double alpha) Get a color given a spectrum, gamma correction, intensity, and alpha value.static Color
getColorBysRGB
(double r, double g, double b) Get an opaque sRGB color with the specified red, green, and blue values in the range [0.0, 1.0].static Color
getColorBysRGB
(double r, double g, double b, double a) Get an sRGB color with the specified red, green, blue and alpha values in the range [0.0, 1.0].static Color
getColorByWavelength
(ColorModel cm, double wavelength) Get a color given the wavelength of light.static Color
getColorByWavelength
(ColorModel cm, double wavelength, double intensity) Get a color given the wavelength and gamma-correction.static Color
getColorByWavelength
(ColorModel cm, double wavelength, double intensity, double alpha) Get a color given a wavelength, gamma correction, intensity, and alpha value.static int[]
getComponentsByCSS
(String spec) Get color components given a CSS specification.static String
Return the CSS specification for a color.static String
getCSSName
(Color c) Return the CSS name of a colorstatic int
namedCSSColorIndex
(Color c, boolean nameOrder) Get the index of a color in a table of named CSS colors, sorted by either name or RGB value.Get a set containing the names of CSS colors.static Collection<String>
namedCSSColors
(boolean nameOrder) Get a Collection containing the names of CSS colors, excluding "transparent".namedCSSColors
(String lower, String upper) Get a set containing the names of CSS colors in a specified range The color "transparent" is not included.
-
Method Details
-
namedCSSColors
Get a set containing the names of CSS colors. The name "transparent" is not included because there is no corresponsding color that can be shown.- Returns:
- a set containing those CSS colors that have names
-
namedCSSColors
Get a Collection containing the names of CSS colors, excluding "transparent".- Parameters:
nameOrder
- true if the set is ordered by name, false if it is ordered by value- Returns:
- a set containing those CSS colors that have names
-
namedCSSColorIndex
Get the index of a color in a table of named CSS colors, sorted by either name or RGB value. A RGB value can be represented as 0xRRGGBB where R, G, and B are hexidecimal digits representing the red, green, and blue components of a color respectively, with the value sorted in numerical order.The CSS color "transparent" is not included.
- Parameters:
c
- the colornameOrder
- true if the colors are sorted by name; false if the colors are sorted by RGB values- Returns:
- the index into the table; -1 if the color is null or is not a named color
-
namedCSSColors
Get a set containing the names of CSS colors in a specified range The color "transparent" is not included.- Parameters:
lower
- the starting name for the range (inclusive)upper
- the ending name for the range (exclusive); null for all keys starting from lower- Returns:
- a set containing those CSS colors that have names in the specified range
-
getCSSName
Return the CSS name of a color- Parameters:
c
- a color- Returns:
- the CSS name for the color; null if the color is not one with a name recognized by CSS.
-
getCSS
Return the CSS specification for a color. If the color matches a named color, that name is returned. Otherwise a rgb or rgba specification is returned.Some CSS named colors are mapped to the same color, and the value returned simply picks one. Also, the CSS rgb and rgba specifications use floating point numbers for the alpha value whereas Java's Color class uses an integer in the range [0, 255]. If a specification is used to create a color, this method may return a slightly different specification due to rounding.
- Parameters:
c
- the color- Returns:
- the CSS specification for the color.
-
getColorByCSS
Get a color given a CSS specification. The specification for the color is a string containing one of the following:- 6 hexadecimal digits following an '#' and providing an opaque color (i.e., the alpha value is 255 in a RGBA color space).
rgb(RED,BLUE,GREEN)
, whereRED
,BLUE
, andGREEN
are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%'.rgba(RED,BLUE,GREEN,ALPHA)
, whereRED
,BLUE
, andGREEN
are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%', andALPHA
varies from 0.0 (fully transparent to 1.0 (opaque).hsl(HUE,SATURATION,LIGHTNESS)
, whereHUE
is in the range [0,360],SATURATION
is a number in the range [0,100] followed immediately by a '%', and whereLIGHTNESS
is a number in the range [0,100] followed immediately by a '%'.hsla(HUE,SATURATION,LIGHTNESS, ALPHA)
, whereHUE
is in the range [0,360],SATURATION
is a number in the range [0,100] followed immediately by a '%', whereLIGHTNESS
is a number in the range [0,100] followed immediately by a '%', and whereALPHA
is a floating point number in the range [0, 1].- a keyword denoting an X11 color with the addition of "gray" and its synonym "grey" from SVG 1.0. X11 colors are described in the document X11 color names. The variant of these names that this class uses are the ones given in the W3C recommendation cited above. These are all lower case with no whitespace.
- Parameters:
spec
- the CSS specification- Returns:
- the color corresponding to the specification
- Throws:
IllegalArgumentException
- the CSS specification was ill-formed.
-
getComponentsByCSS
Get color components given a CSS specification. The specification for the color is a string containing one of the following:- 6 hexadecimal digits following an '#' and providing an opaque color (i.e., the alpha value is 255 in a RGBA color space).
rgb(RED,BLUE,GREEN)
, whereRED
,BLUE
, andGREEN
are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%'.rgba(RED,BLUE,GREEN,ALPHA)
, whereRED
,BLUE
, andGREEN
are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%', andALPHA
varies from 0.0 (fully transparent to 1.0 (opaque).hsl(HUE,SATURATION,LIGHTNESS)
, whereHUE
is in the range [0,360],SATURATION
is a number in the range [0,100] followed immediately by a '%', and whereLIGHTNESS
is a number in the range [0,100] followed immediately by a '%'.hsl(HUE,SATURATION,LIGHTNESS, ALPHA)
, whereHUE
is in the range [0,360],SATURATION
is a number in the range [0,100] followed immediately by a '%', whereLIGHTNESS
is a number in the range [0,100] followed immediately by a '%', and whereALPHA
is a floating point number in the range [0, 1].- a keyword denoting an X11 color with the addition of "gray" and its synonym "grey" from SVG 1.0. X11 colors are described in the document X11 color names. The variant of these names that this class uses are the ones given in the W3C recommendation cited above. These are all lower case with no whitespace.
- Parameters:
spec
- the CSS specification- Returns:
- an array whose first three elements are the red, green, and blue components of a color and whose optional fourth component in the alpha value, each in the range [0,255]
- Throws:
IllegalArgumentException
- the CSS specification was ill-formed.
-
getColorByHSL
Get a color given HSL values. In CSS, h varies from 0 to 360, but Java's HSB code assumes that h varies from 0 to 1. This method uses the Java convention.- Parameters:
h
- the hue specified as a number in the range [0.0, 1.0)s
- the saturation specified as a number in the range [0, 1]l
- the lightness specified as a number in the range [0, 1]- Returns:
- the color corresponding the parameters h, s, and l
-
getColorByHSL
Get a color given HSLA values. In CSS, h varies from 0 to 360, but Java's HSB code assumes that h varies from 0 to 1. This method uses the Java convention.- Parameters:
h
- the hue specified as a number in the range [0.0, 1.0)s
- the saturation specified as a number in the range [0.0, 1.0]l
- the lightness specified as a number in the range [0.0, 1.0]a
- the value of alpha as a number in the range [0.0, 1.0]- Returns:
- the color corresponding the parameters h, s, l, and a
-
getColorBysRGB
Get an opaque sRGB color with the specified red, green, and blue values in the range [0.0, 1.0].This method simply calls a
Color
constructor and is provided for scripting languages such as ESP that do not support single-precision real numbers.- Parameters:
r
- the red componentg
- the green componentb
- the blue component- Returns:
- the color for the given values
-
getColorBysRGB
Get an sRGB color with the specified red, green, blue and alpha values in the range [0.0, 1.0].This method simply calls a
Color
constructor and is provided for scripting languages such as ESP that do not support single-precision real numbers.- Parameters:
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha component- Returns:
- the color for the given values
-
getColorByWavelength
Get a color given the wavelength of light.- Parameters:
cm
- the color modelwavelength
- the wavelength in meters- Returns:
- the color for the given values
-
getColorByWavelength
Get a color given the wavelength and gamma-correction. When the intensity is 1.0, the color is as bright as possible given that the maximum RGB value for either of these three components has an upper bound (255 for integer values and 1.0F for floating-point values). An intensity of 0.0 will result in a color equal toColor.BLACK
.- Parameters:
cm
- the color modelwavelength
- the wavelength in metersintensity
- an intensity scaling factor in the range [0.0, 1.0]- Returns:
- the color for the given values
-
getColorByWavelength
public static Color getColorByWavelength(ColorModel cm, double wavelength, double intensity, double alpha) Get a color given a wavelength, gamma correction, intensity, and alpha value. The visible portion of the spectrum is between 380 nm and 780 nm. Anything outside that range will appear black. When the intensity is 1.0, the color is as bright as possible given that the maximum RGB value for either of these three components has an upper bound (255 for integer values and 1.0F for floating-point values). An intensity of 0.0 will result in a color equal toColor.BLACK
.- Parameters:
cm
- the color modelwavelength
- the wavelength in metersintensity
- an intensity scaling factor in the range [0.0, 1.0]alpha
- the alpha component of the color (range: [0.0, 1.0])- Returns:
- the color for the given values
-
getColorBySpectrum
public static Color getColorBySpectrum(ColorModel cm, RealValuedFunctOps spectrum, double intensity, double alpha) Get a color given a spectrum, gamma correction, intensity, and alpha value. The visible portion of the spectrum is between 380 nm and 780 nm. Anything outside that range will appear black. When the intensity is 1.0, the color is as bright as possible given that the maximum RGB value for either of these three components has an upper bound (255 for integer values and 1.0F for floating-point values). An intensity of 0.0 will result in a color equal toColor.BLACK
.- Parameters:
cm
- the color modelspectrum
- the spectral radiance (measured per unit wavelength) of electromagnetic radiation as a function of wavelengthintensity
- an intensity scaling factor in the range [0, 1.0]alpha
- the alpha component of the color (range: [0.0, 1.0])- Returns:
- the color for the given values
-
blackbodySpectrum
Get the blackbody spectrum for a specified temperature. The spectrum is defined as the spectral radiance (measured per unit wavelength) of electromagnetic radiation as a function of wavelength.- Parameters:
T
- the temperature in Kelvin- Returns:
- the spectrum as a real-valued function.
-