The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a 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 .

@version
10 Feb 1997
@author
Sami Shaio
@author
Arthur van Hoff
Creates an opaque sRGB color with the specified red, green, and blue values 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. Alpha is defaulted to 255.
Parameters
rthe red component
gthe green component
bthe blue component
Throws
IllegalArgumentExceptionif r, g or b are outside of the range 0 to 255, inclusive
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
Parameters
rthe red component
gthe green component
bthe blue component
athe alpha component
Throws
IllegalArgumentExceptionif r, g, b or a are outside of the range 0 to 255, inclusive
Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. The actual color used in rendering depends on finding the best match given the color space available for a particular output device. Alpha is defaulted to 255.
Parameters
rgbthe combined RGB components
Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255.
Parameters
rgbathe combined RGBA components
hasalphatrue if the alpha bits are valid; false otherwise
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0). Alpha is defaulted to 1.0. The actual color used in rendering depends on finding the best match given the color space available for a particular output device.
Parameters
rthe red component
gthe green component
bthe blue component
Throws
IllegalArgumentExceptionif r, g or b are outside of the range 0.0 to 1.0, inclusive
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0). The actual color used in rendering depends on finding the best match given the color space available for a particular output device.
Parameters
rthe red component
gthe green component
bthe blue component
athe alpha component
Throws
IllegalArgumentExceptionif r, g b or a are outside of the range 0.0 to 1.0, inclusive
Creates a color in the specified 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.
Parameters
cspacethe ColorSpace to be used to interpret the components
componentsan arbitrary number of color components that is compatible with the
alphaalpha value
Throws
IllegalArgumentExceptionif any of the values in the components array or alpha is outside of the range 0.0 to 1.0
The color black. In the default sRGB space.
The color black. In the default sRGB space.
The color blue. In the default sRGB space.
The color blue. In the default sRGB space.
The color cyan. In the default sRGB space.
The color cyan. In the default sRGB space.
The color dark gray. In the default sRGB space.
The color dark gray. In the default sRGB space.
The color gray. In the default sRGB space.
The color gray. In the default sRGB space.
The color green. In the default sRGB space.
The color green. In the default sRGB space.
The color light gray. In the default sRGB space.
The color light gray. In the default sRGB space.
The color magenta. In the default sRGB space.
The color magenta. In the default sRGB space.
The color orange. In the default sRGB space.
The color orange. In the default sRGB space.
The color pink. In the default sRGB space.
The color pink. In the default sRGB space.
The color red. In the default sRGB space.
The color red. In the default sRGB space.
The color white. In the default sRGB space.
The color white. In the default sRGB space.
The color yellow. In the default sRGB space.
The color yellow. In the default sRGB space.
Creates a new 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.

Return
a new Color object that is a brighter version of this Color.
@since
JDK1.0
Creates and returns a PaintContext used to generate the color pattern. Since the ColorModel argument to createContext is only a hint, implementations of Paint should accept a null argument for ColorModel. Note that if the application does not prefer a specific ColorModel, the null ColorModel argument will give the Paint implementation full leeway in using the most efficient ColorModel it prefers for its raster processing.

Since the API documentation was not specific about this in releases before 1.4, there may be implementations of Paint that do not accept a null ColorModel argument. If a developer is writing code which passes a null ColorModel argument to the createContext method of Paint objects from arbitrary sources it would be wise to code defensively by manufacturing a non-null ColorModel for those objects which throw a NullPointerException.

Parameters
cmthe {@link ColorModel} that receives the Paint data. This is used only as a hint.
deviceBoundsthe device space bounding box of the graphics primitive being rendered
userBoundsthe user space bounding box of the graphics primitive being rendered
xformthe {@link AffineTransform} from user space into device space
hintsthe hint that the context object uses to choose between rendering alternatives
Return
the PaintContext for generating color patterns
See Also
Creates a new 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.

