Class ColorUtil


  • public class ColorUtil
    extends java.lang.Object
    Utility class to do color parsing or converting work. A color is a either a keyword or a numerical RGB specification.

    Color property format preference have the following choices:

    • INT_FORMAT, display color as an integer.
    • HTML_FORMAT ( #RRGGBB )
    • JAVA_FORMAT ( 0xRRGGBB )
    • CSS_ABSOLUTE_FORMAT ( RGB(r,g,b) )
    • CSS_RELATIVE_FORMAT ( RGB(r%,g%,b%) )
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CSS_ABSOLUTE_FORMAT
      Color display preference for CSS absolute style: RGB(r,g,b).
      static int CSS_RELATIVE_FORMAT
      Color display preference for CSS relative style: RGB(r%,g%,b%).
      static int DEFAULT_FORMAT
      Default format for display preference: CSS_ABSOLUTE_FORMAT.
      static int HTML_FORMAT
      Color display preference for HTML style: #RRGGBB.
      static int INT_FORMAT
      Useful constant for Color display preference, display Color as integer.
      static int JAVA_FORMAT
      Color display preference for JAVA style: 0xRRGGBB.
    • Constructor Summary

      Constructors 
      Constructor Description
      ColorUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String format​(int rgbValue, int rgbFormat)
      Formats an integer RGB value according to the format preference provided.
      static java.lang.String format​(java.lang.String value, int rgbFormat)
      Formats an color value according to the format preference provided.
      static int formRGB​(int r, int g, int b)
      Calculates the integer color value given its red, blue, green values.
      static java.lang.String getPredefinedColor​(int rgb)
      Gets a css predefined color given its rgb value.
      static int[] getRGBs​(int rgbValue)
      Returns the Red, Blue, Green value for a integer RGB color value.
      static int[] getRGBs​(java.lang.String colorValue)
      Returns the Red, Blue, Green value for a color value.
      static boolean isCssAbsolute​(java.lang.String value)
      Indicates whether the color value is of valid css absolute format: "RGB(r,g,b)".
      static boolean isCssRelative​(java.lang.String value)
      Indicates whether the color value is of a valid css relative format: "RGB( r%, g%, b%)".
      static int parseColor​(java.lang.String value)
      Parses the string color value as a color keyword or a numerical RGB notation, return its corresponding rgb integer value.
      static int parsePredefinedColor​(java.lang.String color)
      Gets the integer value of a predefined color.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HTML_FORMAT

        public static final int HTML_FORMAT
        Color display preference for HTML style: #RRGGBB. #RRGGBB
        See Also:
        Constant Field Values
      • INT_FORMAT

        public static final int INT_FORMAT
        Useful constant for Color display preference, display Color as integer.
        See Also:
        Constant Field Values
      • JAVA_FORMAT

        public static final int JAVA_FORMAT
        Color display preference for JAVA style: 0xRRGGBB.
        See Also:
        Constant Field Values
      • CSS_ABSOLUTE_FORMAT

        public static final int CSS_ABSOLUTE_FORMAT
        Color display preference for CSS absolute style: RGB(r,g,b).
        See Also:
        Constant Field Values
      • CSS_RELATIVE_FORMAT

        public static final int CSS_RELATIVE_FORMAT
        Color display preference for CSS relative style: RGB(r%,g%,b%).
        See Also:
        Constant Field Values
      • DEFAULT_FORMAT

        public static final int DEFAULT_FORMAT
        Default format for display preference: CSS_ABSOLUTE_FORMAT.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ColorUtil

        public ColorUtil()
    • Method Detail

      • parsePredefinedColor

        public static int parsePredefinedColor​(java.lang.String color)
        Gets the integer value of a predefined color. The color should be a predefined color name, otherwise -1 is returned.
        Parameters:
        color - a given color name, it is case insensitive.
        Returns:
        the integer value of a predefined color, return -1 if the name of the given color is not defined.
      • getPredefinedColor

        public static java.lang.String getPredefinedColor​(int rgb)
        Gets a css predefined color given its rgb value.
        Parameters:
        rgb - integer rgb value.
        Returns:
        a css predefined color if there is a predefined color matches the given rgb, return null otherwise.
      • isCssAbsolute

        public static boolean isCssAbsolute​(java.lang.String value)
        Indicates whether the color value is of valid css absolute format: "RGB(r,g,b)". The RGB prefix is case insensitive and r, g, b should be integer value. Whitespace characters are allowed around the numerical values. The followings are some cases of valid css absolute colors:

        • RGB(255,0,0)
        • Rgb( 255, 0, 0)
        • rgb(300,300,300)
        Parameters:
        value - a string color value
        Returns:
        true if the color value is in a valid css absolute color representation.
      • isCssRelative

        public static boolean isCssRelative​(java.lang.String value)
        Indicates whether the color value is of a valid css relative format: "RGB( r%, g%, b%)". The RGB prefix is case insensitive and r, g, b should be a float value. Whitespace characters are allowed around the numerical values. The followings are some cases of valid css relative colors:

        • RGB(100%,0%,0%)
        • Rgb( 100% , 0% , 0% )
        • rgb(200%,200%,200%)
        Parameters:
        value - a string color value
        Returns:
        true if the color value is in a valid css relative. color representation.
      • format

        public static java.lang.String format​(int rgbValue,
                                              int rgbFormat)
        Formats an integer RGB value according to the format preference provided. An integer RGB value can be formatted as follows:
        • INT_FORMAT, display color as an integer.
        • HTML_FORMAT ( #RRGGBB )
        • JAVA_FORMAT ( 0xRRGGBB )
        • CSS_ABSOLUTE_FORMAT ( RGB(r,g,b) )
        • CSS_RELATIVE_FORMAT ( RGB(r%,g%,b%) )

        The integer value will first be converted into 6-digits hex format(filling "0" from left), so only the right most 6 digits will be used.

        Parameters:
        rgbValue - integer RGB value for a color
        rgbFormat - Color display preference, one of Color display preference constants. For example, CSS_ABSOLUTE_FORMAT that will convert into style "RGB(255,0,0)". If the preference provided is not in the predefined list, then CSS_ABSOLUTE_FORMATwill be applied.
        Returns:
        a string representation of the color in the target format.
        See Also:
        INT_FORMAT, HTML_FORMAT, JAVA_FORMAT, CSS_ABSOLUTE_FORMAT, CSS_RELATIVE_FORMAT, DEFAULT_FORMAT
      • format

        public static java.lang.String format​(java.lang.String value,
                                              int rgbFormat)
        Formats an color value according to the format preference provided. See parseColor(String)for the allowed color value representations.
        Parameters:
        value - a given string containing one of the allowed notation.
        rgbFormat - Color display preference, one of Color display preference constants. For example, CSS_ABSOLUTE_FORMAT that will convert into style "RGB(255,0,0)". If the preference provided is not in the predefined list, then CSS_ABSOLUTE_FORMATwill be applied.
        Returns:
        a string representation of the color in the target format.
        Throws:
        java.lang.NumberFormatException - if the String representing a numerical value does not contain a parsable integer.
        See Also:
        INT_FORMAT, HTML_FORMAT, JAVA_FORMAT, CSS_ABSOLUTE_FORMAT, CSS_RELATIVE_FORMAT, DEFAULT_FORMAT, parseColor(String), format(int, int)
      • parseColor

        public static int parseColor​(java.lang.String value)
        Parses the string color value as a color keyword or a numerical RGB notation, return its corresponding rgb integer value. The string value can be one of the followings:
        • A decimal number: "16711680". The number will be clipped into 0~#FFFFFF
        • A hexadecimal number in HTML format. '#' immediately followed by either three or six hexadecimal characters. The three-digit RGB notation (#rgb) is converted into six-digit form (#rrggbb) by replicating digits, not by adding zeros. For example, #fb0 expands to #ffbb00. Values outside "#FFFFFF" will be clipped.
        • A hexadecimal number in Java format: "0xRRGGBB"
        • A css predefined color name: "red", "green", "yellow" etc.
        • A css absolute or relative notation: "rgb(r,g,b)" or "rgb(r%,g%,b%)". 'rgb(' followed by a comma-separated list of three numerical values (either three integer values in the range of 0-255, or three percentage values in the range of 0.0% to 100.0%) followed by '), Values outside the numerical ranges will be clipped. Whitespace characters are allowed around the numerical values.

        These examples given a allowed color value that can be parsed into an integer.

        • "16711680"
        • "#FFFF00"
        • "#FFFF00F", will be clipped into "#FFFFFF"
        • "0xFF00FF"
        • "red" or "green"
        • rgb(255,0,0)
        • rgb(100.0%,0%,0%)
        Parameters:
        value - a given string containing one of the allowed notation.
        Returns:
        the integer value of the color, return -1 if the value is not in one of the allowed format. If the value is in a valid integer format, return value will be clipped to 0 ~ 0xFFFFFF
      • getRGBs

        public static int[] getRGBs​(int rgbValue)
        Returns the Red, Blue, Green value for a integer RGB color value. The given RGB value should be in the scope of (0~0xFFFFFF), otherwise return null.
        Parameters:
        rgbValue - a given integer RGB color value.
        Returns:
        an array containing Red, Blue, Green separately. Return null if the value is not in (0~0xFFFFFF).
      • getRGBs

        public static int[] getRGBs​(java.lang.String colorValue)
        Returns the Red, Blue, Green value for a color value. The given string containing one of the allowed notations.
        Parameters:
        colorValue - a given string color value in one of the allowed notations.
        Returns:
        an array containing Red, Blue, Green separately. Return null if the given color value is not parsable.
      • formRGB

        public static int formRGB​(int r,
                                  int g,
                                  int b)
        Calculates the integer color value given its red, blue, green values. If any color factor is over 0xFF, it will be clipped to 0xFF; if any color factor is below 0, it will be increased to 0.
        Parameters:
        r - red value.
        g - green value.
        b - blue value.
        Returns:
        the integer color value of the given color factors.