Class ColorUtil

java.lang.Object
com.jalios.util.ColorUtil

public class ColorUtil extends Object
This class provides utility methods to manage colors.
Since:
jcms-5.5.0
Author:
Olivier Dedieu
  • Constructor Details

    • ColorUtil

      public ColorUtil()
  • Method Details

    • getColor

      public static Color getColor(String colorStr)
      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

      public static String getStringColor(Color color)
      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

      public static String addOffset(String colorStr, int offset)
      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

      public static Color addOffset(Color color, int offset)
      Add an offset to the given color and return the corresponding color
      Parameters:
      color - the color to process
      offset - the offset to apply
      Returns:
      the modified color
      Since:
      jcms-5.5.0
      See Also:
    • getForegroundColor

      public static String getForegroundColor(String bgColorStr)
      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

      public static String getForegroundColor(String bgColorStr, int threshold)
      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

      public static Color getForegroundColor(Color bgColor)
      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

      public static Color getForegroundColor(Color bgColor, int threshold)
      Parameters:
      bgColor - the background color
      threshold - 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

      public static String adjustLightnessToHsl(String hex, float newLightnessPercent)
      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

      public static String getClosestColor(String targetColor, Collection<String> colors)