- All Implemented Interfaces:
- Paint,- Transparency,- Serializable
- Direct Known Subclasses:
- ColorUIResource,- SystemColor
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 .
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final ColorThe color black.static final ColorThe color black.static final ColorThe color blue.static final ColorThe color blue.static final ColorThe color cyan.static final ColorThe color cyan.static final ColorThe color dark gray.static final ColorThe color dark gray.static final ColorThe color gray.static final ColorThe color gray.static final ColorThe color green.static final ColorThe color green.static final ColorThe color light gray.static final ColorThe color light gray.static final ColorThe color magenta.static final ColorThe color magenta.static final ColorThe color orange.static final ColorThe color orange.static final ColorThe color pink.static final ColorThe color pink.static final ColorThe color red.static final ColorThe color red.static final ColorThe color white.static final ColorThe color white.static final ColorThe color yellow.static final ColorThe color yellow.Fields declared in interface java.awt.TransparencyBITMASK, OPAQUE, TRANSLUCENT
- 
Constructor SummaryConstructorsConstructorDescriptionColor(float r, float g, float b) Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).Color(float r, float g, float b, float a) Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).Color(int rgb) 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.Color(int rgba, boolean hasalpha) 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.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).Color(ColorSpace cspace, float[] components, float alpha) Creates a color in the specifiedColorSpacewith the color components specified in thefloatarray and the specified alpha.
- 
Method SummaryModifier and TypeMethodDescriptionbrighter()Creates a newColorthat is a brighter version of thisColor.createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints) Creates and returns aPaintContextused to generate a solid color field pattern.darker()Creates a newColorthat is a darker version of thisColor.static ColorConverts aStringto an integer and returns the specified opaqueColor.booleanDetermines whether another object is equal to thisColor.intgetAlpha()Returns the alpha component in the range 0-255.intgetBlue()Returns the blue component in the range 0-255 in the default sRGB space.static ColorFinds a color in the system properties.static ColorFinds a color in the system properties.static ColorFinds a color in the system properties.float[]getColorComponents(float[] compArray) Returns afloatarray containing only the color components of theColor, in theColorSpaceof theColor.float[]getColorComponents(ColorSpace cspace, float[] compArray) Returns afloatarray containing only the color components of theColorin theColorSpacespecified by thecspaceparameter.Returns theColorSpaceof thisColor.float[]getComponents(float[] compArray) Returns afloatarray containing the color and alpha components of theColor, in theColorSpaceof theColor.float[]getComponents(ColorSpace cspace, float[] compArray) Returns afloatarray containing the color and alpha components of theColor, in theColorSpacespecified by thecspaceparameter.intgetGreen()Returns the green component in the range 0-255 in the default sRGB space.static ColorgetHSBColor(float h, float s, float b) Creates aColorobject based on the specified values for the HSB color model.intgetRed()Returns the red component in the range 0-255 in the default sRGB space.intgetRGB()Returns the RGB value representing the color in the default sRGBColorModel.float[]getRGBColorComponents(float[] compArray) Returns afloatarray containing only the color components of theColor, in the default sRGB color space.float[]getRGBComponents(float[] compArray) Returns afloatarray containing the color and alpha components of theColor, as represented in the default sRGB color space.intReturns the transparency mode for thisColor.inthashCode()Computes the hash code for thisColor.static intHSBtoRGB(float hue, float saturation, float brightness) Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.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.toString()Returns a string representation of thisColor.
- 
Field Details- 
whiteThe color white. In the default sRGB space.
- 
WHITEThe color white. In the default sRGB space.- Since:
- 1.4
 
- 
lightGrayThe color light gray. In the default sRGB space.
- 
LIGHT_GRAYThe color light gray. In the default sRGB space.- Since:
- 1.4
 
- 
grayThe color gray. In the default sRGB space.
- 
GRAYThe color gray. In the default sRGB space.- Since:
- 1.4
 
