Color Contract

An instance of Java's Color class represents a particular color. Each color is viewed as consisting of a red, blue, and green component (so-called RGB format), where each component ranges from 0 to 255. The component value 0 indicates no contribution by that color, while the component value 255 indicates maximal contribution by that color. Move the sliders below to mix together a color out of the RGB components:



The following table shows the RGB components of some common colors.

Color Name

Red
component

Green
component

Blue
component

black

0

0

0

blue

0

0

255

cyan

0

255

255

darkGray

64

64

64

gray

128

128

128

green

0

255

0

lighGray

192

192

192

magenta

255

0

255

orange

255

200

0

pink

255

175

175

red

255

0

0

white

255

255

255

yellow

255

255

0

Color objects can be created in two ways:

  1. new Color(red, green, blue), where red, green, and blue are integers in the range 0 to 255. For example a green color can be created by new Color(0,0,255) and an orange color can be created by new Color(255,200,0).
  2. new Color(red, green, blue), where red, green, and blue are floating point numbers in the range 0.0 to 1.0 . In this scale, 0.0 means no contribution of a component, while 1.0 means maximal contribution of a component. For example, a yellow color can be created by is new Color(1.0, 1.0, 0.0) and a dark gray color can be created by new Color(.25, .25, .25).

All of the colors appearing in the above table are predefined constants of the Color class, and can be referred to as Color.name, where name is the name of the color.

The methods of a color object include extracting its red, green, and blue components, and creating "brighter" and "darker" versions of the color.

Below is a simple contract for the Color class. The Color class actually implements more constructors and methods than are described here. The full list of constructors and methods can be found in the Java API for the class java.awt.Color.

 


Index for the Color Class

Class Constants:

Constructors:

Methods:


Constants of the Color class


black

public final static Color black = new Color(0, 0, 0)

The color black.


blue

public final static Color blue = new Color(0, 0, 255)

The color blue.


cyan

public final static Color cyan = new Color(0, 255, 255)

The color cyan.


darkGray

public final static Color darkGray = new Color(64, 64, 64)

The color dark gray.


gray

public final static Color gray = new Color(128, 128, 128)

The color gray.


green

public final static Color green = new Color(0, 255, 0);

The color green.


lightGray

public final static Color lightGray = new Color(192, 192, 192)

The color light gray.


magenta

public final static Color magenta = new Color(255, 0, 255)

The color magneta.


orange

public final static Color orange = new Color(255, 200, 0)

The color orange.


pink

public final static Color pink = new Color(255, 175, 175)

The color pink.


red

public final static Color red = new Color(255, 0, 0)

The color red.


white

public final static Color white = new Color(255, 255, 255)

The color white.


yellow

public final static Color yellow = new Color(255, 255, 0)

The color yellow.



Constructors of the Color class


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


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



Methods of Color class


brighter

public Color brighter()

Returns:
a brighter version of this color.


darker

public Color darker()

Returns:
a darker version of this color.


equals

public boolean equals(Object obj)

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 value as this object.

Parameters:
obj - the object to compare with
Returns:
true if the objects are the same; false otherwise.


getBlue

public int getBlue()

Returns:
the blue component of this color (an integer in the range 0 to 255).


getGreen

public int getGreen()

Returns:
the green component of this color (an integer in the range 0 to 255).


getRed

public int getRed()

Returns:
the red component of this color (an integer in the range 0 to 255).


toString

public String toString()

Returns:
a string representation of this color.


Franklyn Turbak adapted this page from the
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to
doug.kramer@sun.com