Package org.openstreetmap.josm.tools
Class ColorHelper
- java.lang.Object
-
- org.openstreetmap.josm.tools.ColorHelper
-
public final class ColorHelper extends java.lang.Object
Helper to convert from color to HTML string and back.
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ColorHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.awt.Color
alphaMultiply(java.awt.Color color, float alphaFactor)
Multiply the alpha value of the given color with the factor.static double
calculateContrastRatio(java.awt.Color first, java.awt.Color second)
Calculate the contrast between two colors (e.g.private static double
calculateLuminance(java.awt.Color color)
Calculate the relative "luminance" of a color.private static double
calculateLuminanceStepFunction(double color)
This is a step function forcalculateLuminance(Color)
static java.lang.String
color2html(java.awt.Color col)
Returns the HTML color code (6 or 8 digit).static java.lang.String
color2html(java.awt.Color color, boolean withAlpha)
Returns the HTML color code (6 or 8 digit).static java.awt.Color
complement(java.awt.Color clr)
Returns the complementary color ofclr
.static java.lang.Integer
float2int(java.lang.Float val)
convert float range 0 <= x <= 1 to integer range 0..255 when dealing with colors and color alpha valuestatic java.awt.Color
getForegroundColor(java.awt.Color bg)
Determines the correct foreground color (black or white) to use for the given background, so the text will be readable.static java.awt.Color
html2color(java.lang.String html)
Returns theColor
for the given HTML code.static java.lang.Float
int2float(java.lang.Integer val)
convert integer range 0..255 to float range 0 <= x <= 1 when dealing with colors and color alpha value
-
-
-
Constructor Detail
-
ColorHelper
private ColorHelper()
-
-
Method Detail
-
html2color
public static java.awt.Color html2color(java.lang.String html)
Returns theColor
for the given HTML code.- Parameters:
html
- the color code- Returns:
- the color
-
color2html
public static java.lang.String color2html(java.awt.Color col)
Returns the HTML color code (6 or 8 digit).- Parameters:
col
- The color to convert- Returns:
- the HTML color code (6 or 8 digit)
-
color2html
public static java.lang.String color2html(java.awt.Color color, boolean withAlpha)
Returns the HTML color code (6 or 8 digit).- Parameters:
color
- The color to convertwithAlpha
- iftrue
and alpha value < 255, return 8-digit color code, else always 6-digit- Returns:
- the HTML color code (6 or 8 digit)
- Since:
- 6655
-
getForegroundColor
public static java.awt.Color getForegroundColor(java.awt.Color bg)
Determines the correct foreground color (black or white) to use for the given background, so the text will be readable.- Parameters:
bg
- background color- Returns:
Color#BLACK
orColor#WHITE
- Since:
- 9223
-
float2int
public static java.lang.Integer float2int(java.lang.Float val)
convert float range 0 <= x <= 1 to integer range 0..255 when dealing with colors and color alpha value- Parameters:
val
- float value between 0 and 1- Returns:
- null if val is null, the corresponding int if val is in the range 0...1. If val is outside that range, return 255
-
int2float
public static java.lang.Float int2float(java.lang.Integer val)
convert integer range 0..255 to float range 0 <= x <= 1 when dealing with colors and color alpha value- Parameters:
val
- integer value- Returns:
- corresponding float value in range 0 <= x <= 1
-
alphaMultiply
public static java.awt.Color alphaMultiply(java.awt.Color color, float alphaFactor)
Multiply the alpha value of the given color with the factor. The alpha value is clamped to 0..255- Parameters:
color
- The coloralphaFactor
- The factor to multiply alpha with.- Returns:
- The new color.
- Since:
- 11692
-
complement
public static java.awt.Color complement(java.awt.Color clr)
Returns the complementary color ofclr
.- Parameters:
clr
- the color to complement- Returns:
- the complementary color of
clr
-
calculateLuminance
private static double calculateLuminance(java.awt.Color color)
Calculate the relative "luminance" of a color. This is mostly useful for choosing background/foreground colours- See Also:
- constrast ratio
-
calculateLuminanceStepFunction
private static double calculateLuminanceStepFunction(double color)
This is a step function forcalculateLuminance(Color)
- Parameters:
color
- The color to get the values for- Returns:
- The value to use when calculating relative luminance
-
calculateContrastRatio
public static double calculateContrastRatio(java.awt.Color first, java.awt.Color second)
Calculate the contrast between two colors (e.g.Color.black
andColor.white
).- Parameters:
first
- The first color to usesecond
- The second color to use- Returns:
- The contrast ratio ((L1 + 0.05)/(L2 + 0.05))
- Since:
- 19236
-
-