- 
darkGrayThe color dark gray. In the default sRGB space.
- 
DARK_GRAYThe color dark gray. In the default sRGB space.- Since:
- 1.4
 
- 
blackThe color black. In the default sRGB space.
- 
BLACKThe color black. In the default sRGB space.- Since:
- 1.4
 
- 
redThe color red. In the default sRGB space.
- 
REDThe color red. In the default sRGB space.- Since:
- 1.4
 
- 
pinkThe color pink. In the default sRGB space.
- 
PINKThe color pink. In the default sRGB space.- Since:
- 1.4
 
- 
orangeThe color orange. In the default sRGB space.
- 
ORANGEThe color orange. In the default sRGB space.- Since:
- 1.4
 
- 
yellowThe color yellow. In the default sRGB space.
- 
YELLOWThe color yellow. In the default sRGB space.- Since:
- 1.4
 
- 
greenThe color green. In the default sRGB space.
- 
GREENThe color green. In the default sRGB space.- Since:
- 1.4
 
- 
magentaThe color magenta. In the default sRGB space.
- 
MAGENTAThe color magenta. In the default sRGB space.- Since:
- 1.4
 
- 
cyanThe color cyan. In the default sRGB space.
- 
CYANThe color cyan. In the default sRGB space.- Since:
- 1.4
 
- 
blueThe color blue. In the default sRGB space.
- 
BLUEThe color blue. In the default sRGB space.- Since:
- 1.4
 
 
- 
- 
Constructor Details- 
Colorpublic 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
- Throws:
- IllegalArgumentException- if- r,- gor- bare outside of the range 0 to 255, inclusive
- See Also:
 
- 
ColorCreates 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
- Throws:
- IllegalArgumentException- if- r,- g,- bor- aare outside of the range 0 to 255, inclusive
- See Also:
 
- 
Colorpublic Color(int rgb) 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:
- rgb- the combined RGB components
- See Also:
 
- 
Colorpublic Color(int rgba, boolean hasalpha) 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 thehasalphaargument isfalse, alpha is defaulted to 255.- Parameters:
- rgba- the combined RGBA components
- hasalpha-- trueif the alpha bits are valid;- falseotherwise
- See Also:
 
- 
Colorpublic Color(float r, float g, float b) 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:
- r- the red component
- g- the green component
- b- the blue component
- Throws:
- IllegalArgumentException- if- r,- gor- bare outside of the range 0.0 to 1.0, inclusive
- See Also:
 
- 
Colorpublic Color(float r, float g, float b, float a) 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:
- r- the red component
- g- the green component
- b- the blue component
- a- the alpha component
- Throws:
- IllegalArgumentException- if- r,- g- bor- aare outside of the range 0.0 to 1.0, inclusive
- See Also:
 
- 
ColorCreates a color in the specifiedColorSpacewith the color components specified in thefloatarray and the specified alpha. The number of components is determined by the type of theColorSpace. For example, RGB requires 3 components, but CMYK requires 4 components.- Parameters:
- cspace- the- ColorSpaceto be used to interpret the components
- components- an arbitrary number of color components that is compatible with the- ColorSpace
- alpha- alpha value
- Throws:
- IllegalArgumentException- if any of the values in the- componentsarray or- alphais outside of the range 0.0 to 1.0
- See Also:
 
 
- 
- 
Method Details- 
getRedpublic int getRed()Returns the red component in the range 0-255 in the default sRGB space.- Returns:
- the red component.
- See Also:
 
- 
getGreenpublic int getGreen()Returns the green component in the range 0-255 in the default sRGB space.- Returns:
- the green component.
- See Also:
 
- 
getBluepublic int getBlue()Returns the blue component in the range 0-255 in the default sRGB space.- Returns:
- the blue component.
- See Also:
 
- 
getAlphapublic int getAlpha()Returns the alpha component in the range 0-255.- Returns:
- the alpha component.
- See Also:
 
