Class ColorToolkit


  • public class ColorToolkit
    extends java.lang.Object
    Toolkit for working with AWT colors.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static float normalHashValue  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ColorToolkit()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.Color blend​(java.awt.Color fg, java.awt.Color bg)  
      static java.awt.Color decode​(java.lang.String hexColor)
      Decode a string created by encode(Color) and create a corresponding color instance.
      static java.lang.String encode​(java.awt.Color color)
      Encode a color as a hexadecimal string starting with '#'.
      static java.awt.Color getDistinguishableColor​(java.lang.Object o)
      Generate a color based on an object.
      private static java.awt.Color getGradientBottomColor​(java.awt.Color topColor)  
      private static int getGradientComponent​(int c)  
      static java.awt.GradientPaint getGradientPaint​(java.awt.Color topColor, int top)
      Get a gradient paint based on a top color.
      static java.awt.Color withAlpha​(java.awt.Color color, int alpha)
      Create a color with a specified alpha value.
      • Methods inherited from class java.lang.Object

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

      • normalHashValue

        private static float normalHashValue
    • Constructor Detail

      • ColorToolkit

        private ColorToolkit()
    • Method Detail

      • encode

        public static java.lang.String encode​(java.awt.Color color)
        Encode a color as a hexadecimal string starting with '#'. This string can be decoded using decode(String).
        Parameters:
        color - color to encode
        Returns:
        a hexadecimal string representing the color
      • decode

        public static java.awt.Color decode​(java.lang.String hexColor)
                                     throws java.lang.NumberFormatException
        Decode a string created by encode(Color) and create a corresponding color instance.
        Parameters:
        hexColor - hexadecimal string to decode
        Returns:
        a color instance
        Throws:
        java.lang.NumberFormatException - if the string can't be decoded
      • getDistinguishableColor

        public static java.awt.Color getDistinguishableColor​(java.lang.Object o)
        Generate a color based on an object. The goal is to be able to run this on a number of different objects and get colors that are distinguishable from each other.

        The algorithm for generating colors is arbitrary and may be changed.

        Parameters:
        o - object to get a color for
        Returns:
        a color instance
      • getGradientPaint

        public static java.awt.GradientPaint getGradientPaint​(java.awt.Color topColor,
                                                              int top)
        Get a gradient paint based on a top color. The bottom color will be generated based on the top color.
        Parameters:
        topColor - color for the top of the gradient
        top - X coordinate for the top color
        Returns:
        a gradient with a generated bottom color at X=0 and the top color at X=top
      • getGradientBottomColor

        private static java.awt.Color getGradientBottomColor​(java.awt.Color topColor)
      • getGradientComponent

        private static int getGradientComponent​(int c)
      • withAlpha

        public static java.awt.Color withAlpha​(java.awt.Color color,
                                               int alpha)
        Create a color with a specified alpha value.
        Parameters:
        color - base color
        alpha - alpha value
        Returns:
        a color based on the base color and with the specified alpha value
      • blend

        public static java.awt.Color blend​(java.awt.Color fg,
                                           java.awt.Color bg)