站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java(TM) 2 Platform, Standard Edition, v1.2.2 API Specification: Class Color - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

JavaTM 2 Platform
Standard Edition

java.awt
Class Color

java.lang.Object
  |
  +--java.awt.Color
Direct Known Subclasses:
ColorUIResource, SystemColor

public class Color
extends Object
implements Paint, Serializable

A class 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. 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 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 .

Eventually this class should supersede java.awt.Color, but for now it is defined to be part of the java.java2d package, so we can generate documentation for a single package for review.

See Also:
ColorSpace, Serialized Form

Field Summary
static Color black
          The color black.
static Color blue
          The color blue.
static Color cyan
          The color cyan.
static Color darkGray
          The color dark gray.
static Color gray
          The color gray.
static Color green
          The color green.
static Color lightGray
          The color light gray.
static Color magenta
          The color magenta.
static Color orange
          The color orange.
static Color pink
          The color pink.
static Color red
          The color red.
static Color white
          The color white.
static Color yellow
          The color yellow.
 
Fields inherited from interface java.awt.Transparency
BITMASK, OPAQUE, TRANSLUCENT
 
Constructor Summary
Color(ColorSpace cspace, float[] components, float alpha)
          Creates a color in the color space of the supplied ColorSpace with the color components specified in the float array and the specified alpha.
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).
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).
 
Method Summary
 Color brighter()
          Creates a brighter version of this color.
 PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
          Create and return a PaintContext used to generate a solid color pattern.
 Color darker()
          Creates a darker version of this color.
static Color decode(String nm)
          Converts a string to an integer and returns the specified opaque color.
 boolean equals(Object obj)
          Determines whether another object is equal to this color.
 int getAlpha()
          Returns the alpha component.
 int getBlue()
          Returns the blue component.
static Color getColor(String nm)
          Finds a color in the system properties.
static Color getColor(String nm, Color v)
          Finds a color in the system properties.
static Color getColor(String nm, int v)
          Finds a color in the system properties.
 float[] getColorComponents(ColorSpace cspace, float[] compArray)
          Returns a float array containing the color components (no alpha) of the Color, in the ColorSpace specified by the cspace parameter.
 float[] getColorComponents(float[] compArray)
          Returns a float array containing the color components (no alpha) of the Color, in the ColorSpace of the color.
 ColorSpace getColorSpace()
          Returns the ColorSpace of the Color.
 float[] getComponents(ColorSpace cspace, float[] compArray)
          Returns a float array containing the color and alpha components of the Color, in the ColorSpace specified by the cspace parameter.
 float[] getComponents(float[] compArray)
          Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color.
 int getGreen()
          Returns the green component.
static Color getHSBColor(float h, float s, float b)
          Creates a Color object based on values supplied for the HSB color model.
 int getRed()
          Returns the red component.
 int getRGB()
          Returns the RGB value representing the color in the default sRGB ColorModel.
 float[] getRGBColorComponents(float[] compArray)
          Returns a float array containing the color components (no alpha) of the Color, in the default sRGB color space.
 float[] getRGBComponents(float[] compArray)
          Returns a float array containing the color and alpha components of the Color, as represented in the default sRGB color space.
 int getTransparency()
          Return the transparency mode for this Color.
 int hashCode()
          Computes the hash code for this color.
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.
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, the three components of the HSB model.
 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

white

public static final Color white
The color white. In the default sRGB space.

lightGray

public static final Color lightGray
The color light gray. In the default sRGB space.

gray

public static final Color gray
The color gray. In the default sRGB space.

darkGray

public static final Color darkGray
The color dark gray. In the default sRGB space.

black

public static final Color black
The color black. In the default sRGB space.

red

public static final Color red
The color red. In the default sRGB space.

pink

public static final Color pink
The color pink. In the default sRGB space.

orange

public static final Color orange
The color orange. In the default sRGB space.

yellow

public static final Color yellow
The color yellow. In the default sRGB space.

