Color
class is used encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a {@link ColorSpace}. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor. The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 - 1.0 or 0 - 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent. 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(tm) 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 .
Field Detail |
public static final java.awt.Color white
public static final java.awt.Color lightGray
public static final java.awt.Color gray
public static final java.awt.Color darkGray
public static final java.awt.Color black
public static final java.awt.Color red
public static final java.awt.Color pink
public static final java.awt.Color orange
public static final java.awt.Color yellow
public static final java.awt.Color green
public static final java.awt.Color magenta
public static final java.awt.Color cyan
public static final java.awt.Color blue
private transient long pData
int value
private float[] frgbvalue
ColorSpace
as float
components (no alpha). If null
after object construction, this must be an sRGB color constructed with 8-bit precision, so compute from the int
color value.private float[] fvalue
ColorSpace
as float
components (no alpha). If null
after object construction, this must be an sRGB color constructed with 8-bit precision, so compute from the int
color value.private float falpha
float
component. If frgbvalue
is null
, this is not valid data, so compute from the int
color value.private ColorSpace cs
ColorSpace
. If null
, then it's default is sRGB.private PaintContext theContext
Constructor Detail |
public Color(int r, int g, int b)
r
- the red componentg
- the green componentb
- the blue componentpublic Color(int r, int g, int b, int a)
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha componentpublic Color(int rgb)
rgb
- the combined RGB componentspublic Color(int rgba, boolean hasalpha)
hasalpha
argument is false
, alpha is defaulted to 255.rgba
- the combined RGBA componentshasalpha
- true
if the alpha bits are valid; false
otherwisepublic Color(float r, float g, float b)
r
- the red componentg
- the green componentb
- the blue componentpublic Color(float r, float g, float b, float a)
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha componentpublic Color(ColorSpace cspace, float[] components, float alpha)
ColorSpace
with the color components specified in the float
array and the specified alpha. The number of components is determined by the type of the ColorSpace
. For example, RGB requires 3 components, but CMYK requires 4 components.cspace
- the ColorSpace
to be used to interpret the componentscomponents
- an arbitrary number of color components that is compatible with thealpha
- alpha valueIllegalArgumentException
- if any of the values in the components
array or alpha
is outside of the range 0.0 to 1.0Method Detail |
private static native void initIDs()
private static void testColorValueRange(int r, int g, int b, int a)
r
- the Red componentg
- the Green componentb
- the Blue componentprivate static void testColorValueRange(float r, float g, float b, float a)
float
components supplied for validity. Throws an IllegalArgumentException
if the value is out of range.r
- the Red componentg
- the Green componentb
- the Blue componentpublic int getRed()
public int getGreen()
public int getBlue()
public int getAlpha()
public int getRGB()
ColorModel
.public java.awt.Color brighter()
Color
that is a brighter version of this Color
. This method applies an arbitrary scale factor to each of the three RGB components of this Color
to create a brighter version of this Color
. Although brighter
and darker
are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.
Color
object that is a brighter version of this Color
.public java.awt.Color darker()
Color
that is a darker version of this Color
. This method applies an arbitrary scale factor to each of the three RGB components of this Color
to create a darker version of this Color
. Although brighter
and darker
are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.
Color
object that is a darker version of this Color
.public int hashCode()
Color
.public boolean equals(Object obj)
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.
obj
- the object to test for equality with this Color
true
if the objects are the same; false
otherwise.public String toString()
Color
. This method is intended to be used only for debugging purposes. The content and format of the returned string might vary between implementations. The returned string might be empty but cannot be null
.Color
.public static java.awt.Color decode(String nm)
String
to an integer and returns the specified opaque Color
. This method handles string formats that are used to represent octal and hexidecimal numbers.nm
- a String
that represents an opaque color as a 24-bit integerColor
object.NumberFormatException
- if the specified string cannot be interpreted as a decimal, octal, or hexidecimal integer.public static java.awt.Color getColor(String nm)
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
object.
If the specified property is not found or could not be parsed as an integer then null
is returned.
nm
- the name of the color propertyColor
converted from the system property.public static java.awt.Color getColor(String nm, java.awt.Color v)
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
object.
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.
nm
- the name of the color propertyv
- the default Color
Color
converted from the system property, or the specified Color
.public static java.awt.Color getColor(String nm, int v)
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
object.
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
object.
nm
- the name of the color propertyv
- the default color value, as an integerColor
converted from the system property or the Color
converted from the specified integer.public static int HSBtoRGB(float hue, float saturation, float brightness)
The saturation
and brightness
components should be floating-point values between zero and one (numbers in the range 0.0-1.0). The hue
component can be any floating-point number. The floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
The integer that is returned by HSBtoRGB
encodes the value of a color in bits 0-23 of an integer value that is the same format used by the method {@link #getRGB() getRGB
}. This integer can be supplied as an argument to the Color
constructor that takes a single integer argument.
hue
- the hue component of the colorsaturation
- the saturation of the colorbrightness
- the brightness of the colorpublic static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)
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.
r
- the red component of the colorg
- the green component of the colorb
- the blue component of the colorhsbvals
- the array used to return the three HSB values, or null
public static java.awt.Color getHSBColor(float h, float s, float b)
Color
object based on the specified values for the HSB color model. The s
and b
components should be floating-point values between zero and one (numbers in the range 0.0-1.0). The h
component can be any floating-point number. The floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
h
- the hue components
- the saturation of the colorb
- the brightness of the colorColor
object with the specified hue, saturation, and brightness.public float[] getRGBComponents(float[] compArray)
float
array containing the color and alpha components of the Color
, as represented in the default sRGB color space. If compArray
is null
, an array of length 4 is created for the return value. Otherwise, compArray
must have length 4 or greater, and it is filled in with the components and returned.compArray
- an array that this method fills with color and alpha components and returnsfloat
array.public float[] getRGBColorComponents(float[] compArray)
float
array containing only the color components of the Color
, in the default sRGB color space. If compArray
is null
, an array of length 3 is created for the return value. Otherwise, compArray
must have length 3 or greater, and it is filled in with the components and returned.compArray
- an array that this method fills with color components and returnsfloat
array.public float[] getComponents(float[] compArray)
float
array containing the color and alpha components of the Color
, in the ColorSpace
of the Color
. If compArray
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, compArray
must have at least this length and it is filled in with the components and returned.compArray
- an array that this method fills with the color and alpha components of this Color
in its ColorSpace
and returnsfloat
array.public float[] getColorComponents(float[] compArray)
float
array containing only the color components of the Color
, in the ColorSpace
of the Color
. If compArray
is null
, an array with length equal to the number of components in the associated ColorSpace
is created for the return value. Otherwise, compArray
must have at least this length and it is filled in with the components and returned.compArray
- an array that this method fills with the color components of this Color
in its ColorSpace
and returnsfloat
array.public float[] getComponents(ColorSpace cspace, float[] compArray)
float
array containing the color and alpha components of the Color
, in the ColorSpace
specified by the cspace
parameter. If compArray
is null
, an array with length equal to the number of components in cspace
plus one is created for the return value. Otherwise, compArray
must have at least this length, and it is filled in with the components and returned.cspace
- a specified ColorSpace
compArray
- an array that this method fills with the color and alpha components of this Color
in the specified ColorSpace
and returnsfloat
array.public float[] getColorComponents(ColorSpace cspace, float[] compArray)
float
array containing only the color components of the Color
in the ColorSpace
specified by the cspace
parameter. If compArray
is null
, an array with length equal to the number of components in cspace
is created for the return value. Otherwise, compArray
must have at least this length, and it is filled in with the components and returned.cspace
- a specified ColorSpace
compArray
- an array that this method fills with the color components of this Color
in the specified ColorSpace
float
array.public ColorSpace getColorSpace()
ColorSpace
of this Color
.Color
object's ColorSpace
.public synchronized PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
Color
object to be used as an argument to any method requiring an object implementing the {@link Paint} interface. The same PaintContext
is returned, regardless of whether or not r
, r2d
, xform
, or hints
are null
.cm
- the specified ColorModel
r
- the specified {@link Rectangle}r2d
- the specified {@link Rectangle2D}xform
- the specified {@link AffineTransform}hints
- the specified {@link RenderingHints}PaintContext
that is used to generate a solid color pattern.public int getTransparency()
Color
. This is required to implement the Paint
interface.Color
object's transparency mode.