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:
|
component |
component |
component |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Color objects can be created in two ways:
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.
public final static Color black = new Color(0, 0, 0)
public final static Color blue = new Color(0, 0, 255)
public final static Color cyan = new Color(0, 255, 255)
public final static Color darkGray = new Color(64, 64, 64)
public final static Color gray = new Color(128, 128, 128)
public final static Color green = new Color(0, 255, 0);
public final static Color lightGray = new Color(192, 192,
192)
public final static Color magenta = new Color(255, 0, 255)
public final static Color orange = new Color(255, 200, 0)
public final static Color pink = new Color(255, 175, 175)
public final static Color red = new Color(255, 0, 0)
public final static Color white = new Color(255, 255, 255)
public final static Color yellow = new Color(255, 255, 0)
public Color(float r,
float g, float b)
r -
the red component
g -
the red component
b -
the red component
public Color(int r, int g, int
b)
r -
the red component
g -
the green component
b -
the blue component
public Color brighter()
- Returns:
- a brighter version of this color.
public Color darker()
- Returns:
- a darker version of this color.
public boolean equals(Object obj)
obj -
the object to compare with
public int getBlue()
- Returns:
- the blue component of this color (an integer in the range 0 to 255).
public int getGreen()
- Returns:
- the green component of this color (an integer in the range 0 to 255).
public int getRed()
- Returns:
- the red component of this color (an integer in the range 0 to 255).
public String toString()
- Returns:
- a string representation of this color.