Class ColorHelper


  • public class ColorHelper
    extends Object
    Helper class for converting Colors to-and-from strings.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • ColorHelper

        public ColorHelper()
    • Method Detail

      • getNotation

        public static ColorHelper.ColorNotation getNotation​(String str)
        Tries to determine the type of color notation.
        Parameters:
        str - the color string to analyze
        Returns:
        the used notation
      • valueOf

        public static Color valueOf​(String str)
        Returns a color generated from the string. Uses Color.BLACK as default color. Formats:
        • hex notation: #(AA)RRGGBB with AA/RR/GG/BB being hexadecimal strings
        • RGB notation: (A,)R,G,B with A/R/G/B from 0-255
        • predefined names (case-insensitive) : black, blue, cyan, darkgray, darkgrey, gray, grey, green, lightgray, lightgrey, magenta, orange, pink, red, white, yellow
        Parameters:
        str - the string to convert to a color
        Returns:
        the generated color
        See Also:
        getNotation(String), valueOf(String, Color)
      • valueOf

        public static Color valueOf​(String str,
                                    Color defColor)
        Returns a color generated from the string. Formats:
        • hex notation: #(AA)RRGGBB with AA/RR/GG/BB being hexadecimal strings
        • hex notation: #RGB with R/G/B being hexadecimal shorthand strings
        • RGB notation: (A,)R,G,B with A/R/G/B from 0-255
        • predefined names (case-insensitive) : black, blue, cyan, darkgray, darkgrey, gray, grey, green, lightgray, lightgrey, magenta, orange, pink, red, white, yellow
        Parameters:
        str - the string to convert to a color
        defColor - the default color if parsing fails
        Returns:
        the generated color
        See Also:
        getNotation(String)
      • toHex

        protected static String toHex​(int i)
        Turns the integer into a hex string, left-pads with zero.
        Parameters:
        i - the integer to convert
        Returns:
        the generated string
      • toHex

        public static String toHex​(Color color)
        Returns the color as hex string ("#RRGGBB" or "#AARRGGBB"). Alpha is only output if different from 255.
        Parameters:
        color - the color to convert
        Returns:
        the generated string
      • toRGB

        public static String toRGB​(Color color)
        Turns the color into RGB notation ("R,G,B" or "A,R,G,B" with A/R/G/B ranging from 0-255). Alpha is only output if different from 255.
        Parameters:
        color - the color to convert
        Returns:
        the generated string
      • toName

        public static String toName​(Color color)
        Tries to turn color into a name, like 'black' or 'red'. If it cannot find a match, null gets returned.
        Parameters:
        color - the color to convert
        Returns:
        the name of the color, null if no match
      • getContrastColor

        public static Color getContrastColor​(Color color)
        Returns a contrast color.

        Taken from here.
        Parameters:
        color - the color to return the contrast color for
        Returns:
        the contrast color
      • addAlpha

        public static Color addAlpha​(Color color,
                                     int alpha)
        Creates a new color with the specified alpha value (255: opaque, 0: fully translucent).
        Parameters:
        color - the color to use as base
        alpha - the alpha value (0-255)
        Returns:
        the new color
      • invert

        public static Color invert​(Color color)
        Inverts the color, ie. 255-VALUE for RGB. Maintains the alpha value.
        Parameters:
        color - the color to use as base
        Returns:
        the new color