All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.awt.Color

java.lang.Object
   |
   +----java.awt.Color

public class Color
extends Object
implements Serializable
This class encapsulates colors using the RGB format. In RGB format, the red, blue, and green components of a color are each represented by an integer in the range 0-255. The value 0 indicates no contribution from this primary color. The value 255 indicates the maximum intensity of this color component.

Although the Color class is based on the three-component RGB model, the class provides a set of convenience methods for converting between RGB and HSB colors. For a definition of the RGB and HSB color models, see Foley, van Dam, Feiner, and Hughes, Computer Graphics: Principles and Practice.


Variable Index

 o black
The color black.
 o blue
The color blue.
 o cyan
The color cyan.
 o darkGray
The color dark gray.
 o gray
The color gray.
 o green
The color green.
 o lightGray
The color light gray.
 o magenta
The color magenta.
 o orange
The color orange.
 o pink
The color pink.
 o red
The color red.
 o white
The color white.
 o yellow
The color yellow.

Constructor Index

 o Color(float, float, float)
Creates a color with the specified red, green, and blue values, where each of the values is in the range 0.0-1.0.
 o Color(int)
Creates a color with the specified RGB value, where the red component is in bits 16-23 of the argument, the green component is in bits 8-15 of the argument, and the blue component is in bits 0-7.
 o Color(int, int, int)
Creates a color with the specified red, green, and blue components.

Method Index

 o brighter()
Creates a brighter version of this color.
 o darker()
Creates a darker version of this color.
 o decode(String)
Converts a string to an integer and returns the specified color.
 o equals(Object)
Determines whether another object is equal to this color.
 o getBlue()
Gets the blue component of this color.
 o getColor(String)
Finds a color in the system properties.
 o getColor(String, Color)
Finds a color in the system properties.
 o getColor(String, int)
Finds a color in the system properties.
 o getGreen()
Gets the green component of this color.
 o getHSBColor(float, float, float)
Creates a Color object based on values supplied for the HSB color model.
 o getRed()
Gets the red component of this color.
 o getRGB()
Gets the RGB value representing the color in the default RGB ColorModel.
 o hashCode()
Computes the hash code for this color.
 o HSBtoRGB(float, float, float)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the RGB model.
 o RGBtoHSB(int, int, int, float[])
Converts the components of a color, as specified by the RGB model, to an equivalent set of values for hue, saturation, and brightness, the three components of the HSB model.
 o toString()
Creates a string that represents this color and indicates the values of its RGB components.

Variables

 o white
 public static final Color white
The color white.

 o lightGray
 public static final Color lightGray
The color light gray.

 o gray
 public static final Color gray
The color gray.

 o darkGray
 public static final Color darkGray
The color dark gray.

 o black
 public static final Color black
The color black.

 o red
 public static final Color red
The color red.

 o pink
 public static final Color pink
The color pink.

 o orange
 public static final Color orange
The color orange.

 o yellow
 public static final Color yellow
The color yellow.

 o green
 public static final Color green
The color green.

 o magenta
 public static final Color magenta
The color magenta.

 o cyan
 public static final Color cyan
The color cyan.

 o blue
 public static final Color blue
The color blue.

Constructors

 o Color
 public Color(int r,
              int g,
              int b)
Creates a color with the specified red, green, and blue components. The three arguments must each be 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.

Parameters:
r - the red component.
g - the green component.
b - the blue component.
See Also:
getRed., getGreen., getBlue., getRGB.
 o Color
 public Color(int rgb)
Creates a color with the specified RGB value, where the red component is in bits 16-23 of the argument, the green component is in bits 8-15 of the argument, and the blue component is in bits 0-7. The value zero indicates no contribution from the primary color component.

The actual color used in rendering depends on finding the best match given the color space available for a given output device.

Parameters:
rgb - an integer giving the red, green, and blue components.
See Also:
getRGBdefault, getRed., getGreen., getBlue., getRGB.
 o Color
 public Color(float r,
              float g,
              float b)
Creates a color with the specified red, green, and blue values, where each of the values is in the range 0.0-1.0. The value 0.0 indicates no contribution from the primary color component. The value 1.0 indicates the maximum intensity of the primary color component.

The actual color used in rendering depends on finding the best match given the color space available for a given output device.

Parameters:
r - the red component
g - the red component
b - the red component
See Also:
getRed., getGreen., getBlue., getRGB.

Methods

 o getRed
 public int getRed()
Gets the red component of this color. The result is an integer in the range 0 to 255.

Returns:
the red component of this color.
See Also:
getRGB
 o getGreen
 public int getGreen()
Gets the green component of this color. The result is an integer in the range 0 to 255.

Returns:
the green component of this color.
See Also:
getRGB
 o getBlue
 public int getBlue()
Gets the blue component of this color. The result is an integer in the range 0 to 255.

Returns:
the blue component of this color.
See Also:
getRGB
 o getRGB
 public int getRGB()
Gets the RGB value representing the color in the default RGB ColorModel. The red, green, and blue components of the color are each scaled to be a value between 0 (abscence of the color) and 255 (complete saturation). Bits 24-31 of the returned integer are 0xff, bits 16-23 are the red value, bit 8-15 are the green value, and bits 0-7 are the blue value.

See Also:
getRGBdefault, getRed, getGreen, getBlue
 o 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.
See Also:
darker
 o 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.
See Also:
brighter
 o hashCode
 public int hashCode()
Computes the hash code for this color.

Returns:
a hash code value for this object.
Overrides:
hashCode in class Object
 o 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.

Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
Overrides:
equals in class Object
 o toString
 public String toString()
Creates a string that represents this color and indicates the values of its RGB components.

Returns:
a representation of this color as a String object.
Overrides:
toString in class Object
 o decode
 public static Color decode(String nm) throws NumberFormatException
Converts a string to an integer and returns the specified color. This method handles string formats that are used to represent octal and hexidecimal numbers.

Parameters:
nm - a string that represents a 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.
See Also:
decode
 o 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.
See Also:
getProperty, getInteger, Color
 o 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.
See Also:
getProperty, getInteger, Color
 o 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.
See Also:
getProperty, getInteger, Color
 o 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 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.
See Also:
getRGB, Color
 o RGBtoHSB
 public static float[] RGBtoHSB(int r,
                                int g,
                                int b,
                                float hsbvals[])
Converts the components of a color, as specified by the 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.
See Also:
getRGB, Color
 o 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.

All Packages  Class Hierarchy  This Package  Previous  Next  Index

Submit a bug or feature - Version 1.1.8 of Java Platform API Specification
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1995-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.