Package mil.nga.color
Class ColorUtils
- java.lang.Object
-
- mil.nga.color.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 StringexpandShorthandHex(String color)Expand the hex if it is in shorthandstatic StringexpandShorthandHexSingle(String color)Expand the hex single if it is in shorthandstatic intgetAlpha(int color)Get the alpha color from color integerstatic StringgetAlpha(String hex)Get the hex alpha color from the hex string if it existsstatic intgetBlue(int color)Get the blue color from color integerstatic StringgetBlue(String hex)Get the hex blue color from the hex stringstatic intgetGreen(int color)Get the green color from color integerstatic StringgetGreen(String hex)Get the hex green color from the hex stringstatic intgetRed(int color)Get the red color from color integerstatic StringgetRed(String hex)Get the hex red color from the hex stringstatic booleanisValidArithmeticRGB(float color)Check if the arithmetic RGB float color is valid, inclusively between 0.0 and 1.0static booleanisValidHex(String color)Check if the hex color value is validstatic booleanisValidHexSingle(String color)Check if the hex single color value is validstatic booleanisValidHue(float hue)Check if the HSL hue float value is valid, inclusively between 0.0 and 360.0static booleanisValidLightness(float lightness)Check if the HSL lightness float value is valid, inclusively between 0.0 and 1.0static booleanisValidRGB(int color)Check if the RBG integer color is valid, inclusively between 0 and 255static booleanisValidSaturation(float saturation)Check if the HSL saturation float value is valid, inclusively between 0.0 and 1.0static StringshorthandHex(String color)Shorthand the hex color if possiblestatic StringshorthandHexSingle(String color)Shorthand the hex single color if possiblestatic float[]toArithmeticRGB(float hue, float saturation, float lightness)Convert HSL (hue, saturation, and lightness) values to RGB arithmetic valuesstatic floattoArithmeticRGB(int color)Convert the RGB integer to an arithmetic RBG floatstatic floattoArithmeticRGB(String color)Convert the hex single color to an arithmetic RBG floatstatic inttoColor(int red, int green, int blue)Convert the RBG values to a color integerstatic StringtoColor(String red, String green, String blue)Convert the hex color values to a hex colorstatic StringtoColorShorthand(String red, String green, String blue)Convert the hex color values to a hex color, shorthanded when possiblestatic StringtoColorShorthandWithAlpha(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 possiblestatic StringtoColorShorthandWithAlpha(String red, String green, String blue, String alpha)Convert the hex color values to a hex color, shorthanded when possiblestatic inttoColorWithAlpha(int red, int green, int blue)Convert the RBG values to a color integer including an opaque alpha value of 255static inttoColorWithAlpha(int red, int green, int blue, int alpha)Convert the RBGA values to a color integerstatic StringtoColorWithAlpha(String red, String green, String blue)Convert the hex color values to a hex color including an opaque alpha value of FFstatic StringtoColorWithAlpha(String red, String green, String blue, String alpha)Convert the hex color values to a hex colorstatic StringtoHex(float color)Convert the arithmetic RGB float to a hex single colorstatic StringtoHex(int color)Convert the RGB integer to a hex single colorstatic float[]toHSL(float red, float green, float blue)Convert red, green, and blue arithmetic values to HSL (hue, saturation, lightness) valuesstatic float[]toHSL(int red, int green, int blue)Convert red, green, and blue integer values to HSL (hue, saturation, lightness) valuesstatic inttoRGB(float color)Convert the arithmetic RGB float to a RBG integerstatic int[]toRGB(float hue, float saturation, float lightness)Convert HSL (hue, saturation, and lightness) values to RGB integer valuesstatic inttoRGB(String color)Convert the hex single color to a RBG integerstatic voidvalidateArithmeticRGB(float color)Validate the arithmetic RGB float color is inclusively between 0.0 and 1.0static voidvalidateHex(String color)Validate the hex color valuestatic voidvalidateHexSingle(String color)Validate the hex single color valuestatic voidvalidateHue(float hue)Validate the HSL hue float value is inclusively between 0.0 and 360.0static voidvalidateLightness(float lightness)Validate the HSL lightness float value is inclusively between 0.0 and 1.0static voidvalidateRGB(int color)Validate the RBG integer color is inclusively between 0 and 255static voidvalidateSaturation(float saturation)Validate the HSL saturation float value is inclusively between 0.0 and 1.0
-
-
-
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 Rgreen- green hex color in format GG or Gblue- 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 Rgreen- green hex color in format GG or Gblue- 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 Rgreen- green hex color in format GG or Gblue- 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 Rgreen- green hex color in format GG or Gblue- 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 Rgreen- green hex color in format GG or Gblue- blue hex color in format BB or Balpha- 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 Rgreen- green hex color in format GG or Gblue- blue hex color in format BB or Balpha- 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 255green- green integer color inclusively between 0 and 255blue- 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 255green- green integer color inclusively between 0 and 255blue- 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 255green- green integer color inclusively between 0 and 255blue- blue integer color inclusively between 0 and 255alpha- 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.0green- green color inclusively between 0.0 and 1.0blue- 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 255green- green color inclusively between 0 and 255blue- 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.0saturation- saturation inclusively between 0.0 and 1.0lightness- 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.0saturation- saturation inclusively between 0.0 and 1.0lightness- 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
-
-