Package de.xima.fc.color
Class CssColor
- java.lang.Object
-
- de.xima.fc.color.CssColor
-
- All Implemented Interfaces:
Serializable
@Immutable public class CssColor extends Object implements Serializable
A CSS color value, with optional transparency. Has got some utility methods to create a color from a CSS color string, and to convert the color to different CSS representation. An instance of this class is immutable.CssColor.valueOf("#ff0000"); CssColor.valueOf("orange");- Author:
- XIMA MEDIA GmbH
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CssColor(double red, double green, double blue, double alpha)A new CSS color with the given red, green, blue, and alpha channel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)doublegetAlpha()doublegetBlue()doublegetGreen()doublegetRed()booleanhasAlpha()inthashCode()booleanisTransparent()doublenorm1(CssColor other)static CssColorofHex(long hexCode)static CssColorofHex(String hexCode, CssColor defaultColor)static CssColorofRgb(double red, double green, double blue)static CssColorofRgb(int red, int green, int blue)static CssColorofRgb(long red, long green, long blue)static CssColorofRgb(String rgba, CssColor defaultColor)static CssColorofRgba(int red, int green, int blue, double alpha)static CssColorofRgba(int red, int green, int blue, int alpha)static CssColorofRgba(long red, long green, long blue, double alpha)static CssColorofRgba(long red, long green, long blue, long alpha)static CssColorofRgba(String rgba, CssColor defaultColor)static CssColorofRgbOrRgba(String hexCode, CssColor defaultColor)StringtoClosestColorName()StringtoCssString()StringtoHexString()StringtoHexString(boolean withHashTag)StringtoNameOrHexOrRgba(double precision)StringtoRgbaOrHexString()StringtoRgbaString()RgbTriplettoRgbTripel()StringtoString()static CssColorvalueOf(String color)static CssColorvalueOf(String color, CssColor defaultColor)static CssColorvalueOf(String color, ENamedCssColor defaultColor)CssColorwithAlpha(double alpha)
-
-
-
Method Detail
-
getAlpha
public double getAlpha()
- Returns:
- The alpha component of the color.
-
getBlue
public double getBlue()
- Returns:
- The blue component of the color.
-
getGreen
public double getGreen()
- Returns:
- The green component of the color.
-
getRed
public double getRed()
- Returns:
- The red component of the color.
-
hasAlpha
public boolean hasAlpha()
-
isTransparent
public boolean isTransparent()
- Returns:
- Whether this color has any transparency.
-
norm1
public double norm1(CssColor other)
- Parameters:
other- Another color.- Returns:
- 1-norm between this and the other color.
-
toClosestColorName
public String toClosestColorName()
- Returns:
- The CSS color name that is closest to this color.
-
toCssString
public String toCssString()
- Returns:
- This color as a CSS string that can be used in color properties.
-
toHexString
public String toHexString()
- Returns:
- A hex string of this color, such as
#ff00ff.
-
toHexString
public String toHexString(boolean withHashTag)
- Parameters:
withHashTag- Iftrue, prepends the#hash tag.- Returns:
- A hex string of this color, such as
#ff00ff.
-
toNameOrHexOrRgba
public String toNameOrHexOrRgba(double precision)
- Parameters:
precision- How close a named or hex color must lie to be considered valid.- Returns:
- Converts this color to a CSS color name, if one is close to this color. Otherwise, converts to a
#ff00ffhex string if one is close to this color. Otherwise, returns argba(0,2.5,4} RGB(A) CSS color.
-
toRgbaOrHexString
public String toRgbaOrHexString()
- Returns:
- Converts this color to a
#ff00ffhex string if one is close to this color. Otherwise, returns argba(0,2.5,4)RGB(A) CSS color.
-
toRgbaString
public String toRgbaString()
- Returns:
- Converts this color to a
rgba(0,2.5,4)RGB(A) CSS color.
-
toRgbTripel
public RgbTriplet toRgbTripel()
-
withAlpha
public CssColor withAlpha(double alpha)
- Parameters:
alpha- New alpha value.- Returns:
- This color, with the new alpha value.
-
ofHex
public static CssColor ofHex(long hexCode)
- Parameters:
hexCode- Hex code to parse.- Returns:
- The parsed CSS color.
-
ofHex
public static CssColor ofHex(String hexCode, CssColor defaultColor)
- Parameters:
hexCode- Hex code to parse.defaultColor- Default color if hex code is invalid.- Returns:
- The parsed CSS color.
-
ofRgb
public static CssColor ofRgb(double red, double green, double blue)
- Parameters:
red- Red component, in the range [0,1].green- Green component, in the range [0,1].blue- Blue component, in the range [0,1].- Returns:
- An immutable CSS color for the given values.
-
ofRgb
public static CssColor ofRgb(int red, int green, int blue)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].- Returns:
- An immutable CSS color for the given values.
-
ofRgb
public static CssColor ofRgb(long red, long green, long blue)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].- Returns:
- An immutable CSS color for the given values.
-
ofRgba
public static CssColor ofRgba(int red, int green, int blue, double alpha)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].alpha- Alpha component, in the range [0,1]. 0 is fully transparent.- Returns:
- An immutable CSS color for the given values.
-
ofRgba
public static CssColor ofRgba(long red, long green, long blue, double alpha)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].alpha- Alpha component, in the range [0,1]. 0 is fully transparent.- Returns:
- An immutable CSS color for the given values.
-
ofRgba
public static CssColor ofRgba(int red, int green, int blue, int alpha)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].alpha- Alpha component, in the range [0,255]. 0 is fully transparent.- Returns:
- An immutable CSS color for the given values.
-
ofRgba
public static CssColor ofRgba(long red, long green, long blue, long alpha)
- Parameters:
red- Red component, in the range [0,255].green- Green component, in the range [0,255].blue- Blue component, in the range [0,255].alpha- Alpha component, in the range [0,255]. 0 is fully transparent.- Returns:
- An immutable CSS color for the given values.
-
valueOf
public static CssColor valueOf(String color) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
valueOf
public static CssColor valueOf(String color, ENamedCssColor defaultColor)
-
-