Class Color


  • @API(type=EXTENDABLE,
         src=PRIVATE)
    public class Color
    extends java.lang.Object
    The class used to represent a Color. WARNING: This class is immutable. Its values are sometimes cached in class StyleSheet.
    • Constructor Summary

      Constructors 
      Constructor Description
      Color​(int rgba)
      Creates an sRGB color from the specified RGBa color value.
      Color​(int[] rgba)
      Creates a color from a given RGBa array.
      Color​(int r, int g, int b)
      Creates an opaque sRGB color with the specified red, green, and blue values in the range [0 - 255].
      Color​(int r, int g, int b, int a)
      Creates an sRGB color with the specified red, green, blue, and alpha values in the range [0 - 255].
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Color alphaComposite​(Color color, Color background)
      Used to simulate an alpha composition between a color and a background color.
      static Color brighter​(Color color)
      Increase brightness of the color.
      static int changeBrightnessAndSaturation​(int rgba, float brightness, float saturation)
      Change brightness and saturation of the color.
      static Color darker​(Color color)
      Decrease the brightness of the color.
      static Color darker​(Color color, float percent)
      Decrease the brightness of the color.
      static Color decodeColor​(java.lang.String colorString)
      Method used to decode the given string to a color.
      static Color desaturate​(Color fillColor, double percent)
      Used for decrease the color saturation with the given percent.
      boolean equals​(java.lang.Object obj)
      Checks if the two colors have the same RGB value.
      int getAlpha()
      Returns the alpha component in the range 0-255.
      int getBlue()
      Returns the blue component in the range 0-255 in the default sRGB space.
      float getBrightness()
      Gets the L component form the HSL spectrum of the color.
      int getGreen()
      Returns the green component in the range 0-255 in the default sRGB space.
      static java.lang.String getHexColor​(Color color)
      Get the hexadecimal value for the given color.
      int getRed()
      Returns the red component in the range 0-255 in the default sRGB space.
      int getRGB()
      Returns the RGB value representing the color in the default sRGB (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
      static int getRGB​(int red, int green, int blue, int alpha)
      Gets the RGBa value for a color.
      int hashCode()  
      static int HSBtoRGB​(float hue, float saturation, float brightness, int alpha)
      Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
      static Color hslToColor​(float H, float S, float L, int alpha)
      HSL color space to RGB convert method.
      static Color hslToRGB​(float H, float S, float L)
      HSL color space to RGB convert method.
      static int normalize​(int value)
      Normalizes a value according to the range of values a sRGB color component can have - [0-255].
      static java.util.Optional<Color> parseColor​(java.lang.String colorString)
      Parses a RGBa color's String representation to create a Color from it.
      static float[] RGBtoHSB​(int r, int g, int b, float[] hsbvals)
      Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model.
      static float[] rgbTohsl​(int r, int g, int b)
      RGB to HSl space convert method.
      java.lang.String toString()
      Returns a string representation of this Color.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • COLOR_WHITE_ALPHA

        public static final Color COLOR_WHITE_ALPHA
        Light gray with alpha color constant. The RGB values are: (192, 192, 192, 128).
      • COLOR_LIGHT_GRAY

        public static final Color COLOR_LIGHT_GRAY
        Light gray color constant. The RGB values are: (192, 192, 192).
      • COLOR_LIGHT_GRAY_ALPHA

        public static final Color COLOR_LIGHT_GRAY_ALPHA
        Gray with alpha color constant. The RGB values are: (192, 192, 192, 50).
      • COLOR_WHITE

        public static final Color COLOR_WHITE
        White color constant. The RGB values are: (255, 255, 255).
      • COLOR_BLACK

        public static final Color COLOR_BLACK
        Black color constant. The RGB values are: (0, 0, 0).
      • COLOR_BLACK_ALPHA

        public static final Color COLOR_BLACK_ALPHA
        Black color with alpha constant. The RGB values are: (0, 0, 0, 40).
      • COLOR_RED

        public static final Color COLOR_RED
        Red color constant. The RGB values are: (255, 0, 0).
      • COLOR_RED_DARKER

        public static final Color COLOR_RED_DARKER
        Red darker color constant. The RGB values are: (178, 0, 0).
      • COLOR_DARK_GRAY

        public static final Color COLOR_DARK_GRAY
        Dark gray color constant. The RGB values are: (64, 64, 64).
      • COLOR_BLUE

        public static final Color COLOR_BLUE
        Blue color constant. The RGB values are: (0, 0, 255).
      • COLOR_GRAY

        public static final Color COLOR_GRAY
        Gray color constant. The RGB values are: (128, 128, 128).
      • COLOR_LIGHT_GREEN

        public static final Color COLOR_LIGHT_GREEN
        Light green color constant. The RGB values are: (230, 255, 230).
      • COLOR_ORANGE

        public static final Color COLOR_ORANGE
        Orange color constant. The RGB values are: (255, 200, 0).
      • COLOR_LIGHTER_GRAY

        public static final Color COLOR_LIGHTER_GRAY
        Lighter gray color constant. The RGB values are: (240, 240, 240).
      • COLOR_LIGHTER_BLUE

        public static final Color COLOR_LIGHTER_BLUE
        Lighter blue color constant. The RGB values are: (247, 250, 252).
      • COLOR_DARK_GREEN

        public static final Color COLOR_DARK_GREEN
        Darker green color constant. The RGB values are: (0, 128, 0).
      • COLOR_LIGHT_YELLOW

        public static final Color COLOR_LIGHT_YELLOW
        Light yellow color constant. The RGB values are: (255, 255, 230).
      • COLOR_YELLOW

        public static final Color COLOR_YELLOW
        A yellow color.
      • COLOR_DARK_YELLOW

        public static final Color COLOR_DARK_YELLOW
        A dark yellow color.
      • COLOR_PASTE_HIGHLIGHT_YELLOW

        public static final Color COLOR_PASTE_HIGHLIGHT_YELLOW
        The paste highlight color.
      • COLOR_AUTOCORRECT_HIGHLIGHT_YELLOW_FOR_DARK_THEME

        public static final Color COLOR_AUTOCORRECT_HIGHLIGHT_YELLOW_FOR_DARK_THEME
        The AutoCorrect highlight color.
      • COLOR_TRANSPARENT

        public static final Color COLOR_TRANSPARENT
        A transparent color.
      • ALPHA_CHANNEL_DEFAULT_VALUE

        public static final int ALPHA_CHANNEL_DEFAULT_VALUE
        Default value for the alpha channel, when not specified.
        Fully opaque color.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Color

        public Color​(int r,
                     int g,
                     int b,
                     int a)
        Creates an sRGB color with the specified red, green, blue, and alpha values in the range [0 - 255].
        Parameters:
        r - the red component.
        g - the green component.
        b - the blue component.
        a - the alpha component.
      • Color

        public Color​(int rgba)
        Creates an sRGB color from the specified RGBa color value.
        Parameters:
        rgba - The value of the RGBa color. Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue.
      • Color

        public Color​(int[] rgba)
        Creates a color from a given RGBa array.
        Parameters:
        rgba - A color's RGBa components. The alpha channel can be missing, will default to 255 (fully opaque color).
      • Color

        public Color​(int r,
                     int g,
                     int b)
        Creates an opaque sRGB color with the specified red, green, and blue values in the range [0 - 255]. The actual color used in rendering depends on finding the best match given the color space available for a given output device. Alpha is defaulted to 255.
        Parameters:
        r - the red component.
        g - the green component.
        b - the blue component.
    • Method Detail

      • normalize

        public static int normalize​(int value)
        Normalizes a value according to the range of values a sRGB color component can have - [0-255].
        Parameters:
        value - The value to be checked and normalized.
        Returns:
        The same value, if value is in the [0-255] range.
        0 if value is < 0. 255 if value is > 255.
      • getRGB

        public static int getRGB​(int red,
                                 int green,
                                 int blue,
                                 int alpha)
        Gets the RGBa value for a color.
        Parameters:
        red - Red value.
        green - Green value.
        blue - Blue value
        alpha - Alpha value.
        Returns:
        The representation of the given values as an int.
      • getRed

        public int getRed()
        Returns the red component in the range 0-255 in the default sRGB space.
        Returns:
        The red component.
      • getGreen

        public int getGreen()
        Returns the green component in the range 0-255 in the default sRGB space.
        Returns:
        The green component.
      • getBlue

        public int getBlue()
        Returns the blue component in the range 0-255 in the default sRGB space.
        Returns:
        The blue component.
      • getAlpha

        public int getAlpha()
        Returns the alpha component in the range 0-255.
        Returns:
        The alpha component.
      • getRGB

        public int getRGB()
        Returns the RGB value representing the color in the default sRGB (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
        Returns:
        the RGB value of the color in the default sRGB ColorModel.
      • toString

        public java.lang.String toString()
        Returns a string representation of this Color. This method is intended to be used only for debugging purposes. The content and format of the returned string might vary between implementations. The returned string might be empty but cannot be null.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of this Color.
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if the two colors have the same RGB value. The alpha is also taken into account.
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • desaturate

        public static Color desaturate​(Color fillColor,
                                       double percent)
        Used for decrease the color saturation with the given percent.
        Parameters:
        fillColor - The color to be darkened.
        percent - The percent to be used for decrease the color saturation.
        Returns:
        The modified color.
      • alphaComposite

        public static Color alphaComposite​(Color color,
                                           Color background)
        Used to simulate an alpha composition between a color and a background color.
        Parameters:
        color - The transparent color.
        background - The background color.
        Returns:
        The resulted color.
        See Also:
        "http://en.wikipedia.org/wiki/Alpha_compositing"
      • decodeColor

        public static Color decodeColor​(java.lang.String colorString)
        Method used to decode the given string to a color.
        Parameters:
        colorString - The string representation of the color.
        Returns:
        The decoded color if possible, black otherwise.
      • parseColor

        public static java.util.Optional<Color> parseColor​(java.lang.String colorString)
                                                    throws ro.sync.basic.util.NumberFormatException
        Parses a RGBa color's String representation to create a Color from it.
        Parameters:
        colorString - A RGBa color's String representation.
        Returns:
        The corresponding Color.
        Can be null if colorString is null, the empty string, or it cannot be properly tokenized to identify the 3 or 4 components of a RGBa color (red, green and blue channels; alpha channel is optional).
        Throws:
        ro.sync.basic.util.NumberFormatException - If invalid values for color channels R, G, B or alpha.
        See Also:
        normalize(int)
      • rgbTohsl

        public static float[] rgbTohsl​(int r,
                                       int g,
                                       int b)
        RGB to HSl space convert method. Copied from: http://www.easyrgb.com/math.php?MATH=M19#text19
        Parameters:
        r - Red (0 -> 255)
        g - Green (0 -> 255)
        b - Blue (0 -> 255)
        Returns:
        The HSL array of values between 0.0f and 1.0f
      • hslToColor

        public static Color hslToColor​(float H,
                                       float S,
                                       float L,
                                       int alpha)
        HSL color space to RGB convert method. Copied from: http://www.easyrgb.com/math.php?MATH=M19#text19
        Parameters:
        H - (between 0.0 and 1.0)
        S - (between 0.0 and 1.0)
        L - (between 0.0 and 1.0)
        alpha - The alpha tone.
        Returns:
        The RGB color.
      • hslToRGB

        public static Color hslToRGB​(float H,
                                     float S,
                                     float L)
        HSL color space to RGB convert method. Copied from: http://www.easyrgb.com/math.php?MATH=M19#text19
        Parameters:
        H - (between 0.0 and 1.0)
        S - (between 0.0 and 1.0)
        L - (between 0.0 and 1.0)
        Returns:
        The RGB color.
      • darker

        public static Color darker​(Color color)
        Decrease the brightness of the color.
        Parameters:
        color - The color to be modified.
        Returns:
        The color with decreased brightness.
      • darker

        public static Color darker​(Color color,
                                   float percent)
        Decrease the brightness of the color.
        Parameters:
        color - The color to be modified.
        percent - The percent to be used for darkening a color.
        Returns:
        The color with decreased brightness.
      • brighter

        public static Color brighter​(Color color)
        Increase brightness of the color.
        Parameters:
        color - The color to be modified.
        Returns:
        The color with increased brightness.
      • getBrightness

        public float getBrightness()
        Gets the L component form the HSL spectrum of the color.
        Returns:
        The brightness of the color. Between 0.0 and 1.0
      • getHexColor

        public static java.lang.String getHexColor​(Color color)
        Get the hexadecimal value for the given color.
        Parameters:
        color - The color to compute HEX value,
        Returns:
        The hexadecimal value for the given color.
      • changeBrightnessAndSaturation

        public static int changeBrightnessAndSaturation​(int rgba,
                                                        float brightness,
                                                        float saturation)
        Change brightness and saturation of the color.
        Parameters:
        rgba - The color to be modified.
        brightness - The factor for changing the brightness.
        saturation - The factor for changing the saturation.
        Returns:
        The modified color.
      • RGBtoHSB

        public static float[] RGBtoHSB​(int r,
                                       int g,
                                       int b,
                                       float[] hsbvals)
        Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model.

        If the hsbvals argument is null, then a new array is allocated to return the result. Otherwise, the method returns the array hsbvals, with the values put into that array. Copied from Color.RGBtoHSB(int, int, int, float[])

        Parameters:
        r - the red component of the color
        g - the green component of the color
        b - the blue component of the color
        hsbvals - the array used to return the three HSB values, or null
        Returns:
        an array of three elements containing the hue, saturation, and brightness (in that order), of the color with the indicated red, green, and blue components.
        See Also:
        Color.getRGB(), Color(int), ColorModel.getRGBdefault()
      • HSBtoRGB

        public static int HSBtoRGB​(float hue,
                                   float saturation,
                                   float brightness,
                                   int alpha)
        Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.

        The saturation and brightness components should be floating-point values between zero and one (numbers in the range 0.0-1.0). The hue component can be any floating-point number. The floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.

        The integer that is returned by HSBtoRGB encodes the value of a color in bits 0-23 of an integer value that is the same format used by the method getRGB. This integer can be supplied as an argument to the Color constructor that takes a single integer argument. Copied from Color.HSBtoRGB(float, float, float)

        Parameters:
        hue - the hue component of the color
        saturation - the saturation of the color
        brightness - the brightness of the color
        alpha - the alpha level.
        Returns:
        the RGB value of the color with the indicated hue, saturation, and brightness.
        See Also:
        Color.getRGB(), Color(int), ColorModel.getRGBdefault()