Class Colors

java.lang.Object
org.bzdev.graphs.Colors

public class Colors extends Object
Class to provide various colors. For colors related to blackbody radiation, this class is based on the algorithm used in the article Approximate RGB values for Visible Wavelengths, Colour Rendering of Spectra, and a public-domain C program.

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.
In addition to creating a color from a CSS specification, there is also a method that returns an integer array containing the RGB values or the RGBA values for a color corresponding to a CSS specification.
  • Method Details

    • namedCSSColors

      public static Set<String> 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

      public static Collection<String> namedCSSColors(boolean nameOrder)
      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

      public static 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. 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 color
      nameOrder - 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

      public static Set<String> namedCSSColors(String lower, String upper)
      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

      public static String getCSSName(Color c)
      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

      public static String getCSS(Color c)
      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

      public static Color getColorByCSS(String spec) throws IllegalArgumentException
      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), where RED, BLUE, and GREEN are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%'.
      • rgba(RED,BLUE,GREEN,ALPHA), where RED, BLUE, and GREEN are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%', and ALPHA varies from 0.0 (fully transparent to 1.0 (opaque).
      • hsl(HUE,SATURATION,LIGHTNESS), where HUE is in the range [0,360], SATURATION is a number in the range [0,100] followed immediately by a '%', and where LIGHTNESS is a number in the range [0,100] followed immediately by a '%'.
      • hsla(HUE,SATURATION,LIGHTNESS, ALPHA), where HUE is in the range [0,360], SATURATION is a number in the range [0,100] followed immediately by a '%', where LIGHTNESS is a number in the range [0,100] followed immediately by a '%', and where ALPHA 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.
      As a convenience, whitespace will be trimmed from the specification before processing it and the specification will be converted to lower case.
      Parameters:
      spec - the CSS specification
      Returns:
      the color corresponding to the specification
      Throws:
      IllegalArgumentException - the CSS specification was ill-formed.
    • getComponentsByCSS

      public static int[] getComponentsByCSS(String spec) throws IllegalArgumentException
      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), where RED, BLUE, and GREEN are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%'.
      • rgba(RED,BLUE,GREEN,ALPHA), where RED, BLUE, and GREEN are either numbers in the range [0, 255] or a number (typically floating point) followed by a '%', and ALPHA varies from 0.0 (fully transparent to 1.0 (opaque).
      • hsl(HUE,SATURATION,LIGHTNESS), where HUE is in the range [0,360], SATURATION is a number in the range [0,100] followed immediately by a '%', and where LIGHTNESS is a number in the range [0,100] followed immediately by a '%'.
      • hsl(HUE,SATURATION,LIGHTNESS, ALPHA), where HUE is in the range [0,360], SATURATION is a number in the range [0,100] followed immediately by a '%', where LIGHTNESS is a number in the range [0,100] followed immediately by a '%', and where ALPHA 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.
      As a convenience, whitespace will be trimmed from the specification before processing it and the specification will be converted to lower case.
      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

      public static Color getColorByHSL(double h, double s, double l)
      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

      public static Color getColorByHSL(double h, double s, double l, double a)
      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

      public 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].

      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 component
      g - the green component
      b - the blue component
      Returns:
      the color for the given values
    • getColorBysRGB

      public 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].

      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 component
      g - the green component
      b - the blue component
      a - the alpha component
      Returns:
      the color for the given values
    • getColorByWavelength

      public static Color getColorByWavelength(ColorModel cm, double wavelength)
      Get a color given the wavelength of light.
      Parameters:
      cm - the color model
      wavelength - the wavelength in meters
      Returns:
      the color for the given values
    • getColorByWavelength

      public static Color getColorByWavelength(ColorModel cm, double wavelength, double intensity)
      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 to Color.BLACK.
      Parameters:
      cm - the color model
      wavelength - the wavelength in meters
      intensity - 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 to Color.BLACK.
      Parameters:
      cm - the color model
      wavelength - the wavelength in meters
      intensity - 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 to Color.BLACK.
      Parameters:
      cm - the color model
      spectrum - the spectral radiance (measured per unit wavelength) of electromagnetic radiation as a function of wavelength
      intensity - 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

      public static RealValuedFunction blackbodySpectrum(double T)
      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.