Class PaintUtils


  • public class PaintUtils
    extends java.lang.Object
    A collection of utilities for working with Paints and Colors.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private PaintUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.awt.geom.Point2D[] adjustPoints​(java.awt.geom.Point2D[] pts, int width, int height)  
      static java.awt.Color blend​(java.awt.Color origin, java.awt.Color over)
      Blends two colors to create a new color.
      private static double calcAngle​(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)  
      static java.awt.Color computeForeground​(java.awt.Color bg)
      Computes an appropriate foreground color (either white or black) for the given background color.
      static java.awt.Paint getCheckerPaint()
      Creates a new Paint that is a checkered effect using the colors gray and Color.WHITE.
      static java.awt.Paint getCheckerPaint​(java.awt.Paint c1, java.awt.Paint c2, int size)
      Creates a new Paint that is a checkered effect using the specified colors.
      static java.awt.Color interpolate​(java.awt.Color b, java.awt.Color a, float t)
      Interpolates a color.
      private static java.lang.Object invokeMethod​(java.lang.Object p, java.lang.String methodName)  
      private static boolean isNear​(double angle, double target, double error)  
      private static float normalize​(double original, float target)  
      static java.awt.Color removeAlpha​(java.awt.Color color)
      Returns a new color equal to the old one, except that there is no alpha (transparency) channel.
      static java.awt.Paint resizeGradient​(java.awt.Paint p, int width, int height)
      Resizes a gradient to fill the width and height available.
      private static java.awt.Paint resizeLinearGradient​(java.awt.Paint p, int width, int height)  
      static java.awt.Color setAlpha​(java.awt.Color color, int alpha)
      Returns a new color equal to the old one, except alpha (transparency) channel is set to the new value.
      static java.awt.Color setBrightness​(java.awt.Color color, float brightness)
      Returns a new color equal to the old one, except the brightness is set to the new value.
      static java.awt.Color setSaturation​(java.awt.Color color, float saturation)
      Returns a new color equal to the old one, except the saturation is set to the new value.
      static java.lang.String toHexString​(java.awt.Color color)
      Creates a String that represents the supplied color as a hex-value RGB triplet, including the "#".
      • Methods inherited from class java.lang.Object

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

      • BLUE_EXPERIENCE

        public static final java.awt.GradientPaint BLUE_EXPERIENCE
      • MAC_OSX_SELECTED

        public static final java.awt.GradientPaint MAC_OSX_SELECTED
      • MAC_OSX

        public static final java.awt.GradientPaint MAC_OSX
      • AERITH

        public static final java.awt.GradientPaint AERITH
      • GRAY

        public static final java.awt.GradientPaint GRAY
      • RED_XP

        public static final java.awt.GradientPaint RED_XP
      • NIGHT_GRAY

        public static final java.awt.GradientPaint NIGHT_GRAY
      • NIGHT_GRAY_LIGHT

        public static final java.awt.GradientPaint NIGHT_GRAY_LIGHT
      • ORANGE_DELIGHT

        public static final java.awt.Paint ORANGE_DELIGHT
      • BLACK_STAR

        public static final java.awt.Paint BLACK_STAR
    • Constructor Detail

      • PaintUtils

        private PaintUtils()
    • Method Detail

      • resizeGradient

        public static java.awt.Paint resizeGradient​(java.awt.Paint p,
                                                    int width,
                                                    int height)
        Resizes a gradient to fill the width and height available. If the gradient is left to right it will be resized to fill the entire width. If the gradient is top to bottom it will be resized to fill the entire height. If the gradient is on an angle it will be resized to go from one corner to the other of the rectangle formed by (0,0 -> width,height). This method can resize java.awt.GradientPaint, java.awt.LinearGradientPaint, and the LinearGradientPaint implementation from Apache's Batik project. Note, this method does not require the MultipleGradientPaint.jar from Apache to compile or to run. MultipleGradientPaint.jar *is* required if you want to resize the LinearGradientPaint from that jar. Any paint passed into this method which is not a kind of gradient paint (like a Color or TexturePaint) will be returned unmodified. It will not throw an exception. If the gradient cannot be resized due to other errors the original paint will be returned unmodified. It will not throw an exception.
      • resizeLinearGradient

        private static java.awt.Paint resizeLinearGradient​(java.awt.Paint p,
                                                           int width,
                                                           int height)
      • invokeMethod

        private static java.lang.Object invokeMethod​(java.lang.Object p,
                                                     java.lang.String methodName)
                                              throws java.lang.NoSuchMethodException,
                                                     java.lang.reflect.InvocationTargetException,
                                                     java.lang.IllegalArgumentException,
                                                     java.lang.SecurityException,
                                                     java.lang.IllegalAccessException
        Throws:
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalArgumentException
        java.lang.SecurityException
        java.lang.IllegalAccessException
      • adjustPoints

        private static java.awt.geom.Point2D[] adjustPoints​(java.awt.geom.Point2D[] pts,
                                                            int width,
                                                            int height)
      • isNear

        private static boolean isNear​(double angle,
                                      double target,
                                      double error)
      • normalize

        private static float normalize​(double original,
                                       float target)
      • calcAngle

        private static double calcAngle​(java.awt.geom.Point2D p1,
                                        java.awt.geom.Point2D p2)
      • getCheckerPaint

        public static java.awt.Paint getCheckerPaint()
        Creates a new Paint that is a checkered effect using the colors gray and Color.WHITE.
        Returns:
        a the checkered paint
      • getCheckerPaint

        public static java.awt.Paint getCheckerPaint​(java.awt.Paint c1,
                                                     java.awt.Paint c2,
                                                     int size)
        Creates a new Paint that is a checkered effect using the specified colors.

        While this method supports transparent colors, this implementation performs painting operations using the second color after it performs operations using the first color. This means that to create a checkered paint with a fully-transparent color, you MUST specify that color first.

        Parameters:
        c1 - the first color
        c2 - the second color
        size - the size of the paint
        Returns:
        a new Paint checkering the supplied colors
      • toHexString

        public static java.lang.String toHexString​(java.awt.Color color)
        Creates a String that represents the supplied color as a hex-value RGB triplet, including the "#". The return value is suitable for use in HTML. The alpha (transparency) channel is neither include nor used in producing the string.
        Parameters:
        color - the color to convert
        Returns:
        the hex String
      • removeAlpha

        public static java.awt.Color removeAlpha​(java.awt.Color color)
        Returns a new color equal to the old one, except that there is no alpha (transparency) channel.

        This method is a convenience and has the same effect as setAlpha(color, 255).

        Parameters:
        color - the color to remove the alpha (transparency) from
        Returns:
        a new non-transparent Color
        Throws:
        java.lang.NullPointerException - if color is null
      • setAlpha

        public static java.awt.Color setAlpha​(java.awt.Color color,
                                              int alpha)
        Returns a new color equal to the old one, except alpha (transparency) channel is set to the new value.
        Parameters:
        color - the color to modify
        alpha - the new alpha (transparency) level. Must be an int between 0 and 255
        Returns:
        a new alpha-applied Color
        Throws:
        java.lang.IllegalArgumentException - if alpha is not between 0 and 255 inclusive
        java.lang.NullPointerException - if color is null
      • setSaturation

        public static java.awt.Color setSaturation​(java.awt.Color color,
                                                   float saturation)
        Returns a new color equal to the old one, except the saturation is set to the new value. The new color will have the same alpha (transparency) as the original color.

        The color is modified using HSB calculations. The saturation must be a float between 0 and 1. If 0 the resulting color will be gray. If 1 the resulting color will be the most saturated possible form of the passed in color.

        Parameters:
        color - the color to modify
        saturation - the saturation to use in the new color
        Returns:
        a new saturation-applied Color
        Throws:
        java.lang.IllegalArgumentException - if saturation is not between 0 and 1 inclusive
        java.lang.NullPointerException - if color is null
      • setBrightness

        public static java.awt.Color setBrightness​(java.awt.Color color,
                                                   float brightness)
        Returns a new color equal to the old one, except the brightness is set to the new value. The new color will have the same alpha (transparency) as the original color.

        The color is modified using HSB calculations. The brightness must be a float between 0 and 1. If 0 the resulting color will be black. If 1 the resulting color will be the brightest possible form of the passed in color.

        Parameters:
        color - the color to modify
        brightness - the brightness to use in the new color
        Returns:
        a new brightness-applied Color
        Throws:
        java.lang.IllegalArgumentException - if brightness is not between 0 and 1 inclusive
        java.lang.NullPointerException - if color is null
      • blend

        public static java.awt.Color blend​(java.awt.Color origin,
                                           java.awt.Color over)
        Blends two colors to create a new color. The origin color is the base for the new color and regardless of its alpha component, it is treated as fully opaque (alpha 255).
        Parameters:
        origin - the base of the new color
        over - the alpha-enabled color to add to the origin color
        Returns:
        a new color comprised of the origin and over colors
      • interpolate

        public static java.awt.Color interpolate​(java.awt.Color b,
                                                 java.awt.Color a,
                                                 float t)
        Interpolates a color.
        Parameters:
        b - the first color
        a - the second color
        t - the amount to interpolate
        Returns:
        a new color
      • computeForeground

        public static java.awt.Color computeForeground​(java.awt.Color bg)
        Computes an appropriate foreground color (either white or black) for the given background color.
        Parameters:
        bg - the background color
        Returns:
        Color.WHITE or Color.BLACK
        Throws:
        java.lang.NullPointerException - if bg is null