Package jmri.util
Class ColorUtil
- java.lang.Object
-
- jmri.util.ColorUtil
-
public class ColorUtil extends java.lang.Object
A collection of utilities related to colors.
-
-
Field Summary
Fields Modifier and Type Field Description static java.awt.Color
BROWN
static java.awt.Color
clear
static java.awt.Color
CLEAR
static java.lang.String
ColorBlack
static java.lang.String
ColorBlue
static java.lang.String
ColorBrown
static java.lang.String
ColorClear
static java.lang.String
ColorCyan
static java.lang.String
ColorDarkGray
static java.lang.String
ColorGray
static java.lang.String
ColorGreen
static java.lang.String
ColorLightGray
static java.lang.String
ColorMagenta
static java.lang.String
ColorOrange
static java.lang.String
ColorPink
static java.lang.String
ColorRed
static java.lang.String
ColorTrack
static java.lang.String
ColorWhite
static java.lang.String
ColorYellow
-
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)
-
-
-
Field Detail
-
ColorTrack
public static final java.lang.String ColorTrack
- See Also:
- Constant Field Values
-
ColorBlack
public static final java.lang.String ColorBlack
- See Also:
- Constant Field Values
-
ColorDarkGray
public static final java.lang.String ColorDarkGray
- See Also:
- Constant Field Values
-
ColorGray
public static final java.lang.String ColorGray
- See Also:
- Constant Field Values
-
ColorLightGray
public static final java.lang.String ColorLightGray
- See Also:
- Constant Field Values
-
ColorWhite
public static final java.lang.String ColorWhite
- See Also:
- Constant Field Values
-
ColorRed
public static final java.lang.String ColorRed
- See Also:
- Constant Field Values
-
ColorPink
public static final java.lang.String ColorPink
- See Also:
- Constant Field Values
-
ColorOrange
public static final java.lang.String ColorOrange
- See Also:
- Constant Field Values
-
ColorYellow
public static final java.lang.String ColorYellow
- See Also:
- Constant Field Values
-
ColorGreen
public static final java.lang.String ColorGreen
- See Also:
- Constant Field Values
-
ColorBlue
public static final java.lang.String ColorBlue
- See Also:
- Constant Field Values
-
ColorMagenta
public static final java.lang.String ColorMagenta
- See Also:
- Constant Field Values
-
ColorCyan
public static final java.lang.String ColorCyan
- See Also:
- Constant Field Values
-
ColorClear
public static final java.lang.String ColorClear
- See Also:
- Constant Field Values
-
ColorBrown
public static final java.lang.String ColorBrown
- See Also:
- Constant Field Values
-
clear
public static final java.awt.Color clear
-
CLEAR
public static final java.awt.Color CLEAR
-
BROWN
public static final java.awt.Color BROWN
-
-
Constructor Detail
-
ColorUtil
public ColorUtil()
-
-
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 colorcolorB
- the second colort
- 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 coloralpha
- 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 coloralpha
- the alpha component (double 0.0 - 1.0)- Returns:
- the new color with the specified alpha
-
-