Return
a new Color object that is a darker version of this Color.
@since
JDK1.0
Converts a String to an integer and returns the specified opaque Color. This method handles string formats that are used to represent octal and hexidecimal numbers.
Parameters
nma String that represents an opaque color as a 24-bit integer
Return
the new Color object.
Throws
NumberFormatExceptionif the specified string cannot be interpreted as a decimal, octal, or hexidecimal integer.
@since
JDK1.1
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, blue, and alpha values as this object.

Parameters
objthe object to test for equality with this Color
Return
true if the objects are the same; false otherwise.
@since
JDK1.0
Returns the alpha component in the range 0-255.
Return
the alpha component.
See Also
Returns the blue component in the range 0-255 in the default sRGB space.
Return
the blue component.
See Also
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Return
The java.lang.Class object that represents the runtime class of the object. The result is of type {@code Class} where X is the erasure of the static type of the expression on which getClass is called.
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 object.

If the specified property is not found or could not be parsed as an integer then null is returned.

Parameters
nmthe name of the color property
Return
the Color converted from the system property.
@since
JDK1.0
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 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.

Parameters
nmthe name of the color property
vthe default Color
Return
the Color converted from the system property, or the specified Color.
@since
JDK1.0
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 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.

Parameters
nmthe name of the color property
vthe default color value, as an integer
Return
the Color converted from the system property or the Color converted from the specified integer.
@since
JDK1.0
Returns a 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.
Parameters
cspacea specified ColorSpace
compArrayan array that this method fills with the color components of this Color in the specified ColorSpace
Return
the color components in a float array.
Returns a 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.
Parameters
compArrayan array that this method fills with the color components of this Color in its ColorSpace and returns
Return
the color components in a float array.
Returns the ColorSpace of this Color.
Return
this Color object's ColorSpace.
Returns a 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.
Parameters
cspacea specified ColorSpace
compArrayan array that this method fills with the color and alpha components of this Color in the specified ColorSpace and returns
Return
the color and alpha components in a float array.
Returns a 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.
Parameters
compArrayan array that this method fills with the color and alpha components of this Color in its ColorSpace and returns
Return
the color and alpha components in a float array.
Returns the green component in the range 0-255 in the default sRGB space.
Return
the green component.
See Also
Creates a 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.

Parameters
hthe hue component
sthe saturation of the color
bthe brightness of the color
Return
a Color object with the specified hue, saturation, and brightness.
@since
JDK1.0
Returns the red component in the range 0-255 in the default sRGB space.
Return
the red component.
See Also
Returns the RGB value representing the color in the default sRGB ColorModel . (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
Return
the RGB value of the color in the default sRGB ColorModel.
@since
JDK1.0
Returns a 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.
Parameters
compArrayan array that this method fills with color components and returns
Return
the RGB components in a float array.
Returns a 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.
Parameters
compArrayan array that this method fills with color and alpha components and returns
Return
the RGBA components in a float array.
Returns the transparency mode for this Color. This is required to implement the Paint interface.
Return
this Color object's transparency mode.
Computes the hash code for this Color.
Return
a hash code value for this object.
@since
JDK1.0
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.

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 getRGB . This integer can be supplied as an argument to the Color constructor that takes a single integer argument.

Parameters
huethe hue component of the color
saturationthe saturation of the color
brightnessthe brightness of the color
Return
the RGB value of the color with the indicated hue, saturation, and brightness.
@since
JDK1.0
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are 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
rthe red component of the color
gthe green component of the color
bthe blue component of the color
hsbvalsthe array used to return the three HSB values, or null
Return
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.
@since
JDK1.0
Returns a string representation of this 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.
Return
a string representation of this Color.
Causes current thread to wait until another thread invokes the method or the method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.

The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     }
 
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Parameters
timeoutthe maximum time to wait in milliseconds.
Throws
IllegalArgumentExceptionif the value of timeout is negative.
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:

 1000000*timeout+nanos

In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout, nanos);
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Parameters
timeoutthe maximum time to wait in milliseconds.
nanosadditional time, in nanoseconds range 0-999999.
Throws
IllegalArgumentExceptionif the value of timeout is negative or the value of nanos is not in the range 0-999999.
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.