green

public static final Color green
The color green. In the default sRGB space.

magenta

public static final Color magenta
The color magenta. In the default sRGB space.

cyan

public static final Color cyan
The color cyan. In the default sRGB space.

blue

public static final Color blue
The color blue. In the default sRGB space.
Constructor Detail

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 will depend 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
See Also:
getRed(), getGreen(), getBlue(), getRGB()

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
See Also:
getRed(), getGreen(), getBlue(), getAlpha(), getRGB()

Color

public 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 will depend on finding the best match given the color space available for a given output device. Alpha is defaulted to 255.
Parameters:
rgb - the combined RGB components
See Also:
ColorModel.getRGBdefault(), getRed(), getGreen(), getBlue(), getRGB()

Color

public 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 the hasalpha argument is False, alpha is defaulted to 255.
Parameters:
rgba - the combined RGBA components
hasalpha - true if the alpha bits are valid, false otherwise
See Also:
ColorModel.getRGBdefault(), getRed(), getGreen(), getBlue(), getAlpha(), getRGB()

Color

public 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 will depend on finding the best match given the color space available for a given output device.
Parameters:
r - the red component
g - the green component
b - the blue component
See Also:
getRed(), getGreen(), getBlue(), getRGB()

Color

public 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 will depend on finding the best match given the color space available for a given output device.
Parameters:
r - the red component
g - the green component
b - the blue component
a - the alpha component
See Also:
getRed(), getGreen(), getBlue(), getAlpha(), getRGB()

Color

public Color(ColorSpace cspace,
             float[] components,
             float alpha)
Creates a color in the color space of the supplied ColorSpace with the color components specified in the float array and the specified alpha. The number of components is determined by the color space type of the ColorSpace (e.g. RGB requires 3 components, CMYK requires 4, etc.).
Parameters:
cspace - The ColorSpace to be used to interpret the components
components - An arbitrary number of color components that is compatible with the ColorSpace
alpha - Alpha value
See Also:
getComponents(float[]), getColorComponents(float[])
Method Detail

getRed

public int getRed()
Returns the red component. In the range 0-255 in the default sRGB space.
See Also:
getRGB()

getGreen

public int getGreen()
Returns the green component. In the range 0-255 in the default sRGB space.
See Also:
getRGB()

getBlue

public int getBlue()
Returns the blue component. In the range 0-255 in the default sRGB space.
See Also:
getRGB()

getAlpha

public int getAlpha()
Returns the alpha component. In the range 0-255.
See Also:
getRGB()

getRGB

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).
Since:
JDK1.0
See Also:
ColorModel.getRGBdefault(), getRed(), getGreen(), getBlue()

brighter

public Color brighter()
Creates a brighter version of this color.

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

Returns:
a new Color object, a brighter version of this color.
Since:
JDK1.0
See Also:
darker()

darker

public Color darker()
Creates a darker version of this color.

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

Returns:
a new Color object, a darker version of this color.
Since:
JDK1.0
See Also:
brighter()

hashCode

public int hashCode()
Computes the hash code for this color.
Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
Since:
JDK1.0

equals

public boolean equals(Object obj)
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, and blue values as this object.

Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
Since:
JDK1.0

toString

public String toString()
Returns a string representation of this color. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.
Overrides:
toString in class Object
Returns:
a string representation of this color.

decode

public static Color decode(String nm)
                    throws NumberFormatException
Converts a string to an integer and returns the specified opaque color. This method handles string formats that are used to represent octal and hexidecimal numbers.
Parameters:
nm - a string that represents an opaque color as a 24-bit integer.
Returns:
the new color
Throws:
NumberFormatException - if the specified string cannot be interpreted as a decimal, octal, or hexidecimal integer.
Since:
JDK1.1
See Also:
Integer.decode(java.lang.String)

getColor

public static Color getColor(String nm)
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.

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

