public class Color
extends Object
implements Paint, Serializable

The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor. The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 - 1.0 or 0 - 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent. When constructing a Color with an explicit alpha or getting the color/alpha components of a Color, the color components are never premultiplied by the alpha component.

The default color space for the Java 2D(tm) API is sRGB, a proposed standard RGB color space. For further information on sRGB, see http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html .

public static final black

The color black. In the default sRGB space.

public static final BLACK

The color black. In the default sRGB space.

public static final blue

The color blue. In the default sRGB space.

public static final BLUE

The color blue. In the default sRGB space.

public static final cyan

The color cyan. In the default sRGB space.

public static final CYAN

The color cyan. In the default sRGB space.

public static final DARK_GRAY

The color dark gray. In the default sRGB space.

public static final darkGray

The color dark gray. In the default sRGB space.

public static final gray

The color gray. In the default sRGB space.

public static final GRAY

The color gray. In the default sRGB space.

public static final green

The color green. In the default sRGB space.

public static final GREEN

The color green. In the default sRGB space.

public static final LIGHT_GRAY

The color light gray. In the default sRGB space.

public static final lightGray

The color light gray. In the default sRGB space.

public static final magenta

The color magenta. In the default sRGB space.

public static final MAGENTA

The color magenta. In the default sRGB space.

public static final orange

The color orange. In the default sRGB space.

public static final ORANGE

The color orange. In the default sRGB space.

public static final pink

The color pink. In the default sRGB space.

public static final PINK

The color pink. In the default sRGB space.

public static final red

The color red. In the default sRGB space.

public static final RED

The color red. In the default sRGB space.

public static final white

The color white. In the default sRGB space.

public static final WHITE

The color white. In the default sRGB space.

public static final yellow

The color yellow. In the default sRGB space.

public static final YELLOW

The color yellow. In the default sRGB space.

public Color(ColorSpace, float[], float)

Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha. The number of components is determined by the type of the ColorSpace. For example, RGB requires 3 components, but CMYK requires 4 components.

Parameters

ColorSpace cspace: the ColorSpace to be used to interpret the components

float[] components: an arbitrary number of color components that is compatible with the ColorSpace

float alpha: alpha value

Throws

IllegalArgumentException: if any of the values in the components array or alpha is outside of the range 0.0 to 1.0

public Color(float, float, float)

Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0). Alpha is defaulted to 1.0. The actual color used in rendering depends on finding the best match given the color space available for a particular output device.

Parameters

float r: the red component

float g: the green component

float b: the blue component

Throws

IllegalArgumentException: if r, g or b are outside of the range 0.0 to 1.0, inclusive

public Color(float, float, float, float)

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0). The actual color used in rendering depends on finding the best match given the color space available for a particular output device.

Parameters

float r: the red component

float g: the green component

float b: the blue component

float a: the alpha component

Throws

IllegalArgumentException: if r, g b or a are outside of the range 0.0 to 1.0, inclusive

public Color(int)

Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. The actual color used in rendering depends on finding the best match given the color space available for a particular output device. Alpha is defaulted to 255.

Parameters

int rgb: the combined RGB components

public Color(int, boolean)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255.

Parameters

int rgba: the combined RGBA components

boolean hasalpha: true if the alpha bits are valid; false otherwise

public Color(int, int, int)

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

int r: the red component

int g: the green component

int b: the blue component

Throws

IllegalArgumentException: if r, g or b are outside of the range 0 to 255, inclusive

public Color(int, int, int, int)

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

Parameters

int r: the red component

int g: the green component

int b: the blue component

int a: the alpha component

Throws

IllegalArgumentException: if r, g, b or a are outside of the range 0 to 255, inclusive

public Color brighter()

Creates a new Color that is a brighter version of this Color.

This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a brighter version of this Color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.

public synchronized PaintContext createContext(ColorModel, Rectangle, Rectangle2D, AffineTransform, RenderingHints)

Creates and returns a PaintContext used to generate a solid color pattern. This enables a Color object to be used as an argument to any method requiring an object implementing the Paint interface. The same PaintContext is returned, regardless of whether or not r, r2d, xform, or hints are null.

Parameters

ColorModel cm: the specified ColorModel

Rectangle r: the specified Rectangle

Rectangle2D r2d: the specified Rectangle2D

AffineTransform xform: the specified AffineTransform

RenderingHints hints: the specified RenderingHints

public Color darker()

Creates a new Color that is a darker version of this Color.

This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a darker version of this Color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.

public static Color decode(String)

Converts a String to an integer and returns the specified opaque Color. This method handles string formats that are used to represent octal and hexadecimal numbers.

Parameters

String nm: a String that represents an opaque color as a 24-bit integer

Throws

NumberFormatException: if the specified string cannot be interpreted as a decimal, octal, or hexadecimal integer.

