Package mil.nga.color

Class ColorUtils


  • public class ColorUtils
    extends Object
    Color utilities with support for hex, RBG, arithmetic RBG, HSL, and integer colors
    Author:
    osbornb
    • Constructor Summary

      Constructors 
      Constructor Description
      ColorUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String expandShorthandHex​(String color)
      Expand the hex if it is in shorthand
      static String expandShorthandHexSingle​(String color)
      Expand the hex single if it is in shorthand
      static int getAlpha​(int color)
      Get the alpha color from color integer
      static String getAlpha​(String hex)
      Get the hex alpha color from the hex string if it exists
      static int getBlue​(int color)
      Get the blue color from color integer
      static String getBlue​(String hex)
      Get the hex blue color from the hex string
      static int getGreen​(int color)
      Get the green color from color integer
      static String getGreen​(String hex)
      Get the hex green color from the hex string
      static int getRed​(int color)
      Get the red color from color integer
      static String getRed​(String hex)
      Get the hex red color from the hex string
      static boolean isValidArithmeticRGB​(float color)
      Check if the arithmetic RGB float color is valid, inclusively between 0.0 and 1.0
      static boolean isValidHex​(String color)
      Check if the hex color value is valid
      static boolean isValidHexSingle​(String color)
      Check if the hex single color value is valid
      static boolean isValidHue​(float hue)
      Check if the HSL hue float value is valid, inclusively between 0.0 and 360.0
      static boolean isValidLightness​(float lightness)
      Check if the HSL lightness float value is valid, inclusively between 0.0 and 1.0
      static boolean isValidRGB​(int color)
      Check if the RBG integer color is valid, inclusively between 0 and 255
      static boolean isValidSaturation​(float saturation)
      Check if the HSL saturation float value is valid, inclusively between 0.0 and 1.0
      static String shorthandHex​(String color)
      Shorthand the hex color if possible
      static String shorthandHexSingle​(String color)
      Shorthand the hex single color if possible
      static float[] toArithmeticRGB​(float hue, float saturation, float lightness)
      Convert HSL (hue, saturation, and lightness) values to RGB arithmetic values
      static float toArithmeticRGB​(int color)
      Convert the RGB integer to an arithmetic RBG float
      static float toArithmeticRGB​(String color)
      Convert the hex single color to an arithmetic RBG float
      static int toColor​(int red, int green, int blue)
      Convert the RBG values to a color integer
      static String toColor​(String red, String green, String blue)
      Convert the hex color values to a hex color
      static String toColorShorthand​(String red, String green, String blue)
      Convert the hex color values to a hex color, shorthanded when possible
      static String toColorShorthandWithAlpha​(String red, String green, String blue)
      Convert the hex color values to a hex color including an opaque alpha value of FF or F, shorthanded when possible
      static String toColorShorthandWithAlpha​(String red, String green, String blue, String alpha)
      Convert the hex color values to a hex color, shorthanded when possible
      static int toColorWithAlpha​(int red, int green, int blue)
      Convert the RBG values to a color integer including an opaque alpha value of 255
      static int toColorWithAlpha​(int red, int green, int blue, int alpha)
      Convert the RBGA values to a color integer
      static String toColorWithAlpha​(String red, String green, String blue)
      Convert the hex color values to a hex color including an opaque alpha value of FF
      static String toColorWithAlpha​(String red, String green, String blue, String alpha)
      Convert the hex color values to a hex color
      static String toHex​(float color)
      Convert the arithmetic RGB float to a hex single color
      static String toHex​(int color)
      Convert the RGB integer to a hex single color
      static float[] toHSL​(float red, float green, float blue)
      Convert red, green, and blue arithmetic values to HSL (hue, saturation, lightness) values
      static float[] toHSL​(int red, int green, int blue)
      Convert red, green, and blue integer values to HSL (hue, saturation, lightness) values
      static int toRGB​(float color)
      Convert the arithmetic RGB float to a RBG integer
      static int[] toRGB​(float hue, float saturation, float lightness)
      Convert HSL (hue, saturation, and lightness) values to RGB integer values
      static int toRGB​(String color)
      Convert the hex single color to a RBG integer
      static void validateArithmeticRGB​(float color)
      Validate the arithmetic RGB float color is inclusively between 0.0 and 1.0
      static void validateHex​(String color)
      Validate the hex color value
      static void validateHexSingle​(String color)
      Validate the hex single color value
      static void validateHue​(float hue)
      Validate the HSL hue float value is inclusively between 0.0 and 360.0
      static void validateLightness​(float lightness)
      Validate the HSL lightness float value is inclusively between 0.0 and 1.0
      static void validateRGB​(int color)
      Validate the RBG integer color is inclusively between 0 and 255
      static void validateSaturation​(float saturation)
      Validate the HSL saturation float value is inclusively between 0.0 and 1.0
    • Constructor Detail

      • ColorUtils

        public ColorUtils()
    • Method Detail

      • toColor

        public static String toColor​(String red,
                                     String green,
                                     String blue)
        Convert the hex color values to a hex color
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        Returns:
        hex color in format #RRGGBB
      • toColorShorthand

        public static String toColorShorthand​(String red,
                                              String green,
                                              String blue)
        Convert the hex color values to a hex color, shorthanded when possible
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        Returns:
        hex color in format #RGB or #RRGGBB
      • toColorWithAlpha

        public static String toColorWithAlpha​(String red,
                                              String green,
                                              String blue)
        Convert the hex color values to a hex color including an opaque alpha value of FF
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        Returns:
        hex color in format #AARRGGBB
      • toColorShorthandWithAlpha

        public static String toColorShorthandWithAlpha​(String red,
                                                       String green,
                                                       String blue)
        Convert the hex color values to a hex color including an opaque alpha value of FF or F, shorthanded when possible
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        Returns:
        hex color in format #ARGB or #AARRGGBB
      • toColorWithAlpha

        public static String toColorWithAlpha​(String red,
                                              String green,
                                              String blue,
                                              String alpha)
        Convert the hex color values to a hex color
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        alpha - alpha hex color in format AA or A, null to not include alpha
        Returns:
        hex color in format #AARRGGBB or #RRGGBB
      • toColorShorthandWithAlpha

        public static String toColorShorthandWithAlpha​(String red,
                                                       String green,
                                                       String blue,
                                                       String alpha)
        Convert the hex color values to a hex color, shorthanded when possible
        Parameters:
        red - red hex color in format RR or R
        green - green hex color in format GG or G
        blue - blue hex color in format BB or B
        alpha - alpha hex color in format AA or A, null to not include alpha
        Returns:
        hex color in format #ARGB, #RGB, #AARRGGBB, or #RRGGBB
      • toColor

        public static int toColor​(int red,
                                  int green,
                                  int blue)
        Convert the RBG values to a color integer
        Parameters:
        red - red integer color inclusively between 0 and 255
        green - green integer color inclusively between 0 and 255
        blue - blue integer color inclusively between 0 and 255
        Returns:
        integer color
      • toColorWithAlpha

        public static int toColorWithAlpha​(int red,
                                           int green,
                                           int blue)
        Convert the RBG values to a color integer including an opaque alpha value of 255
        Parameters:
        red - red integer color inclusively between 0 and 255
        green - green integer color inclusively between 0 and 255
        blue - blue integer color inclusively between 0 and 255
        Returns:
        integer color
      • toColorWithAlpha

        public static int toColorWithAlpha​(int red,
                                           int green,
                                           int blue,
                                           int alpha)
        Convert the RBGA values to a color integer
        Parameters:
        red - red integer color inclusively between 0 and 255
        green - green integer color inclusively between 0 and 255
        blue - blue integer color inclusively between 0 and 255
        alpha - alpha integer color inclusively between 0 and 255, -1 to not include alpha
        Returns:
        integer color
      • toHex

        public static String toHex​(int color)
        Convert the RGB integer to a hex single color
        Parameters:
        color - integer color inclusively between 0 and 255
        Returns:
        hex single color in format FF
      • toHex

        public static String toHex​(float color)
        Convert the arithmetic RGB float to a hex single color
        Parameters:
        color - float color inclusively between 0.0 and 1.0
        Returns:
        hex single color in format FF
      • toRGB

        public static int toRGB​(String color)
        Convert the hex single color to a RBG integer
        Parameters:
        color - hex single color in format FF or F
        Returns:
        integer color inclusively between 0 and 255
      • toRGB

        public static int toRGB​(float color)
        Convert the arithmetic RGB float to a RBG integer
        Parameters:
        color - float color inclusively between 0.0 and 1.0
        Returns:
        integer color inclusively between 0 and 255
      • toArithmeticRGB

        public static float toArithmeticRGB​(String color)
        Convert the hex single color to an arithmetic RBG float
        Parameters:
        color - hex single color in format FF or F
        Returns:
        float color inclusively between 0.0 and 1.0
      • toArithmeticRGB

        public static float toArithmeticRGB​(int color)
        Convert the RGB integer to an arithmetic RBG float
        Parameters:
        color - integer color inclusively between 0 and 255
        Returns:
        float color inclusively between 0.0 and 1.0
      • toHSL

        public static float[] toHSL​(float red,
                                    float green,
                                    float blue)
        Convert red, green, and blue arithmetic values to HSL (hue, saturation, lightness) values
        Parameters:
        red - red color inclusively between 0.0 and 1.0
        green - green color inclusively between 0.0 and 1.0
        blue - blue color inclusively between 0.0 and 1.0
        Returns:
        HSL array where: 0 = hue, 1 = saturation, 2 = lightness
      • toHSL

        public static float[] toHSL​(int red,
                                    int green,
                                    int blue)
        Convert red, green, and blue integer values to HSL (hue, saturation, lightness) values
        Parameters:
        red - red color inclusively between 0 and 255
        green - green color inclusively between 0 and 255
        blue - blue color inclusively between 0 and 255
        Returns:
        HSL array where: 0 = hue, 1 = saturation, 2 = lightness
      • toArithmeticRGB

        public static float[] toArithmeticRGB​(float hue,
                                              float saturation,
                                              float lightness)
        Convert HSL (hue, saturation, and lightness) values to RGB arithmetic values
        Parameters:
        hue - hue value inclusively between 0.0 and 360.0
        saturation - saturation inclusively between 0.0 and 1.0
        lightness - lightness inclusively between 0.0 and 1.0
        Returns:
        arithmetic RGB array where: 0 = red, 1 = green, 2 = blue
      • toRGB

        public static int[] toRGB​(float hue,
                                  float saturation,
                                  float lightness)
        Convert HSL (hue, saturation, and lightness) values to RGB integer values
        Parameters:
        hue - hue value inclusively between 0.0 and 360.0
        saturation - saturation inclusively between 0.0 and 1.0
        lightness - lightness inclusively between 0.0 and 1.0
        Returns:
        RGB integer array where: 0 = red, 1 = green, 2 = blue
      • getRed

        public static String getRed​(String hex)
        Get the hex red color from the hex string
        Parameters:
        hex - hex color
        Returns:
        hex red color in format RR
      • getGreen

        public static String getGreen​(String hex)
        Get the hex green color from the hex string
        Parameters:
        hex - hex color
        Returns:
        hex green color in format GG
      • getBlue

        public static String getBlue​(String hex)
        Get the hex blue color from the hex string
        Parameters:
        hex - hex color
        Returns:
        hex blue color in format BB
      • getAlpha

        public static String getAlpha​(String hex)
        Get the hex alpha color from the hex string if it exists
        Parameters:
        hex - hex color
        Returns:
        hex alpha color in format AA or null
      • getRed

        public static int getRed​(int color)
        Get the red color from color integer
        Parameters:
        color - color integer
        Returns:
        red color
      • getGreen

        public static int getGreen​(int color)
        Get the green color from color integer
        Parameters:
        color - color integer
        Returns:
        green color
      • getBlue

        public static int getBlue​(int color)
        Get the blue color from color integer
        Parameters:
        color - color integer
        Returns:
        blue color
      • getAlpha

        public static int getAlpha​(int color)
        Get the alpha color from color integer
        Parameters:
        color - color integer
        Returns:
        alpha color
      • shorthandHex

        public static String shorthandHex​(String color)
        Shorthand the hex color if possible
        Parameters:
        color - hex color
        Returns:
        shorthand hex color or original value
      • expandShorthandHex

        public static String expandShorthandHex​(String color)
        Expand the hex if it is in shorthand
        Parameters:
        color - hex color
        Returns:
        expanded hex color or original value
      • shorthandHexSingle

        public static String shorthandHexSingle​(String color)
        Shorthand the hex single color if possible
        Parameters:
        color - hex single color
        Returns:
        shorthand hex color or original value
      • expandShorthandHexSingle

        public static String expandShorthandHexSingle​(String color)
        Expand the hex single if it is in shorthand
        Parameters:
        color - hex single color
        Returns:
        expanded hex color or original value
      • isValidHex

        public static boolean isValidHex​(String color)
        Check if the hex color value is valid
        Parameters:
        color - hex color
        Returns:
        true if valid
      • validateHex

        public static void validateHex​(String color)
        Validate the hex color value
        Parameters:
        color - hex color
      • isValidHexSingle

        public static boolean isValidHexSingle​(String color)
        Check if the hex single color value is valid
        Parameters:
        color - hex single color
        Returns:
        true if valid
      • validateHexSingle

        public static void validateHexSingle​(String color)
        Validate the hex single color value
        Parameters:
        color - hex single color
      • isValidRGB

        public static boolean isValidRGB​(int color)
        Check if the RBG integer color is valid, inclusively between 0 and 255
        Parameters:
        color - decimal color
        Returns:
        true if valid
      • validateRGB

        public static void validateRGB​(int color)
        Validate the RBG integer color is inclusively between 0 and 255
        Parameters:
        color - decimal color
      • isValidArithmeticRGB

        public static boolean isValidArithmeticRGB​(float color)
        Check if the arithmetic RGB float color is valid, inclusively between 0.0 and 1.0
        Parameters:
        color - decimal color
        Returns:
        true if valid
      • validateArithmeticRGB

        public static void validateArithmeticRGB​(float color)
        Validate the arithmetic RGB float color is inclusively between 0.0 and 1.0
        Parameters:
        color - decimal color
      • isValidHue

        public static boolean isValidHue​(float hue)
        Check if the HSL hue float value is valid, inclusively between 0.0 and 360.0
        Parameters:
        hue - hue value
        Returns:
        true if valid
      • validateHue

        public static void validateHue​(float hue)
        Validate the HSL hue float value is inclusively between 0.0 and 360.0
        Parameters:
        hue - hue value
      • isValidSaturation

        public static boolean isValidSaturation​(float saturation)
        Check if the HSL saturation float value is valid, inclusively between 0.0 and 1.0
        Parameters:
        saturation - saturation value
        Returns:
        true if valid
      • validateSaturation

        public static void validateSaturation​(float saturation)
        Validate the HSL saturation float value is inclusively between 0.0 and 1.0
        Parameters:
        saturation - saturation value
      • isValidLightness

        public static boolean isValidLightness​(float lightness)
        Check if the HSL lightness float value is valid, inclusively between 0.0 and 1.0
        Parameters:
        lightness - lightness value
        Returns:
        true if valid
      • validateLightness

        public static void validateLightness​(float lightness)
        Validate the HSL lightness float value is inclusively between 0.0 and 1.0
        Parameters:
        lightness - lightness value