Class ColorUtil


  • public final class ColorUtil
    extends java.lang.Object
    Generic Color helper class.

    This class supports parsing string values into color values and creating color values for strings. It provides a list of standard color names.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ALPHA_PSEUDO_PROFILE  
      static java.lang.String CMYK_PSEUDO_PROFILE
      The name for the uncalibrated CMYK pseudo-profile
      private static java.util.Map<java.lang.String,​java.awt.Color> colorMap
      Keeps all the predefined and parsed colors.
      private static org.apache.commons.logging.Log log
      Logger instance
      static java.lang.String SEPARATION_PSEUDO_PROFILE
      The name for the Separation pseudo-profile used for spot colors
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ColorUtil()
      Private constructor since this is an utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String colorToString​(java.awt.Color color)
      Creates a re-parsable string representation of the given color.
      private static java.awt.Color createColor​(int r, int g, int b)  
      private static float[] getICCComponents​(java.util.ListIterator<java.lang.String> args)  
      private static java.awt.Color getsRGBFallback​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)  
      private static void initializeColorMap()
      Initializes the colorMap with some predefined values.
      static boolean isGray​(java.awt.Color col)
      Indicates whether the color is a gray value.
      static boolean isPseudoProfile​(java.lang.String colorProfileName)
      Indicates whether the given color profile name is one of the pseudo-profiles supported by FOP (ex.
      static java.awt.Color lightenColor​(java.awt.Color col, float factor)
      Lightens up a color for groove, ridge, inset and outset border effects.
      private static java.awt.Color parseAsCIELabColor​(FOUserAgent foUserAgent, java.lang.String value)
      Parse a color specified using the cie-lab-color() function.
      private static java.awt.Color parseAsCMYK​(java.lang.String value)
      Parse a color given with the cmyk() function.
      private static java.awt.Color parseAsFopRgbIcc​(FOUserAgent foUserAgent, java.lang.String value)
      Parse a color specified using the fop-rgb-icc() function.
      private static java.awt.Color parseAsFopRgbNamedColor​(FOUserAgent foUserAgent, java.lang.String value)
      Parse a color specified using the fop-rgb-named-color() function.
      private static java.awt.Color parseAsJavaAWTColor​(java.lang.String value)
      Tries to parse the standard java.awt.Color toString output.
      private static java.awt.Color parseAsOCA​(java.lang.String value)  
      private static java.awt.Color parseAsRGB​(java.lang.String value)
      Parse a color given with the rgb() function.
      private static java.awt.Color parseAsSystemColor​(java.lang.String value)
      Tries to parse a color given with the system-color() function.
      static java.awt.Color parseColorString​(FOUserAgent foUserAgent, java.lang.String value)
      Creates a color from a given string.
      private static float parseComponent​(java.lang.String argument, float min, float max, java.lang.String function)  
      private static float parseComponent1​(java.lang.String argument, java.lang.String function)  
      private static float parseComponent255​(java.lang.String str, java.lang.String function)  
      private static java.awt.Color parseFallback​(java.util.ListIterator<java.lang.String> args, java.lang.String value)  
      private static java.awt.Color parseWithHash​(java.lang.String value)
      Parse a color given in the #....
      private static java.lang.String toCIELabFunctionCall​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)  
      static java.awt.Color toCMYKGrayColor​(float black)
      Creates an uncalibrated CMYK color with the given gray value.
      private static java.lang.String toFunctionCall​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)
      Create string representation of a fop-rgb-icc (or fop-rgb-named-color) function call from the given color.
      private static java.lang.String toRGBFunctionCall​(java.awt.Color color)  
      private static java.awt.Color toSRGBColor​(java.awt.Color color)  
      private static java.lang.String unescapeString​(java.lang.String iccProfileSrc)  
      • Methods inherited from class java.lang.Object

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

      • CMYK_PSEUDO_PROFILE

        public static final java.lang.String CMYK_PSEUDO_PROFILE
        The name for the uncalibrated CMYK pseudo-profile
        See Also:
        Constant Field Values
      • SEPARATION_PSEUDO_PROFILE

        public static final java.lang.String SEPARATION_PSEUDO_PROFILE
        The name for the Separation pseudo-profile used for spot colors
        See Also:
        Constant Field Values
      • ALPHA_PSEUDO_PROFILE

        public static final java.lang.String ALPHA_PSEUDO_PROFILE
        See Also:
        Constant Field Values
      • colorMap

        private static java.util.Map<java.lang.String,​java.awt.Color> colorMap
        Keeps all the predefined and parsed colors.

        This map is used to predefine given colors, as well as speeding up parsing of already parsed colors.

        Important: The use of this color map assumes that all Color instances are immutable!

      • log

        private static final org.apache.commons.logging.Log log
        Logger instance
    • Constructor Detail

      • ColorUtil

        private ColorUtil()
        Private constructor since this is an utility class.
    • Method Detail

      • parseColorString

        public static java.awt.Color parseColorString​(FOUserAgent foUserAgent,
                                                      java.lang.String value)
                                               throws PropertyException
        Creates a color from a given string.

        This function supports a wide variety of inputs.

        • #RGB (hex 0..f)
        • #RGBA (hex 0..f)
        • #RRGGBB (hex 00..ff)
        • #RRGGBBAA (hex 00..ff)
        • rgb(r,g,b) (0..255 or 0%..100%)
        • java.awt.Color[r=r,g=g,b=b] (0..255)
        • system-color(colorname)
        • transparent
        • colorname
        • fop-rgb-icc(r,g,b,cs,cs-src,[num]+) (r/g/b: 0..1, num: 0..1)
        • cmyk(c,m,y,k) (0..1)
        Parameters:
        foUserAgent - FOUserAgent object
        value - the string to parse.
        Returns:
        a Color representing the string if possible
        Throws:
        PropertyException - if the string is not parsable or does not follow any of the given formats.
      • parseAsSystemColor

        private static java.awt.Color parseAsSystemColor​(java.lang.String value)
                                                  throws PropertyException
        Tries to parse a color given with the system-color() function.
        Parameters:
        value - the complete line
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • parseAsJavaAWTColor

        private static java.awt.Color parseAsJavaAWTColor​(java.lang.String value)
                                                   throws PropertyException
        Tries to parse the standard java.awt.Color toString output.
        Parameters:
        value - the complete line
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
        See Also:
        Color.toString()
      • parseAsRGB

        private static java.awt.Color parseAsRGB​(java.lang.String value)
                                          throws PropertyException
        Parse a color given with the rgb() function.
        Parameters:
        value - the complete line
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • parseComponent

        private static float parseComponent​(java.lang.String argument,
                                            float min,
                                            float max,
                                            java.lang.String function)
                                     throws PropertyException
        Throws:
        PropertyException
      • parseFallback

        private static java.awt.Color parseFallback​(java.util.ListIterator<java.lang.String> args,
                                                    java.lang.String value)
                                             throws PropertyException
        Throws:
        PropertyException
      • parseWithHash

        private static java.awt.Color parseWithHash​(java.lang.String value)
                                             throws PropertyException
        Parse a color given in the #.... format.
        Parameters:
        value - the complete line
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • parseAsFopRgbIcc

        private static java.awt.Color parseAsFopRgbIcc​(FOUserAgent foUserAgent,
                                                       java.lang.String value)
                                                throws PropertyException
        Parse a color specified using the fop-rgb-icc() function.
        Parameters:
        value - the function call
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • getICCComponents

        private static float[] getICCComponents​(java.util.ListIterator<java.lang.String> args)
      • parseAsFopRgbNamedColor

        private static java.awt.Color parseAsFopRgbNamedColor​(FOUserAgent foUserAgent,
                                                              java.lang.String value)
                                                       throws PropertyException
        Parse a color specified using the fop-rgb-named-color() function.
        Parameters:
        value - the function call
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • parseAsCIELabColor

        private static java.awt.Color parseAsCIELabColor​(FOUserAgent foUserAgent,
                                                         java.lang.String value)
                                                  throws PropertyException
        Parse a color specified using the cie-lab-color() function.
        Parameters:
        value - the function call
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • unescapeString

        private static java.lang.String unescapeString​(java.lang.String iccProfileSrc)
      • parseAsCMYK

        private static java.awt.Color parseAsCMYK​(java.lang.String value)
                                           throws PropertyException
        Parse a color given with the cmyk() function.
        Parameters:
        value - the complete line
        Returns:
        a color if possible
        Throws:
        PropertyException - if the format is wrong.
      • colorToString

        public static java.lang.String colorToString​(java.awt.Color color)
        Creates a re-parsable string representation of the given color.

        First, the color will be converted into the sRGB colorspace. It will then be printed as #rrggbb, or as #rrrggbbaa if an alpha value is present.

        Parameters:
        color - the color to represent.
        Returns:
        a re-parsable string representadion.
      • toRGBFunctionCall

        private static java.lang.String toRGBFunctionCall​(java.awt.Color color)
      • getsRGBFallback

        private static java.awt.Color getsRGBFallback​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)
      • toSRGBColor

        private static java.awt.Color toSRGBColor​(java.awt.Color color)
      • toFunctionCall

        private static java.lang.String toFunctionCall​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)
        Create string representation of a fop-rgb-icc (or fop-rgb-named-color) function call from the given color.
        Parameters:
        color - the color to turn into a function call
        Returns:
        the string representing the internal fop-rgb-icc() or fop-rgb-named-color() function call
      • toCIELabFunctionCall

        private static java.lang.String toCIELabFunctionCall​(org.apache.xmlgraphics.java2d.color.ColorWithAlternatives color)
      • createColor

        private static java.awt.Color createColor​(int r,
                                                  int g,
                                                  int b)
      • initializeColorMap

        private static void initializeColorMap()
        Initializes the colorMap with some predefined values.
      • lightenColor

        public static java.awt.Color lightenColor​(java.awt.Color col,
                                                  float factor)
        Lightens up a color for groove, ridge, inset and outset border effects.
        Parameters:
        col - the color to lighten up
        factor - factor by which to lighten up (negative values darken the color)
        Returns:
        the modified color
      • isPseudoProfile

        public static boolean isPseudoProfile​(java.lang.String colorProfileName)
        Indicates whether the given color profile name is one of the pseudo-profiles supported by FOP (ex. #CMYK).
        Parameters:
        colorProfileName - the color profile name to check
        Returns:
        true if the color profile name is of a built-in pseudo-profile
      • isGray

        public static boolean isGray​(java.awt.Color col)
        Indicates whether the color is a gray value.
        Parameters:
        col - the color
        Returns:
        true if it is a gray value
      • toCMYKGrayColor

        public static java.awt.Color toCMYKGrayColor​(float black)
        Creates an uncalibrated CMYK color with the given gray value.
        Parameters:
        black - the gray component (0 - 1)
        Returns:
        the CMYK color