Package com.helger.css.utils
Class CSSColorHelper
- java.lang.Object
-
- com.helger.css.utils.CSSColorHelper
-
@Immutable public final class CSSColorHelper extends Object
Provides color handling sanity methods.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static intHSL_MAXHSL maximum value (for Hue only)static intHSL_MINHSL minimum value (for Hue only)static intHSL_RANGEHSL range (max-min+1) (for Hue only)static floatOPACITY_MAXMaximum opacity valuestatic floatOPACITY_MINMinimum opacity valuestatic intPERCENTAGE_MAXPercentage maximum value (for HSL)static intPERCENTAGE_MINPercentage minimum value (for HSL)static intRGB_MAXRGB maximum valuestatic intRGB_MINRGB minimum valuestatic intRGB_RANGERGB range (max-min+1)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetHexColorValue(int nRed, int nGreen, int nBlue)static StringgetHSLAColorValue(float fHue, float fSaturation, float fLightness, float fOpacity)Get the passed values as CSS HSLA color valuestatic StringgetHSLAColorValue(int nHue, int nSaturation, int nLightness, float fOpacity)Get the passed values as CSS HSLA color valuestatic 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)static StringgetHSLColorValue(float fHue, float fSaturation, float fLightness)Get the passed values as CSS HSL color valuestatic StringgetHSLColorValue(int nHue, int nSaturation, int nLightness)Get the passed values as CSS HSL color valuestatic floatgetHSLHueValue(float fHSLPart)static intgetHSLHueValue(int nHSLPart)static floatgetHSLPercentageValue(float nHSLPart)Get the passed value as a valid HSL Saturation or Lightness value in the range ofPERCENTAGE_MIN-PERCENTAGE_MAX(percentage).static intgetHSLPercentageValue(int nHSLPart)static floatgetOpacityToUse(float fOpacity)Ensure that the passed opacity value is in the rangeOPACITY_MINandOPACITY_MAX.static CSSHSLAgetParsedHSLAColorValue(String sValue)static CSSHSLgetParsedHSLColorValue(String sValue)static CSSRGBAgetParsedRGBAColorValue(String sValue)static CSSRGBgetParsedRGBColorValue(String sValue)Extract the CSS RGB color value from the passed String.static StringgetRGBAColorValue(int nRed, int nGreen, int nBlue, float fOpacity)Get the passed values as CSS RGBA color valuestatic 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)static StringgetRGBColorValue(int nRed, int nGreen, int nBlue)Get the passed values as CSS RGB color valuestatic intgetRGBValue(int nRGBPart)Convert the passed value to a valid RGB value in the range 0-255.static booleanisColorValue(String sValue)Check if the passed string is any color value.static booleanisHexColorValue(String sValue)Check if the passed String is valid CSS hex color value.static booleanisHSLAColorValue(String sValue)Check if the passed String is valid CSS HSLA color value.static booleanisHSLColorValue(String sValue)Check if the passed String is valid CSS HSL color value.static booleanisRGBAColorValue(String sValue)Check if the passed String is valid CSS RGBA color value.static booleanisRGBColorValue(String sValue)Check if the passed String is valid CSS RGB color value.
-
-
-
Field Detail
-
RGB_MIN
public static final int RGB_MIN
RGB minimum value- See Also:
- Constant Field Values
-
RGB_MAX
public static final int RGB_MAX
RGB maximum value- See Also:
- Constant Field Values
-
RGB_RANGE
public static final int RGB_RANGE
RGB range (max-min+1)- See Also:
- Constant Field Values
-
HSL_MIN
public static final int HSL_MIN
HSL minimum value (for Hue only)- See Also:
- Constant Field Values
-
HSL_MAX
public static final int HSL_MAX
HSL maximum value (for Hue only)- See Also:
- Constant Field Values
-
HSL_RANGE
public static final int HSL_RANGE
HSL range (max-min+1) (for Hue only)- See Also:
- Constant Field Values
-
PERCENTAGE_MIN
public static final int PERCENTAGE_MIN
Percentage minimum value (for HSL)- See Also:
- Constant Field Values
-
PERCENTAGE_MAX
public static final int PERCENTAGE_MAX
Percentage maximum value (for HSL)- See Also:
- Constant Field Values
-
OPACITY_MIN
public static final float OPACITY_MIN
Minimum opacity value- See Also:
- Constant Field Values
-
OPACITY_MAX
public static final float OPACITY_MAX
Maximum opacity value- See Also:
- Constant Field Values
-
-
Method Detail
-
isColorValue
public static boolean isColorValue(@Nullable String sValue)
Check if the passed string is any color value.- Parameters:
sValue- The value to check. May benull.- Returns:
trueif the passed value is notnull, not empty and a valid CSS color value.- See Also:
isRGBColorValue(String),isRGBAColorValue(String),isHSLColorValue(String),isHSLAColorValue(String),isHexColorValue(String),ECSSColor.isDefaultColorName(String),ECSSColorName.isDefaultColorName(String),CCSSValue.CURRENTCOLOR,CCSSValue.TRANSPARENT
-
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 benull.- Returns:
trueif it is a CSS RGB color value,falseif 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 benull.- Returns:
nullif 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 benull.- Returns:
trueif it is a CSS RGBA color value,falseif not
-
getParsedRGBAColorValue
@Nullable public static CSSRGBA getParsedRGBAColorValue(@Nullable String sValue)
-
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 benull.- Returns:
trueif it is a CSS HSL color value,falseif not
-
getParsedHSLColorValue
@Nullable public static CSSHSL getParsedHSLColorValue(@Nullable String sValue)
-
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 benull.- Returns:
trueif it is a CSS HSLA color value,falseif not
-
getParsedHSLAColorValue
@Nullable public static CSSHSLA getParsedHSLAColorValue(@Nullable String sValue)
-
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 benull.- Returns:
trueif it is a CSS hex color value,falseif 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 rangeOPACITY_MINandOPACITY_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-255nGreen- Green - is scaled to 0-255nBlue- 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-255nGreen- Green - is scaled to 0-255nBlue- Blue - is scaled to 0-255fOpacity- Opacity to use - is scaled to 0-1.- Returns:
- The CSS string to use
-
getHSLHueValue
@Nonnegative public static int getHSLHueValue(int nHSLPart)
-
getHSLHueValue
@Nonnegative public static float getHSLHueValue(float fHSLPart)
-
getHSLPercentageValue
@Nonnegative public static int getHSLPercentageValue(int nHSLPart)
-
getHSLPercentageValue
@Nonnegative public static float getHSLPercentageValue(float nHSLPart)
Get the passed value as a valid HSL Saturation or Lightness value in the range ofPERCENTAGE_MIN-PERCENTAGE_MAX(percentage).
-
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-359nSaturation- Saturation - is scaled to 0-100nLightness- 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-359fSaturation- Saturation - is scaled to 0-100fLightness- 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-359nSaturation- Saturation - is scaled to 0-100nLightness- Lightness - is scaled to 0-100fOpacity- 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-359fSaturation- Saturation - is scaled to 0-100fLightness- Lightness - is scaled to 0-100fOpacity- 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 valuenGreen- green valuenBlue- 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-359fSaturation- the saturation of the color - in the range 0-100fLightness- 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.
-
-