Class ColorFunctions


  • public class ColorFunctions
    extends java.lang.Object
    Functions that modify colors.
    • Constructor Summary

      Constructors 
      Constructor Description
      ColorFunctions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.Color applyFunctions​(java.awt.Color color, ColorFunctions.ColorFunction... functions)
      Applies the given color functions to the given color and returns the new color.
      static float clamp​(float value)
      Clamps the given value between 0 and 100.
      static java.awt.Color darken​(java.awt.Color color, float amount)
      Decrease the lightness of a color in HSL color space by an absolute amount.
      static java.awt.Color desaturate​(java.awt.Color color, float amount)
      Decrease the saturation of a color in HSL color space by an absolute amount.
      static java.awt.Color fade​(java.awt.Color color, float amount)
      Set the opacity (alpha) of a color.
      private static float gammaCorrection​(float value)  
      private static java.awt.Color hslIncreaseDecrease​(java.awt.Color color, float amount, int hslIndex, boolean increase)  
      static java.awt.Color lighten​(java.awt.Color color, float amount)
      Increase the lightness of a color in HSL color space by an absolute amount.
      static float luma​(java.awt.Color color)
      Calculates the luma (perceptual brightness) of the given color.
      static java.awt.Color mix​(java.awt.Color color1, java.awt.Color color2, float weight)
      Returns a color that is a mixture of two colors.
      static java.awt.Color saturate​(java.awt.Color color, float amount)
      Increase the saturation of a color in HSL color space by an absolute amount.
      static java.awt.Color shade​(java.awt.Color color, float weight)
      Mix color with black, which makes the color darker.
      static java.awt.Color spin​(java.awt.Color color, float angle)
      Rotate the hue angle (0-360) of a color in HSL color space in either direction.
      static java.awt.Color tint​(java.awt.Color color, float weight)
      Mix color with white, which makes the color brighter.
      • Methods inherited from class java.lang.Object

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

      • ColorFunctions

        public ColorFunctions()
    • Method Detail

      • lighten

        public static java.awt.Color lighten​(java.awt.Color color,
                                             float amount)
        Increase the lightness of a color in HSL color space by an absolute amount.

        Consider using tint(Color, float) as alternative.

        Parameters:
        color - base color
        amount - the amount (in range 0-1) that is added to the lightness
        Returns:
        new color
        Since:
        2
      • darken

        public static java.awt.Color darken​(java.awt.Color color,
                                            float amount)
        Decrease the lightness of a color in HSL color space by an absolute amount.

        Consider using shade(Color, float) as alternative.

        Parameters:
        color - base color
        amount - the amount (in range 0-1) that is subtracted from the lightness
        Returns:
        new color
        Since:
        2
      • saturate

        public static java.awt.Color saturate​(java.awt.Color color,
                                              float amount)
        Increase the saturation of a color in HSL color space by an absolute amount.
        Parameters:
        color - base color
        amount - the amount (in range 0-1) that is added to the saturation
        Returns:
        new color
        Since:
        2
      • desaturate

        public static java.awt.Color desaturate​(java.awt.Color color,
                                                float amount)
        Decrease the saturation of a color in HSL color space by an absolute amount.
        Parameters:
        color - base color
        amount - the amount (in range 0-1) that is subtracted from the saturation
        Returns:
        new color
        Since:
        2
      • spin

        public static java.awt.Color spin​(java.awt.Color color,
                                          float angle)
        Rotate the hue angle (0-360) of a color in HSL color space in either direction.
        Parameters:
        color - base color
        angle - the number of degrees to rotate (in range -360 - 360)
        Returns:
        new color
        Since:
        2
      • hslIncreaseDecrease

        private static java.awt.Color hslIncreaseDecrease​(java.awt.Color color,
                                                          float amount,
                                                          int hslIndex,
                                                          boolean increase)
      • fade

        public static java.awt.Color fade​(java.awt.Color color,
                                          float amount)
        Set the opacity (alpha) of a color.
        Parameters:
        color - base color
        amount - the amount (in range 0-1) of the new opacity
        Returns:
        new color
        Since:
        3
      • mix

        public static java.awt.Color mix​(java.awt.Color color1,
                                         java.awt.Color color2,
                                         float weight)
        Returns a color that is a mixture of two colors.

        This can be used to animate a color change from color1 to color2 by invoking this method multiple times with growing weight (from 0 to 1).

        Parameters:
        color1 - first color
        color2 - second color
        weight - the weight (in range 0-1) to mix the two colors. Larger weight uses more of first color, smaller weight more of second color.
        Returns:
        mixture of colors
      • tint

        public static java.awt.Color tint​(java.awt.Color color,
                                          float weight)
        Mix color with white, which makes the color brighter. This is the same as mix(java.awt.Color, java.awt.Color, float)(Color.white, color, weight).
        Parameters:
        color - second color
        weight - the weight (in range 0-1) to mix the two colors. Larger weight uses more of first color, smaller weight more of second color.
        Returns:
        mixture of colors
        Since:
        2
      • shade

        public static java.awt.Color shade​(java.awt.Color color,
                                           float weight)
        Mix color with black, which makes the color darker. This is the same as mix(java.awt.Color, java.awt.Color, float)(Color.black, color, weight).
        Parameters:
        color - second color
        weight - the weight (in range 0-1) to mix the two colors. Larger weight uses more of first color, smaller weight more of second color.
        Returns:
        mixture of colors
        Since:
        2
      • luma

        public static float luma​(java.awt.Color color)
        Calculates the luma (perceptual brightness) of the given color.

        Uses SMPTE C / Rec. 709 coefficients, as recommended in WCAG 2.0.

        Parameters:
        color - a color
        Returns:
        the luma (in range 0-1)
        Since:
        2
        See Also:
        https://en.wikipedia.org/wiki/Luma_(video)
      • gammaCorrection

        private static float gammaCorrection​(float value)
      • applyFunctions

        public static java.awt.Color applyFunctions​(java.awt.Color color,
                                                    ColorFunctions.ColorFunction... functions)
        Applies the given color functions to the given color and returns the new color.
      • clamp

        public static float clamp​(float value)
        Clamps the given value between 0 and 100.