Class Colors


  • public class Colors
    extends Object
    A bunch of static utility methods useful in conjunction with colors.
    • Method Detail

      • alpha

        public static Color alpha​(Color c,
                                  int alpha)
        Create a new color identical to the given one, but having its alpha value replaced with the given one.
        Parameters:
        c -
        alpha -
        Returns:
        the color with alpha applied
      • alpha

        public static Color alpha​(Color c,
                                  float alpha)
        Create a new color identical to the given one, but having its alpha value replaced with the given one.
        Parameters:
        c -
        alpha -
        Returns:
        the color with alpha applied
      • alphaComposite

        public static Color alphaComposite​(Color fg,
                                           Color bg)
        Composite two colors using alpha compositing.
        Parameters:
        fg - the foreground color
        bg - the background color
        Returns:
        the composited color
      • luminance

        public static float luminance​(Color c)
        Return the color's luminance normalized to the range [0,1].
        Parameters:
        c -
        Returns:
        the color's luminance
      • luminanceContrast

        public static float luminanceContrast​(Color c1,
                                              Color c2)
        Return luminance contrast between the two colors normalized to the range [0,1].
        Parameters:
        c1 - first color
        c2 - second color
        Returns:
        the luminance contrast
      • invert

        public static Color invert​(Color c)
        Create a new color which is the inverse of the given one.
        Parameters:
        c -
      • optimizeForegroundContrast

        public static Color optimizeForegroundContrast​(Color fg,
                                                       Color bg)
        Optimize the contrast of the foreground color against the background by inverting it, if inversion leads to a higher contrast. Contrast is determined by looking at the color's luminance instead of by looking at individual channels.
        Parameters:
        fg - the foreground color
        bg - the background color
        Returns:
        the optimized color
      • optimizeContrastColorNoAlpha

        public static Color optimizeContrastColorNoAlpha​(Color color,
                                                         Color background,
                                                         int minimumContrastPercent)
        This is an experimental new contrast optimization strategy. Use at your own risk.
        Parameters:
        color -
        background -
        minimumContrastPercent -
        Returns:
        the optimized color
      • optimizeContrastColor

        public static Color optimizeContrastColor​(Color color,
                                                  Color background,
                                                  int minimumContrastPercent)
      • toHSL

        public static float[] toHSL​(Color c)
        Convert the given color to the HSB (sometimes called HSL) color model. The returned values are normalized to the range [0,1].
        Parameters:
        c -
      • fromHSB

        public static Color fromHSB​(float[] hsl)
        Convert the given color in the HSB (sometimes called HSL) color model into a Color in the color space sRGB. The given values must be normalized to the range [0,1].
        Parameters:
        hsl - the HSL components
      • fromHSB

        public static Color fromHSB​(float h,
                                    float s,
                                    float b)
        Convert the given color in the HSB (sometimes called HSL) color model into a Color in the color space sRGB. The given values must be normalized to the range [0,1].
        Parameters:
        h - hue
        s - saturation
        b - brightness
      • parseColor

        public static Color parseColor​(String colorString)
      • toHtml

        public static String toHtml​(Color c)
        Convert the given RGB(A) color into an HTML color code of the Form #[6 or 8 hex digits]. The conversion will automatically include the alpha channel (8 hex digits) or not (6 digits) depending on whether the color has transparency ( Color.getTransparency()).
        Parameters:
        c - the color
        Returns:
        the HTML color code