Colors are defined by the class
java.awt.Color
The sRGB (standard Red-Green-Blue) is the default colorspace.
It spans a subset of the standard CIEXYZ color space that includes
all visible colors (ref. Knudsen).
A color model, based on a particular color space, specifies exactly
how a color is represented. In Java the default ARGB model includes
the RGB components plus an alpha transparency factor that specifies
what happens when a color is drawn over another. Other color models,
such as a gray scale model, can also be obtained.
For convenience, there are several Color
constructors. Some constructors take int values between 0 and 255
for each color component. Some take float values between 0.0f and
1.0f. There are 4-parameter constructors that let you specify the
R, G, B, and alpha values and 3-parameter versions that default
to opaque alpha. In integer format, alpha is 0 for completely transparent
and 255 for opaque and in floating-point these are 0.0f/1.0f, respectively.
To specify an arbitrary color you use constructors with either
int 0-255,
or float 0.0-1.0
value ranges for each of the three color components:
Color
red = Color( 0xFF,0,0); // R,G,B, default alpha = 255
Color blue = Color(0.0,0.0,1.0);// R,G,B, default alpha
= 1.0
Note that it is common to use hexadecimal values to specify color
components and here we used 0xFF instead of the decimal 255 for
the red component. The three color components and alpha, each represented
by a byte value, can be packed into an int.
One constructor has a single int
argument for packed RGB (alpha defaults to 0xFF) and another constructor
has an int
plus a boolean
that is true
if the integer value includes an alpha component as shown here
int
intRed = 0xFF0000; // r=0xFF, g=0x00, b=0x00;
Color red = new Color(intRed); // default alpha = 0xFF
int intGreen = 0x8800FF00;// alpha=0x88, r=0x00, g=0xFF,
b=0x00
Color green = new Color(intGreen, true);
Using hex numbers makes for a compact representation that allows
you to quickly see the component values. In Chapter
10 we discuss bit handling and how to access and modify the
bytes in a pixel integer value.
Partially transparent red and blue colors are created in these
two examples:
Color
transparentRed = new Color (0xFF, 0, 0, 0x33); // R,G,B,Alpha
Color transparentBlue = new Color (0.0f, 0.0f, 1.0f,
0.5f); // R,G,B,Alpha
The transparency factor on the image color determines what percentage
of the component's background color shows through.
For convenience, the class definition includes several colors as
class constants, such as
Color.BLUE,Color.WHITE,
Color.RED, etc.
See the API
Specifications for a list of all the color constants.
(Lower case names, e.g. Color.blue,
are legacies of Java 1.0. These capitalized versions follow the
standard of all capital letters for constants.)
To set the current drawing color in the graphics context:
g.setColor(Color
c);//g = Graphics object
The background and foreground colors of components,
such as for a panel, can be set using
setBackground(Color
c);
setForeground(Color c);
Similarly, "getter" methods are paired with
these methods to obtain the colors:
Color
c = g.getColor();//g = Graphics object
//
In an applet or other Component object:
Color bg = getBackground();
Color fg = getForeground();
References & Web Resources
Latest update: Oct. 27, 2005
|