Package jmri.util

Class ColorUtil


  • public class ColorUtil
    extends java.lang.Object
    A collection of utilities related to colors.
    • Constructor Summary

      Constructors 
      Constructor Description
      ColorUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String colorToColorName​(java.awt.Color color)
      Returns known color name or hex value in form #RRGGBB.
      static java.lang.String colorToHexString​(java.awt.Color color)
      Convert a color into hex value of form #RRGGBB.
      static java.lang.String colorToLocalizedName​(java.awt.Color color)
      Returns localized color name or hex value in form #RRGGBB.
      static java.lang.String colorToString​(java.awt.Color color)
      Handles known colors plus special value for track.
      static java.awt.Color contrast​(java.awt.Color color)
      Return the color (Black/White) that most contrasts with the specified color.
      static java.awt.Color lerp​(java.awt.Color colorA, java.awt.Color colorB, double t)
      Calculate the linear interpolation between two colors.
      static java.awt.Color setAlpha​(java.awt.Color color, double alpha)
      Set the alpha component of a color.
      static java.awt.Color setAlpha​(java.awt.Color color, int alpha)
      Set the alpha component of a color.
      static java.awt.Color stringToColor​(java.lang.String string)  
      • Methods inherited from class java.lang.Object

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

      • colorToString

        @Nonnull
        public static java.lang.String colorToString​(@CheckForNull
                                                     java.awt.Color color)
        Handles known colors plus special value for track.
        Parameters:
        color - the color or null
        Returns:
        the name of the color or "black" if a color was provided; "track" if color is null
      • colorToColorName

        @CheckForNull
        public static java.lang.String colorToColorName​(@CheckForNull
                                                        java.awt.Color color)
        Returns known color name or hex value in form #RRGGBB.
        Parameters:
        color - the color
        Returns:
        the name or hex value of color; returns null if color is null
      • colorToLocalizedName

        @CheckForNull
        public static java.lang.String colorToLocalizedName​(@CheckForNull
                                                            java.awt.Color color)
        Returns localized color name or hex value in form #RRGGBB.
        Parameters:
        color - the color object
        Returns:
        the localized name or hex value of color; returns null if color is null
        Since:
        4.13.1
      • stringToColor

        public static java.awt.Color stringToColor​(java.lang.String string)
        Parameters:
        string - Either a hexadecimal representation of the rgb value of a color or a color name defined as a constant.
        Returns:
        the color from the string or null if the string equals "track" or equals the localized value for "None"
        Throws:
        java.lang.IllegalArgumentException - if string cannot be converted into a Color
      • colorToHexString

        @CheckForNull
        public static java.lang.String colorToHexString​(@CheckForNull
                                                        java.awt.Color color)
        Convert a color into hex value of form #RRGGBB.
        Parameters:
        color - the color or null
        Returns:
        the hex string or null if color is null
      • contrast

        public static java.awt.Color contrast​(@Nonnull
                                              java.awt.Color color)
        Return the color (Black/White) that most contrasts with the specified color.
        Parameters:
        color - the source color
        Returns:
        the contrasting color
      • lerp

        @CheckReturnValue
        public static java.awt.Color lerp​(@Nonnull
                                          java.awt.Color colorA,
                                          @Nonnull
                                          java.awt.Color colorB,
                                          double t)
        Calculate the linear interpolation between two colors.
        Parameters:
        colorA - the first color
        colorB - the second color
        t - the fraction (between 0 and 1)
        Returns:
        the linear interpolation between a and b for t
      • setAlpha

        @CheckReturnValue
        public static java.awt.Color setAlpha​(@Nonnull
                                              java.awt.Color color,
                                              int alpha)
        Set the alpha component of a color.
        Parameters:
        color - the color
        alpha - the alpha component (integer 0 - 255)
        Returns:
        the new color with the specified alpha
      • setAlpha

        @CheckReturnValue
        public static java.awt.Color setAlpha​(@Nonnull
                                              java.awt.Color color,
                                              double alpha)
        Set the alpha component of a color.
        Parameters:
        color - the color
        alpha - the alpha component (double 0.0 - 1.0)
        Returns:
        the new color with the specified alpha