Class ColorHelper


  • public final class ColorHelper
    extends Object
    Helper to convert from color to HTML string and back.
    • Method Detail

      • html2color

        public static Color html2color​(String html)
        Returns the Color for the given HTML code.
        Parameters:
        html - the color code
        Returns:
        the color
      • color2html

        public static String color2html​(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 String color2html​(Color color,
                                        boolean withAlpha)
        Returns the HTML color code (6 or 8 digit).
        Parameters:
        color - The color to convert
        withAlpha - if true 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 Color getForegroundColor​(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 or Color#WHITE
        Since:
        9223
      • float2int

        public static Integer float2int​(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 Float int2float​(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 Color alphaMultiply​(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 color
        alphaFactor - The factor to multiply alpha with.
        Returns:
        The new color.
        Since:
        11692
      • complement

        public static Color complement​(Color clr)
        Returns the complementary color of clr.
        Parameters:
        clr - the color to complement
        Returns:
        the complementary color of clr