- 
getRGBpublic int getRGB()Returns the RGB value representing the color in the default sRGBColorModel. (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.
- Since:
- 1.0
- See Also:
 
- 
brighterCreates a newColorthat is a brighter version of thisColor.This method applies an arbitrary scale factor to each of the three RGB components of this Colorto create a brighter version of thisColor. Thealphavalue is preserved. Althoughbrighteranddarkerare inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.- Returns:
- a new Colorobject that is a brighter version of thisColorwith the samealphavalue.
- Since:
- 1.0
- See Also:
 
- 
darkerCreates a newColorthat is a darker version of thisColor.This method applies an arbitrary scale factor to each of the three RGB components of this Colorto create a darker version of thisColor. Thealphavalue is preserved. Althoughbrighteranddarkerare inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.- Returns:
- a new Colorobject that is a darker version of thisColorwith the samealphavalue.
- Since:
- 1.0
- See Also:
 
- 
hashCodepublic int hashCode()Computes the hash code for thisColor.
- 
equalsDetermines whether another object is equal to thisColor.The result is trueif and only if the argument is notnulland is aColorobject that has the same red, green, blue, and alpha values as this object.
- 
toStringReturns a string representation of thisColor. 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 benull.
- 
decodeConverts aStringto an integer and returns the specified opaqueColor. This method handles string formats that are used to represent octal and hexadecimal numbers.- Parameters:
- nm- a- Stringthat represents an opaque color as a 24-bit integer
- Returns:
- the new Colorobject.
- Throws:
- NumberFormatException- if the specified string cannot be interpreted as a decimal, octal, or hexadecimal integer.
- Since:
- 1.1
- See Also:
 
- 
getColorFinds 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 Colorobject.If the specified property is not found or could not be parsed as an integer then nullis returned.- Parameters:
- nm- the name of the color property
- Returns:
- the Colorconverted from the system property.
- Since:
- 1.0
- See Also:
 
- 
getColorFinds 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 Colorobject.If the specified property is not found or cannot be parsed as an integer then the Colorspecified by the second argument is returned instead.- Parameters:
- nm- the name of the color property
- v- the default- Color
- Returns:
- the Colorconverted from the system property, or the specifiedColor.
- Since:
- 1.0
- See Also:
 
- 
getColorFinds 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 Colorobject.If the specified property is not found or could not be parsed as an integer then the integer value vis used instead, and is converted to aColorobject.- Parameters:
- nm- the name of the color property
- v- the default color value, as an integer
- Returns:
- the Colorconverted from the system property or theColorconverted from the specified integer.
- Since:
- 1.0
- See Also:
 
- 
HSBtoRGBpublic static int HSBtoRGB(float hue, float saturation, float brightness) Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.The saturationandbrightnesscomponents should be floating-point values between zero and one (numbers in the range 0.0-1.0). Thehuecomponent 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 HSBtoRGBencodes the value of a color in bits 0-23 of an integer value that is the same format used by the methodgetRGB. This integer can be supplied as an argument to theColorconstructor that takes a single integer argument.- Parameters:
- hue- the hue component of the color
- saturation- the saturation of the color
- brightness- the brightness of the color
- Returns:
- the RGB value of the color with the indicated hue, saturation, and brightness.
- Since:
- 1.0
- See Also:
 
- 
RGBtoHSBpublic 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 hsbvalsargument isnull, then a new array is allocated to return the result. Otherwise, the method returns the arrayhsbvals, with the values put into that array.- 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.
- Since:
- 1.0
- See Also:
 
- 
getHSBColorCreates aColorobject based on the specified values for the HSB color model.The sandbcomponents should be floating-point values between zero and one (numbers in the range 0.0-1.0). Thehcomponent 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:
- h- the hue component
- s- the saturation of the color
- b- the brightness of the color
- Returns:
- a Colorobject with the specified hue, saturation, and brightness.
- Since:
- 1.0
 
- 
getRGBComponentspublic float[] getRGBComponents(float[] compArray) Returns afloatarray containing the color and alpha components of theColor, as represented in the default sRGB color space. IfcompArrayisnull, an array of length 4 is created for the return value. Otherwise,compArraymust have length 4 or greater, and it is filled in with the components and returned.- Parameters:
- compArray- an array that this method fills with color and alpha components and returns
- Returns:
- the RGBA components in a floatarray.
 
- 
getRGBColorComponentspublic float[] getRGBColorComponents(float[] compArray) Returns afloatarray containing only the color components of theColor, in the default sRGB color space. IfcompArrayisnull, an array of length 3 is created for the return value. Otherwise,compArraymust have length 3 or greater, and it is filled in with the components and returned.- Parameters:
- compArray- an array that this method fills with color components and returns
- Returns:
- the RGB components in a floatarray.
 
- 
getComponentspublic float[] getComponents(float[] compArray) Returns afloatarray containing the color and alpha components of theColor, in theColorSpaceof theColor. IfcompArrayisnull, an array with length equal to the number of components in the associatedColorSpaceplus one is created for the return value. Otherwise,compArraymust have at least this length and it is filled in with the components and returned.- Parameters:
- compArray- an array that this method fills with the color and alpha components of this- Colorin its- ColorSpaceand returns
- Returns:
- the color and alpha components in a floatarray.
 
- 
getColorComponentspublic float[] getColorComponents(float[] compArray) Returns afloatarray containing only the color components of theColor, in theColorSpaceof theColor. IfcompArrayisnull, an array with length equal to the number of components in the associatedColorSpaceis created for the return value. Otherwise,compArraymust have at least this length and it is filled in with the components and returned.- Parameters:
- compArray- an array that this method fills with the color components of this- Colorin its- ColorSpaceand returns
- Returns:
- the color components in a floatarray.
 
- 
getComponentsReturns afloatarray containing the color and alpha components of theColor, in theColorSpacespecified by thecspaceparameter. IfcompArrayisnull, an array with length equal to the number of components incspaceplus one is created for the return value. Otherwise,compArraymust have at least this length, and it is filled in with the components and returned.- Parameters:
- cspace- a specified- ColorSpace
- compArray- an array that this method fills with the color and alpha components of this- Colorin the specified- ColorSpaceand returns
- Returns:
- the color and alpha components in a floatarray.
 
- 
getColorComponentsReturns afloatarray containing only the color components of theColorin theColorSpacespecified by thecspaceparameter. IfcompArrayisnull, an array with length equal to the number of components incspaceis created for the return value. Otherwise,compArraymust have at least this length, and it is filled in with the components and returned.- Parameters:
- cspace- a specified- ColorSpace
- compArray- an array that this method fills with the color components of this- Colorin the specified- ColorSpace
- Returns:
- the color components in a floatarray.
 
- 
getColorSpaceReturns theColorSpaceof thisColor.- Returns:
- this Colorobject'sColorSpace.
 
- 
createContextpublic PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints) Creates and returns aPaintContextused to generate a solid color field pattern. See thespecificationof the method in thePaintinterface for information on null parameter handling.- Specified by:
- createContextin interface- Paint
- Parameters:
- cm- the preferred- ColorModelwhich represents the most convenient format for the caller to receive the pixel data, or- nullif there is no preference.
- r- the device space bounding box of the graphics primitive being rendered.
- r2d- the user space bounding box of the graphics primitive being rendered.
- xform- the- AffineTransformfrom user space into device space.
- hints- the set of hints that the context object can use to choose between rendering alternatives.
- Returns:
- the PaintContextfor generating color patterns.
- See Also:
 
- 
getTransparencypublic int getTransparency()Returns the transparency mode for thisColor. This is required to implement thePaintinterface.- Specified by:
- getTransparencyin interface- Transparency
- Returns:
- this Colorobject's transparency mode.
- See Also:
 
 
-