Parameters:
nm - the name of the color property
Returns:
the color value of the property.
Since:
JDK1.0
See Also:
System.getProperty(java.lang.String), Integer.getInteger(java.lang.String), Color(int)

getColor

public static Color getColor(String nm,
                             Color v)
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.

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:
nm - the name of the color property
v - the default color value.
Returns:
the color value of the property.
Since:
JDK1.0
See Also:
System.getProperty(java.lang.String), Integer.getInteger(java.lang.String), Color(int)

getColor

public static Color getColor(String nm,
                             int v)
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.

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.

Parameters:
nm - the name of the color property.
v - the default color value, as an integer.
Returns:
the color value of the property.
Since:
JDK1.0
See Also:
System.getProperty(java.lang.String), Integer.getInteger(java.lang.String), Color(int)

HSBtoRGB

public 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 integer that is returned by HSBtoRGB encodes the value of a color in bits 0&endash;23 of an integer value, 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.

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:
JDK1.0
See Also:
getRGB(), Color(int), ColorModel.getRGBdefault()

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, 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:
r - the red component of the color.
g - the green component of the color.
b - the blue component of the color.
hsbvals - the array to be 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:
JDK1.0
See Also:
getRGB(), Color(int), ColorModel.getRGBdefault()

getHSBColor

public static Color getHSBColor(float h,
                                float s,
                                float b)
Creates a Color object based on values supplied for the HSB color model.

Each of the three components should be a floating-point value between zero and one (a number in the range 0.0 ≤ h, s, b ≤ 1.0).

Parameters:
h - the hue component.
s - the saturation of the color.
b - the brightness of the color.
Returns:
a Color object with the specified hue, saturation, and brightness.
Since:
JDK1.0

getRGBComponents

public float[] getRGBComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, as represented in the default sRGB color space. If componentarray is null, an array of length 4 is created for the return value. Otherwise, componentarray must have length 4 or greater, and it will be filled in with the components and returned.
Returns:
the RGBA components in a float array

getRGBColorComponents

public float[] getRGBColorComponents(float[] compArray)
Returns a float array containing the color components (no alpha) of the Color, in the default sRGB color space. If componentarray is null, an array of length 3 is created for the return value. Otherwise, componentarray must have length 3 or greater, and it will be filled in with the components and returned.
Returns:
the RGB components in a float array

getComponents

public float[] getComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color. If componentarray 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, componentarray must have at least this length, and it will be filled in with the components and returned.
Returns:
the color and alpha components in a float array

getColorComponents

public float[] getColorComponents(float[] compArray)
Returns a float array containing the color components (no alpha) of the Color, in the ColorSpace of the color. If componentarray is null, an array with length equal to the number of components in the associated ColorSpace is created for the return value. Otherwise, componentarray must have at least this length, and it will be filled in with the components and returned.
Returns:
the color components in a float array

getComponents

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

getColorComponents

public float[] getColorComponents(ColorSpace cspace,
                                  float[] compArray)
Returns a float array containing the color components (no alpha) of the Color, in the ColorSpace specified by the cspace parameter. If componentarray is null, an array with length equal to the number of components in cspace is created for the return value. Otherwise, componentarray must have at least this length, and it will be filled in with the components and returned.
Returns:
the color components in a float array

getColorSpace

public ColorSpace getColorSpace()
Returns the ColorSpace of the Color.

createContext

public PaintContext createContext(ColorModel cm,
                                  Rectangle r,
                                  Rectangle2D r2d,
                                  AffineTransform xform,
                                  RenderingHints hints)
Create and return 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.
Specified by:
createContext in interface Paint
See Also:
Paint, PaintContext, Graphics2D.setPaint(java.awt.Paint)

getTransparency

public int getTransparency()
Return the transparency mode for this Color. This is required in to implement the Paint interface.
See Also:
Paint, Transparency, createContext(java.awt.image.ColorModel, java.awt.Rectangle, java.awt.geom.Rectangle2D, java.awt.geom.AffineTransform, java.awt.RenderingHints)

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.