Package adams.gui.core
Class ColorHelper
- java.lang.Object
-
- adams.gui.core.ColorHelper
-
public class ColorHelper extends Object
Helper class for converting Colors to-and-from strings.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ColorHelper.ColorNotation
The type of color notation.
-
Constructor Summary
Constructors Constructor Description ColorHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Color
addAlpha(Color color, int alpha)
Creates a new color with the specified alpha value (255: opaque, 0: fully translucent).static Color
getContrastColor(Color color)
Returns a contrast color.static ColorHelper.ColorNotation
getNotation(String str)
Tries to determine the type of color notation.static Color
invert(Color color)
Inverts the color, ie.protected static String
toHex(int i)
Turns the integer into a hex string, left-pads with zero.static String
toHex(Color color)
Returns the color as hex string ("#RRGGBB" or "#AARRGGBB").static String
toName(Color color)
Tries to turn color into a name, like 'black' or 'red'.static String
toRGB(Color color)
Turns the color into RGB notation ("R,G,B" or "A,R,G,B" with A/R/G/B ranging from 0-255).static Color
valueOf(String str)
Returns a color generated from the string.static Color
valueOf(String str, Color defColor)
Returns a color generated from the string.
-
-
-
Method Detail
-
getNotation
public static ColorHelper.ColorNotation getNotation(String str)
Tries to determine the type of color notation.- Parameters:
str
- the color string to analyze- Returns:
- the used notation
-
valueOf
public static Color valueOf(String str)
Returns a color generated from the string. UsesColor.BLACK
as default color. Formats:- hex notation: #(AA)RRGGBB with AA/RR/GG/BB being hexadecimal strings
- RGB notation: (A,)R,G,B with A/R/G/B from 0-255 predefined names (case-insensitive) : black, blue, cyan, darkgray, darkgrey, gray, grey, green, lightgray, lightgrey, magenta, orange, pink, red, white, yellow
- Parameters:
str
- the string to convert to a color- Returns:
- the generated color
- See Also:
getNotation(String)
,valueOf(String, Color)
-
valueOf
public static Color valueOf(String str, Color defColor)
Returns a color generated from the string. Formats:- hex notation: #(AA)RRGGBB with AA/RR/GG/BB being hexadecimal strings
- hex notation: #RGB with R/G/B being hexadecimal shorthand strings
- RGB notation: (A,)R,G,B with A/R/G/B from 0-255 predefined names (case-insensitive) : black, blue, cyan, darkgray, darkgrey, gray, grey, green, lightgray, lightgrey, magenta, orange, pink, red, white, yellow
- Parameters:
str
- the string to convert to a colordefColor
- the default color if parsing fails- Returns:
- the generated color
- See Also:
getNotation(String)
-
toHex
protected static String toHex(int i)
Turns the integer into a hex string, left-pads with zero.- Parameters:
i
- the integer to convert- Returns:
- the generated string
-
toHex
public static String toHex(Color color)
Returns the color as hex string ("#RRGGBB" or "#AARRGGBB"). Alpha is only output if different from 255.- Parameters:
color
- the color to convert- Returns:
- the generated string
-
toRGB
public static String toRGB(Color color)
Turns the color into RGB notation ("R,G,B" or "A,R,G,B" with A/R/G/B ranging from 0-255). Alpha is only output if different from 255.- Parameters:
color
- the color to convert- Returns:
- the generated string
-
toName
public static String toName(Color color)
Tries to turn color into a name, like 'black' or 'red'. If it cannot find a match, null gets returned.- Parameters:
color
- the color to convert- Returns:
- the name of the color, null if no match
-
getContrastColor
public static Color getContrastColor(Color color)
- Parameters:
color
- the color to return the contrast color for- Returns:
- the contrast color
-
addAlpha
public static Color addAlpha(Color color, int alpha)
Creates a new color with the specified alpha value (255: opaque, 0: fully translucent).- Parameters:
color
- the color to use as basealpha
- the alpha value (0-255)- Returns:
- the new color
-
-