Class CSSColorHelper


  • @Immutable
    public final class CSSColorHelper
    extends Object
    Provides color handling sanity methods.
    Author:
    Philip Helger
    • Method Detail

      • isRGBColorValue

        public static boolean isRGBColorValue​(@Nullable
                                              String sValue)
        Check if the passed String is valid CSS RGB color value. Example value: rgb(255,0,0)
        Parameters:
        sValue - The value to check. May be null.
        Returns:
        true if it is a CSS RGB color value, false if not
      • getParsedRGBColorValue

        @Nullable
        public static CSSRGB getParsedRGBColorValue​(@Nullable
                                                    String sValue)
        Extract the CSS RGB color value from the passed String. Example value: rgb(255,0,0)
        Parameters:
        sValue - The value to extract the value from. May be null.
        Returns:
        null if the passed value is not a valid CSS RGB color value.
      • isRGBAColorValue

        public static boolean isRGBAColorValue​(@Nullable
                                               String sValue)
        Check if the passed String is valid CSS RGBA color value. Example value: rgba(255,0,0, 0.1)
        Parameters:
        sValue - The value to check. May be null.
        Returns:
        true if it is a CSS RGBA color value, false if not
      • isHSLColorValue

        public static boolean isHSLColorValue​(@Nullable
                                              String sValue)
        Check if the passed String is valid CSS HSL color value. Example value: hsl(255,0%,0%)
        Parameters:
        sValue - The value to check. May be null.
        Returns:
        true if it is a CSS HSL color value, false if not
      • isHSLAColorValue

        public static boolean isHSLAColorValue​(@Nullable
                                               String sValue)
        Check if the passed String is valid CSS HSLA color value. Example value: hsla(255,0%,0%, 0.1)
        Parameters:
        sValue - The value to check. May be null.
        Returns:
        true if it is a CSS HSLA color value, false if not
      • isHexColorValue

        public static boolean isHexColorValue​(@Nullable
                                              String sValue)
        Check if the passed String is valid CSS hex color value. Example value: #ff0000
        Parameters:
        sValue - The value to check. May be null.
        Returns:
        true if it is a CSS hex color value, false if not
      • getRGBValue

        @Nonnegative
        public static int getRGBValue​(int nRGBPart)
        Convert the passed value to a valid RGB value in the range 0-255.
        Parameters:
        nRGBPart - The original value
        Returns:
        The value between 0 and 255.
      • getOpacityToUse

        @Nonnegative
        public static float getOpacityToUse​(float fOpacity)
        Ensure that the passed opacity value is in the range OPACITY_MIN and OPACITY_MAX.
        Parameters:
        fOpacity - The original opacity
        Returns:
        The opacity in the correct range between 0 and 1.
      • getRGBColorValue

        @Nonnull
        @Nonempty
        public static String getRGBColorValue​(int nRed,
                                              int nGreen,
                                              int nBlue)
        Get the passed values as CSS RGB color value
        Parameters:
        nRed - Red - is scaled to 0-255
        nGreen - Green - is scaled to 0-255
        nBlue - Blue - is scaled to 0-255
        Returns:
        The CSS string to use
      • getRGBAColorValue

        @Nonnull
        @Nonempty
        public static String getRGBAColorValue​(int nRed,
                                               int nGreen,
                                               int nBlue,
                                               float fOpacity)
        Get the passed values as CSS RGBA color value
        Parameters:
        nRed - Red - is scaled to 0-255
        nGreen - Green - is scaled to 0-255
        nBlue - Blue - is scaled to 0-255
        fOpacity - Opacity to use - is scaled to 0-1.
        Returns:
        The CSS string to use
      • getHSLHueValue

        @Nonnegative
        public static int getHSLHueValue​(int nHSLPart)
        Get the passed value as a valid HSL Hue value in the range of 0-359
        Parameters:
        nHSLPart - Source Hue
        Returns:
        Hue value in the range of 0-359
      • getHSLHueValue

        @Nonnegative
        public static float getHSLHueValue​(float fHSLPart)
        Get the passed value as a valid HSL Hue value in the range of 0-359
        Parameters:
        fHSLPart - Source Hue
        Returns:
        Hue value in the range of 0-359
      • getHSLPercentageValue

        @Nonnegative
        public static int getHSLPercentageValue​(int nHSLPart)
        Get the passed value as a valid HSL Saturation or Lightness value in the range of 0-100 (percentage).
        Parameters:
        nHSLPart - Source value
        Returns:
        Target value in the range of 0- 100
      • getHSLPercentageValue

        @Nonnegative
        public static float getHSLPercentageValue​(float nHSLPart)
        Get the passed value as a valid HSL Saturation or Lightness value in the range of PERCENTAGE_MIN-PERCENTAGE_MAX (percentage).
        Parameters:
        nHSLPart - Source value
        Returns:
        Target value in the range of 0- 100
      • getHSLColorValue

        @Nonnull
        @Nonempty
        public static String getHSLColorValue​(int nHue,
                                              int nSaturation,
                                              int nLightness)
        Get the passed values as CSS HSL color value
        Parameters:
        nHue - Hue - is scaled to 0-359
        nSaturation - Saturation - is scaled to 0-100
        nLightness - Lightness - is scaled to 0-100
        Returns:
        The CSS string to use
      • getHSLColorValue

        @Nonnull
        @Nonempty
        public static String getHSLColorValue​(float fHue,
                                              float fSaturation,
                                              float fLightness)
        Get the passed values as CSS HSL color value
        Parameters:
        fHue - Hue - is scaled to 0-359
        fSaturation - Saturation - is scaled to 0-100
        fLightness - Lightness - is scaled to 0-100
        Returns:
        The CSS string to use
      • getHSLAColorValue

        @Nonnull
        @Nonempty
        public static String getHSLAColorValue​(int nHue,
                                               int nSaturation,
                                               int nLightness,
                                               float fOpacity)
        Get the passed values as CSS HSLA color value
        Parameters:
        nHue - Hue - is scaled to 0-359
        nSaturation - Saturation - is scaled to 0-100
        nLightness - Lightness - is scaled to 0-100
        fOpacity - Opacity - is scaled to 0-1
        Returns:
        The CSS string to use
      • getHSLAColorValue

        @Nonnull
        @Nonempty
        public static String getHSLAColorValue​(float fHue,
                                               float fSaturation,
                                               float fLightness,
                                               float fOpacity)
        Get the passed values as CSS HSLA color value
        Parameters:
        fHue - Hue - is scaled to 0-359
        fSaturation - Saturation - is scaled to 0-100
        fLightness - Lightness - is scaled to 0-100
        fOpacity - Opacity - is scaled to 0-1
        Returns:
        The CSS string to use
      • getHexColorValue

        @Nonnull
        @Nonempty
        public static String getHexColorValue​(int nRed,
                                              int nGreen,
                                              int nBlue)
      • getRGBAsHSLValue

        @Nonnull
        @Nonempty
        public static float[] getRGBAsHSLValue​(int nRed,
                                               int nGreen,
                                               int nBlue)
        Get the passed RGB values as HSL values compliant for CSS in the CSS range (0-359, 0-100, 0-100)
        Parameters:
        nRed - red value
        nGreen - green value
        nBlue - blue value
        Returns:
        An array of 3 floats, containing hue, saturation and lightness (in this order). The first value is in the range 0-359, and the remaining two values are in the range 0-100 (percentage).
      • getHSLAsRGBValue

        @Nonnull
        @Nonempty
        public static int[] getHSLAsRGBValue​(float fHue,
                                             float fSaturation,
                                             float fLightness)
        Get the passed RGB values as HSL values compliant for CSS in the CSS range (0-359, 0-100, 0-100)
        Parameters:
        fHue - the hue component of the color - in the range 0-359
        fSaturation - the saturation of the color - in the range 0-100
        fLightness - the lightness of the color - in the range 0-100
        Returns:
        An array of 3 ints, containing red, green and blue (in this order). All values are in the range 0-255.