public boolean equals(Object)

Determines whether another object is equal to this Color.

The result is true if and only if the argument is not null and is a Color object that has the same red, green, blue, and alpha values as this object.

Parameters

Object obj: the object to test for equality with this Color

public int getAlpha()

Returns the alpha component in the range 0-255.

public int getBlue()

Returns the blue component in the range 0-255 in the default sRGB space.

public static Color getColor(String)

Finds a color in the system properties.

The argument is treated as the name of a system property to be obtained. The string value of this property is then interpreted as an integer which is then converted to a Color object.

If the specified property is not found or could not be parsed as an integer then null is returned.

Parameters

String nm: the name of the color property

public static Color getColor(String, Color)

Finds a color in the system properties.

The first argument is treated as the name of a system property to be obtained. The string value of this property is then interpreted as an integer which is then converted to a Color object.

If the specified property is not found or cannot be parsed as an integer then the Color specified by the second argument is returned instead.

Parameters

String nm: the name of the color property

Color v: the default Color

public static Color getColor(String, int)

Finds a color in the system properties.

The first argument is treated as the name of a system property to be obtained. The string value of this property is then interpreted as an integer which is then converted to a Color object.

If the specified property is not found or could not be parsed as an integer then the integer value v is used instead, and is converted to a Color object.

Parameters

String nm: the name of the color property

int v: the default color value, as an integer

public float getColorComponents(ColorSpace, float[])

Returns a float array containing only the color components of the Color in the ColorSpace specified by the cspace parameter. If compArray is null, an array with length equal to the number of components in cspace is created for the return value. Otherwise, compArray must have at least this length, and it is filled in with the components and returned.

Parameters

ColorSpace cspace: a specified ColorSpace

float[] compArray: an array that this method fills with the color components of this Color in the specified ColorSpace

public float getColorComponents(float[])

Returns a float array containing only the color components of the Color, in the ColorSpace of the Color. If compArray is null, an array with length equal to the number of components in the associated ColorSpace is created for the return value. Otherwise, compArray must have at least this length and it is filled in with the components and returned.

Parameters

float[] compArray: an array that this method fills with the color components of this Color in its ColorSpace and returns

public ColorSpace getColorSpace()

Returns the ColorSpace of this Color.

public float getComponents(ColorSpace, float[])

Returns a float array containing the color and alpha components of the Color, in the ColorSpace specified by the cspace parameter. If compArray is null, an array with length equal to the number of components in cspace plus one is created for the return value. Otherwise, compArray must have at least this length, and it is filled in with the components and returned.

Parameters

ColorSpace cspace: a specified ColorSpace

float[] compArray: an array that this method fills with the color and alpha components of this Color in the specified ColorSpace and returns

public float getComponents(float[])

Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color. If compArray is null, an array with length equal to the number of components in the associated ColorSpace plus one is created for the return value. Otherwise, compArray must have at least this length and it is filled in with the components and returned.

Parameters

float[] compArray: an array that this method fills with the color and alpha components of this Color in its ColorSpace and returns

public int getGreen()

Returns the green component in the range 0-255 in the default sRGB space.

public static Color getHSBColor(float, float, float)

Creates a Color object based on the specified values for the HSB color model.

The s and b components should be floating-point values between zero and one (numbers in the range 0.0-1.0). The h 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.

Parameters

float h: the hue component

float s: the saturation of the color

float b: the brightness of the color

public int getRed()

Returns the red component in the range 0-255 in the default sRGB space.

public int getRGB()

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

public float getRGBColorComponents(float[])

Returns a float array containing only the color components of the Color, in the default sRGB color space. If compArray is null, an array of length 3 is created for the return value. Otherwise, compArray must have length 3 or greater, and it is filled in with the components and returned.

Parameters

float[] compArray: an array that this method fills with color components and returns

public float getRGBComponents(float[])

Returns a float array containing the color and alpha components of the Color, as represented in the default sRGB color space. If compArray is null, an array of length 4 is created for the return value. Otherwise, compArray must have length 4 or greater, and it is filled in with the components and returned.

Parameters

float[] compArray: an array that this method fills with color and alpha components and returns

public int getTransparency()

Returns the transparency mode for this Color. This is required to implement the Paint interface.

public int hashCode()

Computes the hash code for this Color.

public static int HSBtoRGB(float, float, float)

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() getRGB. This integer can be supplied as an argument to the Color constructor that takes a single integer argument.

Parameters

float hue: the hue component of the color

float saturation: the saturation of the color

float brightness: the brightness of the color

public static float RGBtoHSB(int, int, int, float[])

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.

Parameters

int r: the red component of the color

int g: the green component of the color

int b: the blue component of the color

float[] hsbvals: the array used to return the three HSB values, or null

public 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.