Class 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 Detail

      • CssColor

        public CssColor​(double red,
                        double green,
                        double blue,
                        double alpha)
        A new CSS color with the given red, green, blue, and alpha channel.
        Parameters:
        red - Red channel, 0 to 1.
        green - Green channel, 0 to 1.
        blue - Blue channel, 0 to 1.
        alpha - Alpha channel, 0 to 1.
    • 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()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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 - If true, 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 #ff00ff hex string if one is close to this color. Otherwise, returns a rgba(0,2.5,4} RGB(A) CSS color.
      • toRgbaOrHexString

        public String toRgbaOrHexString()
        Returns:
        Converts this color to a #ff00ff hex string if one is close to this color. Otherwise, returns a rgba(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.
      • 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.