Package com.jalios.util
Class ColorUtil
java.lang.Object
com.jalios.util.ColorUtil
This class provides utility methods to manage colors.
- Since:
- jcms-5.5.0
- Author:
- Olivier Dedieu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ColorAdd an offset to the given color and return the corresponding colorstatic StringAdd an offset to the given color and return an hexadecimal representation of the corresponding colorstatic StringadjustLightnessToHsl(String hex, float newLightnessPercent) Converts a hexadecimal color into HSL and replaces its lightness (L) value with the given percentage.static StringgetClosestColor(String targetColor, Collection<String> colors) static ColorReturn a color from a given hexadecimal string (eg #FF0000 will return a pure red.static ColorgetForegroundColor(Color bgColor) static ColorgetForegroundColor(Color bgColor, int threshold) static StringgetForegroundColor(String bgColorStr) static StringgetForegroundColor(String bgColorStr, int threshold) static StringgetStringColor(Color color) Returns the hexadecimal represention of this color.static float[]rgbToHsl(int r, int g, int b) Converts an RGB color (0–255 each) into its HSL representation.
-
Constructor Details
-
ColorUtil
public ColorUtil()
-
-
Method Details
-
getColor
Return a color from a given hexadecimal string (eg #FF0000 will return a pure red. Leading # is optional).- Parameters:
colorStr- the string to be parsed- Returns:
- a Color (or null if a parsing error occured)
- Since:
- jcms-5.5.0
-
getStringColor
Returns the hexadecimal represention of this color. Eg for a pure red color (100%, 0, 0), it returns "#ff0000"- Parameters:
color- the color to process- Returns:
- the hexadecimal represention of this color.
- Since:
- jcms-5.5.0
-
addOffset
Add an offset to the given color and return an hexadecimal representation of the corresponding color- Parameters:
colorStr- the hexadecimal representation of the color (eg. "#ff0000");offset- the offset to apply- Returns:
- an hexadecimal representation of the modified color
- Since:
- jcms-5.5.0
- See Also:
-
addOffset
Add an offset to the given color and return the corresponding color- Parameters:
color- the color to processoffset- the offset to apply- Returns:
- the modified color
- Since:
- jcms-5.5.0
- See Also:
-
getForegroundColor
- Parameters:
bgColorStr- an hexadecimal string of the background color (eg "#FF0000" for red background)- Returns:
- the foreground color ("#000000" or "#FFFFFF") to use according the given background color.
- Since:
- jcms-10.0.7
-
getForegroundColor
- Parameters:
bgColorStr- an hexadecimal string of the background color (eg "#FF0000" for red background)threshold- the brightness threshold (e.g. 125)- Returns:
- the foreground color ("#000000" or "#FFFFFF") to use according the given background color.
- Since:
- jcms-10.0.7
-
getForegroundColor
- Parameters:
bgColor- the background color- Returns:
- the foreground color (back or white) to use according the given background color.
- Since:
- jcms-10.0.7
-
getForegroundColor
- Parameters:
bgColor- the background colorthreshold- the brightness threshold (e.g. 125)- Returns:
- the foreground color (back or white) to use according the given background color.
- Since:
- jcms-10.0.7
-
adjustLightnessToHsl
Converts a hexadecimal color into HSL and replaces its lightness (L) value with the given percentage. The resulting HSL is returned as a CSS-ready string of the form:hsl(H S% L% / 1)
The input hex value may include or omit the leading
#.Example:
adjustLightnessToHsl("#F3BE5F", 89f) → "hsl(39 86% 89% / 1)"- Parameters:
hex- the color in hexadecimal RGB format (e.g."#F3BE5F"or"F3BE5F")newLightnessPercent- the new lightness value to apply (0–100)- Returns:
- a CSS HSL color string:
hsl(H S% L% / 1) - Since:
- jplatform-11.0.0
-
rgbToHsl
public static float[] rgbToHsl(int r, int g, int b) Converts an RGB color (0–255 each) into its HSL representation.The returned array contains:
- H – hue in degrees (0–360)
- S – saturation (0–1)
- L – lightness (0–1)
This method does not clamp values; input must be inside the 0–255 range.
- Parameters:
r- the red component (0–255)g- the green component (0–255)b- the blue component (0–255)- Returns:
- an array
{H, S, L}where H is in degrees and S/L are normalized floats - Since:
- jplatform-11.0.0
-
getClosestColor
-