Class ColorUtil


  • public class ColorUtil
    extends Object
    Utility class for matching and parsing Color objects from String input. Description of supported formats see http://www.w3schools.com/cssref/css_colors_legal.asp
    Since:
    8.4
    • Field Detail

      • HEX_PATTERN

        public static final Pattern HEX_PATTERN
        Case-insensitive Pattern with regular expression matching the default hexadecimal color presentation pattern:
        '#' followed by six [\da-fA-F] characters.

        Pattern contains named groups red, green, and blue, which represent the individual values.

      • RGB_PATTERN

        public static final Pattern RGB_PATTERN
        Case-insensitive Pattern with regular expression matching common RGB color presentation patterns:
        'rgb' followed by three [0-255] number values. Values can be separated with either comma or whitespace.

        Pattern contains named groups red, green, and blue, which represent the individual values.

      • RGBA_PATTERN

        public static final Pattern RGBA_PATTERN
        Case-insensitive Pattern with regular expression matching common RGBA presentation patterns:
        'rgba' followed by three [0-255] values and one [0.0-1.0] value. Values can be separated with either comma or whitespace. The only accepted decimal marker is point ('.').

        Pattern contains named groups red, green, blue, and alpha, which represent the individual values.

      • HSL_PATTERN

        public static final Pattern HSL_PATTERN
        Case-insensitive Pattern with regular expression matching common HSL presentation patterns:
        'hsl' followed by one [0-360] value and two [0-100] percentage value. Values can be separated with either comma or whitespace. The percent sign ('%') is optional.

        Pattern contains named groups hue,saturation, and light, which represent the individual values.

      • HSLA_PATTERN

        public static final Pattern HSLA_PATTERN
        Case-insensitive Pattern with regular expression matching common HSLA presentation patterns:
        'hsla' followed by one [0-360] value, two [0-100] percentage values, and one [0.0-1.0] value. Values can be separated with either comma or whitespace. The percent sign ('%') is optional. The only accepted decimal marker is point ('.').

        Pattern contains named groups hue,saturation, light, and alpha, which represent the individual values.

    • Method Detail

      • stringToColor

        public static Color stringToColor​(String input)
        Parses Color from any of the following String inputs:
        - RGB hex (e.g. "#FFAA00"), HEX_PATTERN
        - RGB "function" (e.g. "rgb(128,0,255)"), RGB_PATTERN
        - RGBA "function" (e.g. "rgba(50,50,50,0.2)"), RGBA_PATTERN
        - HSL "function" (e.g. "hsl(50,50,50)"), HSL_PATTERN
        - HSLA "function" (e.g. "hsl(50,50,50,0.2)"), HSLA_PATTERN

        Parsing is case-insensitive.

        Parameters:
        input - String input
        Returns:
        Color parsed from input
        Throws:
        NumberFormatException - Input does not match any recognized pattern
      • getHexPatternColor

        public static Color getHexPatternColor​(Matcher matcher)
        Parses Color from matched hexadecimal Matcher.
        Parameters:
        matcher - Matcher matching hexadecimal pattern with named regex groups red, green, and blue
        Returns:
        Color parsed from Matcher
      • getRGBPatternColor

        public static Color getRGBPatternColor​(Matcher matcher)
        Parses Color from matched RGB Matcher.
        Parameters:
        matcher - Matcher matching RGB pattern with named regex groups red, green, and blue
        Returns:
        Color parsed from Matcher
      • getRGBAPatternColor

        public static Color getRGBAPatternColor​(Matcher matcher)
        Parses Color from matched RGBA Matcher.
        Parameters:
        matcher - Matcher matching RGBA pattern with named regex groups red, green, blue, and alpha
        Returns:
        Color parsed from Matcher
      • getHSLPatternColor

        public static Color getHSLPatternColor​(Matcher matcher)
        Parses Color from matched HSL Matcher.
        Parameters:
        matcher - Matcher matching HSL pattern with named regex groups hue, saturation, and light
        Returns:
        Color parsed from Matcher
      • getHSLAPatternColor

        public static Color getHSLAPatternColor​(Matcher matcher)
        Parses Color from matched HSLA Matcher.
        Parameters:
        matcher - Matcher matching HSLA pattern with named regex groups hue, saturation, light, and alpha
        Returns:
        Color parsed from Matcher