Constructor: |
public Color(int rgb) {
value = 0xff000000 | 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. |
public Color(int rgba,
boolean hasalpha) {
if (hasalpha) {
value = rgba;
} else {
value = 0xff000000 | rgba;
}
}
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:
rgba - the combined RGBA components
hasalpha - true if the alpha bits are valid;
false otherwise
Also see:
- java.awt.image.ColorModel#getRGBdefault
- getRed
- getGreen
- getBlue
- getAlpha
- getRGB
|
public Color(int r,
int g,
int b) {
this(r, g, b, 255);
}
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 component
g - the green component
b - the blue component
Throws:
IllegalArgumentException - if r , g
or b are outside of the range
0 to 255, inclusive
Also see:
- getRed
- getGreen
- getBlue
- getRGB
|
public Color(float r,
float g,
float b) {
this( (int) (r*255+0.5), (int) (g*255+0.5), (int) (b*255+0.5));
testColorValueRange(r,g,b,1.0f);
frgbvalue = new float[3];
frgbvalue[0] = r;
frgbvalue[1] = g;
frgbvalue[2] = b;
falpha = 1.0f;
fvalue = frgbvalue;
}
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 component
g - the green component
b - the blue component
Throws:
IllegalArgumentException - if r , g
or b are outside of the range
0.0 to 1.0, inclusive
Also see:
- getRed
- getGreen
- getBlue
- getRGB
|
public Color(ColorSpace cspace,
float[] components,
float alpha) {
boolean rangeError = false;
String badComponentString = "";
int n = cspace.getNumComponents();
fvalue = new float[n];
for (int i = 0; i < n; i++) {
if (components[i] < 0.0 || components[i] > 1.0) {
rangeError = true;
badComponentString = badComponentString + "Component " + i
+ " ";
} else {
fvalue[i] = components[i];
}
}
if (alpha < 0.0 || alpha > 1.0) {
rangeError = true;
badComponentString = badComponentString + "Alpha";
} else {
falpha = alpha;
}
if (rangeError) {
throw new IllegalArgumentException(
"Color parameter outside of expected range: " +
badComponentString);
}
frgbvalue = cspace.toRGB(fvalue);
cs = cspace;
value = ((((int)(falpha*255)) & 0xFF) < < 24) |
((((int)(frgbvalue[0]*255)) & 0xFF) < < 16) |
((((int)(frgbvalue[1]*255)) & 0xFF) < < 8) |
((((int)(frgbvalue[2]*255)) & 0xFF) < < 0);
}
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:
cspace - the ColorSpace to be used to
interpret the components
components - an arbitrary number of color components
that is compatible with the ColorSpace
alpha - alpha value
Throws:
IllegalArgumentException - if any of the values in the
components array or alpha is
outside of the range 0.0 to 1.0
Also see:
- getComponents
- getColorComponents
|
public Color(int r,
int g,
int b,
int a) {
value = ((a & 0xFF) < < 24) |
((r & 0xFF) < < 16) |
((g & 0xFF) < < 8) |
((b & 0xFF) < < 0);
testColorValueRange(r,g,b,a);
}
Creates an sRGB color with the specified red, green, blue, and alpha
values in the range (0 - 255). Parameters:
r - the red component
g - the green component
b - the blue component
a - the alpha component
Throws:
IllegalArgumentException - if r , g ,
b or a are outside of the range
0 to 255, inclusive
Also see:
- getRed
- getGreen
- getBlue
- getAlpha
- getRGB
|
public Color(float r,
float g,
float b,
float a) {
this((int)(r*255+0.5), (int)(g*255+0.5), (int)(b*255+0.5), (int)(a*255+0.5));
frgbvalue = new float[3];
frgbvalue[0] = r;
frgbvalue[1] = g;
frgbvalue[2] = b;
falpha = a;
fvalue = frgbvalue;
}
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 component
g - the green component
b - the blue component
a - the alpha component
Throws:
IllegalArgumentException - if r , g
b or a are outside of the range
0.0 to 1.0, inclusive
Also see:
- getRed
- getGreen
- getBlue
- getAlpha
- getRGB
|
Method from java.awt.Color Detail: |
public static int HSBtoRGB(float hue,
float saturation,
float brightness) {
int r = 0, g = 0, b = 0;
if (saturation == 0) {
r = g = b = (int) (brightness * 255.0f + 0.5f);
} else {
float h = (hue - (float)Math.floor(hue)) * 6.0f;
float f = h - (float)java.lang.Math.floor(h);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - saturation * f);
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int) h) {
case 0:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (t * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 1:
r = (int) (q * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 2:
r = (int) (p * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (t * 255.0f + 0.5f);
break;
case 3:
r = (int) (p * 255.0f + 0.5f);
g = (int) (q * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 4:
r = (int) (t * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 5:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (q * 255.0f + 0.5f);
break;
}
}
return 0xff000000 | (r < < 16) | (g < < 8) | (b < < 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. |
public static float[] RGBtoHSB(int r,
int g,
int b,
float[] hsbvals) {
float hue, saturation, brightness;
if (hsbvals == null) {
hsbvals = new float[3];
}
int cmax = (r > g) ? r : g;
if (b > cmax) cmax = b;
int cmin = (r < g) ? r : g;
if (b < cmin) cmin = b;
brightness = ((float) cmax) / 255.0f;
if (cmax != 0)
saturation = ((float) (cmax - cmin)) / ((float) cmax);
else
saturation = 0;
if (saturation == 0)
hue = 0;
else {
float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
if (r == cmax)
hue = bluec - greenc;
else if (g == cmax)
hue = 2.0f + redc - bluec;
else
hue = 4.0f + greenc - redc;
hue = hue / 6.0f;
if (hue < 0)
hue = hue + 1.0f;
}
hsbvals[0] = hue;
hsbvals[1] = saturation;
hsbvals[2] = brightness;
return 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 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. |
public Color brighter() {
int r = getRed();
int g = getGreen();
int b = getBlue();
int alpha = getAlpha();
/* From 2D group:
* 1. black.brighter() should return grey
* 2. applying brighter to blue will always return blue, brighter
* 3. non pure color (non zero rgb) will eventually return white
*/
int i = (int)(1.0/(1.0-FACTOR));
if ( r == 0 && g == 0 && b == 0) {
return new Color(i, i, i, alpha);
}
if ( r > 0 && r < i ) r = i;
if ( g > 0 && g < i ) g = i;
if ( b > 0 && b < i ) b = i;
return new Color(Math.min((int)(r/FACTOR), 255),
Math.min((int)(g/FACTOR), 255),
Math.min((int)(b/FACTOR), 255),
alpha);
}
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 .
The {@code alpha} value is preserved.
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. |
public synchronized PaintContext createContext(ColorModel cm,
Rectangle r,
Rectangle2D r2d,
AffineTransform xform,
RenderingHints hints) {
return new ColorPaintContext(getRGB(), cm);
}
Creates and returns a PaintContext used to
generate a solid color field pattern.
See the specification of the
method in the Paint interface for information
on null parameter handling. |
public Color darker() {
return new Color(Math.max((int)(getRed() *FACTOR), 0),
Math.max((int)(getGreen()*FACTOR), 0),
Math.max((int)(getBlue() *FACTOR), 0),
getAlpha());
}
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 .
The {@code alpha} value is preserved.
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. |
public static Color decode(String nm) throws NumberFormatException {
Integer intval = Integer.decode(nm);
int i = intval.intValue();
return new Color((i > > 16) & 0xFF, (i > > 8) & 0xFF, i & 0xFF);
}
Converts a String to an integer and returns the
specified opaque Color . This method handles string
formats that are used to represent octal and hexadecimal numbers. |
public boolean equals(Object obj) {
return obj instanceof Color && ((Color)obj).getRGB() == this.getRGB();
}
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. |
public int getAlpha() {
return (getRGB() > > 24) & 0xff;
}
Returns the alpha component in the range 0-255. |
public int getBlue() {
return (getRGB() > > 0) & 0xFF;
}
Returns the blue component in the range 0-255 in the default sRGB
space. |
public static Color getColor(String nm) {
return getColor(nm, null);
}
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. |
public static Color getColor(String nm,
Color v) {
Integer intval = Integer.getInteger(nm);
if (intval == null) {
return v;
}
int i = intval.intValue();
return new Color((i > > 16) & 0xFF, (i > > 8) & 0xFF, i & 0xFF);
}
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. |
public static Color getColor(String nm,
int v) {
Integer intval = Integer.getInteger(nm);
int i = (intval != null) ? intval.intValue() : v;
return new Color((i > > 16) & 0xFF, (i > > 8) & 0xFF, (i > > 0) & 0xFF);
}
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. |
public float[] getColorComponents(float[] compArray) {
if (fvalue == null)
return getRGBColorComponents(compArray);
float[] f;
int n = fvalue.length;
if (compArray == null) {
f = new float[n];
} else {
f = compArray;
}
for (int i = 0; i < n; i++) {
f[i] = fvalue[i];
}
return f;
}
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. |
public float[] getColorComponents(ColorSpace cspace,
float[] compArray) {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
float f[];
if (fvalue == null) {
f = new float[3];
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
} else {
f = fvalue;
}
float tmp[] = cs.toCIEXYZ(f);
float tmpout[] = cspace.fromCIEXYZ(tmp);
if (compArray == null) {
return tmpout;
}
for (int i = 0 ; i < tmpout.length ; i++) {
compArray[i] = tmpout[i];
}
return compArray;
}
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. |
public ColorSpace getColorSpace() {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
return cs;
}
Returns the ColorSpace of this Color . |
public float[] getComponents(float[] compArray) {
if (fvalue == null)
return getRGBComponents(compArray);
float[] f;
int n = fvalue.length;
if (compArray == null) {
f = new float[n + 1];
} else {
f = compArray;
}
for (int i = 0; i < n; i++) {
f[i] = fvalue[i];
}
f[n] = falpha;
return f;
}
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. |
public float[] getComponents(ColorSpace cspace,
float[] compArray) {
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
float f[];
if (fvalue == null) {
f = new float[3];
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
} else {
f = fvalue;
}
float tmp[] = cs.toCIEXYZ(f);
float tmpout[] = cspace.fromCIEXYZ(tmp);
if (compArray == null) {
compArray = new float[tmpout.length + 1];
}
for (int i = 0 ; i < tmpout.length ; i++) {
compArray[i] = tmpout[i];
}
if (fvalue == null) {
compArray[tmpout.length] = ((float)getAlpha())/255f;
} else {
compArray[tmpout.length] = falpha;
}
return compArray;
}
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. |
public int getGreen() {
return (getRGB() > > 8) & 0xFF;
}
Returns the green component in the range 0-255 in the default sRGB
space. |
public static Color getHSBColor(float h,
float s,
float b) {
return new Color(HSBtoRGB(h, s, b));
}
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. |
public int getRGB() {
return value;
}
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). |
public float[] getRGBColorComponents(float[] compArray) {
float[] f;
if (compArray == null) {
f = new float[3];
} else {
f = compArray;
}
if (frgbvalue == null) {
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
} else {
f[0] = frgbvalue[0];
f[1] = frgbvalue[1];
f[2] = frgbvalue[2];
}
return f;
}
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. |
public float[] getRGBComponents(float[] compArray) {
float[] f;
if (compArray == null) {
f = new float[4];
} else {
f = compArray;
}
if (frgbvalue == null) {
f[0] = ((float)getRed())/255f;
f[1] = ((float)getGreen())/255f;
f[2] = ((float)getBlue())/255f;
f[3] = ((float)getAlpha())/255f;
} else {
f[0] = frgbvalue[0];
f[1] = frgbvalue[1];
f[2] = frgbvalue[2];
f[3] = falpha;
}
return f;
}
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. |
public int getRed() {
return (getRGB() > > 16) & 0xFF;
}
Returns the red component in the range 0-255 in the default sRGB
space. |
public int getTransparency() {
int alpha = getAlpha();
if (alpha == 0xff) {
return Transparency.OPAQUE;
}
else if (alpha == 0) {
return Transparency.BITMASK;
}
else {
return Transparency.TRANSLUCENT;
}
}
Returns the transparency mode for this Color . This is
required to implement the Paint interface. |
public int hashCode() {
return value;
}
Computes the hash code for this Color . |
public String toString() {
return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]";
}
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 . |