- java.lang.Object
-
- java.awt.Color
-
- All Implemented Interfaces:
- Paint, Transparency, Serializable
- Direct Known Subclasses:
- ColorUIResource, SystemColor
public class Color extends Object implements Paint, Serializable
TheColor
class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by aColorSpace
. 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 aColor
with an explicit alpha or getting the color/alpha components of aColor
, 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 .
- See Also:
ColorSpace
,AlphaComposite
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static Color
black
The color black.static Color
BLACK
The color black.static Color
blue
The color blue.static Color
BLUE
The color blue.static Color
cyan
The color cyan.static Color
CYAN
The color cyan.static Color
DARK_GRAY
The color dark gray.static Color
darkGray
The color dark gray.static Color
gray
The color gray.static Color
GRAY
The color gray.static Color
green
The color green.static Color
GREEN
The color green.static Color
LIGHT_GRAY
The color light gray.static Color
lightGray
The color light gray.static Color
magenta
The color magenta.static Color
MAGENTA
The color magenta.static Color
orange
The color orange.static Color
ORANGE
The color orange.static Color
pink
The color pink.static Color
PINK
The color pink.static Color
red
The color red.static Color
RED
The color red.static Color
white
The color white.static Color
WHITE
The color white.static Color
yellow
The color yellow.static Color
YELLOW
The color yellow.-
Fields inherited from interface java.awt.Transparency
BITMASK, OPAQUE, TRANSLUCENT
-
-
Constructor Summary
Constructors Constructor and Description Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specifiedColorSpace
with the color components specified in thefloat
array and the specified alpha.Color(float r, float g, float b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).Color(float r, float g, float b, float a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).Color(int rgb)
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.Color(int rgba, boolean hasalpha)
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.Color(int r, int g, int b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
-
Method Summary
Methods Modifier and Type Method and Description Color
brighter()
Creates a newColor
that is a brighter version of thisColor
.PaintContext
createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
Creates and returns aPaintContext
used to generate a solid color field pattern.Color
darker()
Creates a newColor
that is a darker version of thisColor
.static Color
decode(String nm)
Converts aString
to an integer and returns the specified opaqueColor
.boolean
equals(Object obj)
Determines whether another object is equal to thisColor
.int
getAlpha()
Returns the alpha component in the range 0-255.int
getBlue()
Returns the blue component in the range 0-255 in the default sRGB space.static Color
getColor(String nm)
Finds a color in the system properties.static Color
getColor(String nm, Color v)
Finds a color in the system properties.static Color
getColor(String nm, int v)
Finds a color in the system properties.float[]
getColorComponents(ColorSpace cspace, float[] compArray)
Returns afloat
array containing only the color components of theColor
in theColorSpace
specified by thecspace
parameter.float[]
getColorComponents(float[] compArray)
Returns afloat
array containing only the color components of theColor
, in theColorSpace
of theColor
.ColorSpace
getColorSpace()
Returns theColorSpace
of thisColor
.float[]
getComponents(ColorSpace cspace, float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, in theColorSpace
specified by thecspace
parameter.float[]
getComponents(float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, in theColorSpace
of theColor
.int
getGreen()
Returns the green component in the range 0-255 in the default sRGB space.static Color
getHSBColor(float h, float s, float b)
Creates aColor
object based on the specified values for the HSB color model.int
getRed()
Returns the red component in the range 0-255 in the default sRGB space.int
getRGB()
Returns the RGB value representing the color in the default sRGBColorModel
.float[]
getRGBColorComponents(float[] compArray)
Returns afloat
array containing only the color components of theColor
, in the default sRGB color space.float[]
getRGBComponents(float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, as represented in the default sRGB color space.int
getTransparency()
Returns the transparency mode for thisColor
.int
hashCode()
Computes the hash code for thisColor
.static int
HSBtoRGB(float hue, float saturation, float brightness)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.static float[]
RGBtoHSB(int r, int g, int b, float[] hsbvals)
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.String
toString()
Returns a string representation of thisColor
.
-
-
-
Field Detail
-
white
public static final Color white
The color white. In the default sRGB space.
-
WHITE
public static final Color WHITE
The color white. In the default sRGB space.- Since:
- 1.4
-
lightGray
public static final Color lightGray
The color light gray. In the default sRGB space.
-
LIGHT_GRAY
public static final Color LIGHT_GRAY
The color light gray. In the default sRGB space.- Since:
- 1.4
-
gray
public static final Color gray
The color gray. In the default sRGB space.
-
GRAY
public static final Color GRAY
The color gray. In the default sRGB space.- Since:
- 1.4
-
darkGray
public static final Color darkGray
The color dark gray. In the default sRGB space.
-
DARK_GRAY
public static final Color DARK_GRAY
The color dark gray. In the default sRGB space.- Since:
- 1.4
-
black
public static final Color black
The color black. In the default sRGB space.
-
BLACK
public static final Color BLACK
The color black. In the default sRGB space.- Since:
- 1.4
-
red
public static final Color red
The color red. In the default sRGB space.
-
RED
public static final Color RED
The color red. In the default sRGB space.- Since:
- 1.4
-
pink
public static final Color pink
The color pink. In the default sRGB space.
-
PINK
public static final Color PINK
The color pink. In the default sRGB space.- Since:
- 1.4
-
orange
public static final Color orange
The color orange. In the default sRGB space.
-
ORANGE
public static final Color ORANGE
The color orange. In the default sRGB space.- Since:
- 1.4
-
yellow
public static final Color yellow
The color yellow. In the default sRGB space.
-
YELLOW
public static final Color YELLOW
The color yellow. In the default sRGB space.- Since:
- 1.4
-
green
public static final Color green
The color green. In the default sRGB space.
-
GREEN
public static final Color GREEN
The color green. In the default sRGB space.- Since:
- 1.4
-
magenta
public static final Color magenta
The color magenta. In the default sRGB space.
-
MAGENTA
public static final Color MAGENTA
The color magenta. In the default sRGB space.- Since:
- 1.4
-
cyan
public static final Color cyan
The color cyan. In the default sRGB space.
-
CYAN
public static final Color CYAN
The color cyan. In the default sRGB space.- Since:
- 1.4
-
blue
public static final Color blue
The color blue. In the default sRGB space.
-
BLUE
public static final Color BLUE
The color blue. In the default sRGB space.- Since:
- 1.4
-
-
Constructor Detail
-
Color
public Color(int r, int g, int b)
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:
r
- the red componentg
- the green componentb
- the blue component- Throws:
IllegalArgumentException
- ifr
,g
orb
are outside of the range 0 to 255, inclusive- See Also:
getRed()
,getGreen()
,getBlue()
,getRGB()
-
Color
@ConstructorProperties(value={"red","green","blue","alpha"}) public Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).- Parameters:
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha component- Throws:
IllegalArgumentException
- ifr
,g
,b
ora
are outside of the range 0 to 255, inclusive- See Also:
getRed()
,getGreen()
,getBlue()
,getAlpha()
,getRGB()
-
Color
public Color(int rgb)
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:
rgb
- the combined RGB components- See Also:
ColorModel.getRGBdefault()
,getRed()
,getGreen()
,getBlue()
,getRGB()
-
Color
public Color(int rgba, boolean hasalpha)
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 thehasalpha
argument isfalse
, alpha is defaulted to 255.- Parameters:
rgba
- the combined RGBA componentshasalpha
-true
if the alpha bits are valid;false
otherwise- See Also:
ColorModel.getRGBdefault()
,getRed()
,getGreen()
,getBlue()
,getAlpha()
,getRGB()
-
Color
public Color(float r, float g, float b)
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:
r
- the red componentg
- the green componentb
- the blue component- Throws:
IllegalArgumentException
- ifr
,g
orb
are outside of the range 0.0 to 1.0, inclusive- See Also:
getRed()
,getGreen()
,getBlue()
,getRGB()
-
Color
public Color(float r, float g, float b, float a)
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:
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha component- Throws:
IllegalArgumentException
- ifr
,g
b
ora
are outside of the range 0.0 to 1.0, inclusive- See Also:
getRed()
,getGreen()
,getBlue()
,getAlpha()
,getRGB()
-
Color
public Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specifiedColorSpace
with the color components specified in thefloat
array and the specified alpha. The number of components is determined by the type of theColorSpace
. For example, RGB requires 3 components, but CMYK requires 4 components.- Parameters:
cspace
- theColorSpace
to be used to interpret the componentscomponents
- an arbitrary number of color components that is compatible with theColorSpace
alpha
- alpha value- Throws:
IllegalArgumentException
- if any of the values in thecomponents
array oralpha
is outside of the range 0.0 to 1.0- See Also:
getComponents(float[])
,getColorComponents(float[])
-
-
Method Detail
-
getRed
public int getRed()
Returns the red component in the range 0-255 in the default sRGB space.- Returns:
- the red component.
- See Also:
getRGB()
-
getGreen
public int getGreen()
Returns the green component in the range 0-255 in the default sRGB space.- Returns:
- the green component.
- See Also:
getRGB()
-
getBlue
public int getBlue()
Returns the blue component in the range 0-255 in the default sRGB space.- Returns:
- the blue component.
- See Also:
getRGB()
-
getAlpha
public int getAlpha()
Returns the alpha component in the range 0-255.- Returns:
- the alpha component.
- See Also:
getRGB()
-
getRGB
public int getRGB()
Returns the RGB value representing the color in the default sRGBColorModel
. (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).- Returns:
- the RGB value of the color in the default sRGB
ColorModel
. - Since:
- JDK1.0
- See Also:
ColorModel.getRGBdefault()
,getRed()
,getGreen()
,getBlue()
-
brighter
public Color brighter()
Creates a newColor
that is a brighter version of thisColor
.This method applies an arbitrary scale factor to each of the three RGB components of this
Color
to create a brighter version of thisColor
. Thealpha
value is preserved. Althoughbrighter
anddarker
are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.- Returns:
- a new
Color
object that is a brighter version of thisColor
with the samealpha
value. - Since:
- JDK1.0
- See Also:
darker()
-
darker
public Color darker()
Creates a newColor
that is a darker version of thisColor
.This method applies an arbitrary scale factor to each of the three RGB components of this
Color
to create a darker version of thisColor
. Thealpha
value is preserved. Althoughbrighter
anddarker
are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.- Returns:
- a new
Color
object that is a darker version of thisColor
with the samealpha
value. - Since:
- JDK1.0
- See Also:
brighter()
-
hashCode
public int hashCode()
Computes the hash code for thisColor
.- Overrides:
hashCode
in classObject
- Returns:
- a hash code value for this object.
- Since:
- JDK1.0
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
-
equals
public boolean equals(Object obj)
Determines whether another object is equal to thisColor
.The result is
true
if and only if the argument is notnull
and is aColor
object that has the same red, green, blue, and alpha values as this object.- Overrides:
equals
in classObject
- Parameters:
obj
- the object to test for equality with thisColor
- Returns:
true
if the objects are the same;false
otherwise.- Since:
- JDK1.0
- See Also:
Object.hashCode()
,HashMap
-
toString
public String toString()
Returns a string representation of thisColor
. 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 benull
.
-
decode
public static Color decode(String nm) throws NumberFormatException
Converts aString
to an integer and returns the specified opaqueColor
. This method handles string formats that are used to represent octal and hexadecimal numbers.- Parameters:
nm
- aString
that represents an opaque color as a 24-bit integer- Returns:
- the new
Color
object. - Throws:
NumberFormatException
- if the specified string cannot be interpreted as a decimal, octal, or hexadecimal integer.- Since:
- JDK1.1
- See Also:
Integer.decode(java.lang.String)
-
getColor
public static Color getColor(String nm)
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:
nm
- the name of the color property- Returns:
- the
Color
converted from the system property. - Since:
- JDK1.0
- See Also:
System.getProperty(java.lang.String)
,Integer.getInteger(java.lang.String)
,Color(int)
-
getColor
public static Color getColor(String nm, Color v)
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:
nm
- the name of the color propertyv
- the defaultColor
- Returns:
- the
Color
converted from the system property, or the specifiedColor
. - Since:
- JDK1.0
- See Also:
System.getProperty(java.lang.String)
,Integer.getInteger(java.lang.String)
,Color(int)
-
getColor
public static Color getColor(String nm, int v)
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 aColor
object.- Parameters:
nm
- the name of the color propertyv
- the default color value, as an integer- Returns:
- the
Color
converted from the system property or theColor
converted from the specified integer. - Since:
- JDK1.0
- See Also:
System.getProperty(java.lang.String)
,Integer.getInteger(java.lang.String)
,Color(int)
-
HSBtoRGB
public static int HSBtoRGB(float hue, float saturation, float brightness)
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
andbrightness
components should be floating-point values between zero and one (numbers in the range 0.0-1.0). Thehue
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
. This integer can be supplied as an argument to thegetRGB
Color
constructor that takes a single integer argument.- Parameters:
hue
- the hue component of the colorsaturation
- the saturation of the colorbrightness
- the brightness of the color- Returns:
- the RGB value of the color with the indicated hue, saturation, and brightness.
- Since:
- JDK1.0
- See Also:
getRGB()
,Color(int)
,ColorModel.getRGBdefault()
-
RGBtoHSB
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)
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 isnull
, then a new array is allocated to return the result. Otherwise, the method returns the arrayhsbvals
, with the values put into that array.- Parameters:
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, ornull
- Returns:
- 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
- See Also:
getRGB()
,Color(int)
,ColorModel.getRGBdefault()
-
getHSBColor
public static Color getHSBColor(float h, float s, float b)
Creates aColor
object based on the specified values for the HSB color model.The
s
andb
components should be floating-point values between zero and one (numbers in the range 0.0-1.0). Theh
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:
h
- the hue components
- the saturation of the colorb
- the brightness of the color- Returns:
- a
Color
object with the specified hue, saturation, and brightness. - Since:
- JDK1.0
-
getRGBComponents
public float[] getRGBComponents(float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, as represented in the default sRGB color space. IfcompArray
isnull
, 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:
compArray
- an array that this method fills with color and alpha components and returns- Returns:
- the RGBA components in a
float
array.
-
getRGBColorComponents
public float[] getRGBColorComponents(float[] compArray)
Returns afloat
array containing only the color components of theColor
, in the default sRGB color space. IfcompArray
isnull
, 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:
compArray
- an array that this method fills with color components and returns- Returns:
- the RGB components in a
float
array.
-
getComponents
public float[] getComponents(float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, in theColorSpace
of theColor
. IfcompArray
isnull
, an array with length equal to the number of components in the associatedColorSpace
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:
compArray
- an array that this method fills with the color and alpha components of thisColor
in itsColorSpace
and returns- Returns:
- the color and alpha components in a
float
array.
-
getColorComponents
public float[] getColorComponents(float[] compArray)
Returns afloat
array containing only the color components of theColor
, in theColorSpace
of theColor
. IfcompArray
isnull
, an array with length equal to the number of components in the associatedColorSpace
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:
compArray
- an array that this method fills with the color components of thisColor
in itsColorSpace
and returns- Returns:
- the color components in a
float
array.
-
getComponents
public float[] getComponents(ColorSpace cspace, float[] compArray)
Returns afloat
array containing the color and alpha components of theColor
, in theColorSpace
specified by thecspace
parameter. IfcompArray
isnull
, an array with length equal to the number of components incspace
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:
cspace
- a specifiedColorSpace
compArray
- an array that this method fills with the color and alpha components of thisColor
in the specifiedColorSpace
and returns- Returns:
- the color and alpha components in a
float
array.
-
getColorComponents
public float[] getColorComponents(ColorSpace cspace, float[] compArray)
Returns afloat
array containing only the color components of theColor
in theColorSpace
specified by thecspace
parameter. IfcompArray
isnull
, an array with length equal to the number of components incspace
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:
cspace
- a specifiedColorSpace
compArray
- an array that this method fills with the color components of thisColor
in the specifiedColorSpace
- Returns:
- the color components in a
float
array.
-
getColorSpace
public ColorSpace getColorSpace()
Returns theColorSpace
of thisColor
.- Returns:
- this
Color
object'sColorSpace
.
-
createContext
public PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
Creates and returns aPaintContext
used to generate a solid color field pattern. See thespecification
of the method in thePaint
interface for information on null parameter handling.- Specified by:
createContext
in interfacePaint
- Parameters:
cm
- the preferredColorModel
which represents the most convenient format for the caller to receive the pixel data, ornull
if there is no preference.r
- the device space bounding box of the graphics primitive being rendered.r2d
- the user space bounding box of the graphics primitive being rendered.xform
- theAffineTransform
from user space into device space.hints
- the set of hints that the context object can use to choose between rendering alternatives.- Returns:
- the
PaintContext
for generating color patterns. - See Also:
Paint
,PaintContext
,ColorModel
,Rectangle
,Rectangle2D
,AffineTransform
,RenderingHints
-
getTransparency
public int getTransparency()
Returns the transparency mode for thisColor
. This is required to implement thePaint
interface.- Specified by:
getTransparency
in interfaceTransparency
- Returns:
- this
Color
object's transparency mode. - See Also:
Paint
,Transparency
,createContext(java.awt.image.ColorModel, java.awt.Rectangle, java.awt.geom.Rectangle2D, java.awt.geom.AffineTransform, java.awt.RenderingHints)
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
20/12/2024 07:10:27 Cette version de la page est en cache (à la date du 20/12/2024 07:10:27) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/awt/color
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.