Colorpublic class Color extends Object implements Serializable, PaintThe Color class defines colors in the default sRGB color space or in the
specified ColorSpace. Every Color contains alpha value. 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. |
Fields Summary |
---|
private static final long | serialVersionUIDThe Constant serialVersionUID. | public static final Color | whiteThe color white. | public static final Color | WHITEThe color white. | public static final Color | lightGrayThe color light gray. | public static final Color | LIGHT_GRAYThe color light gray. | public static final Color | grayThe color gray. | public static final Color | GRAYThe color gray. | public static final Color | darkGrayThe color dark gray. | public static final Color | DARK_GRAYThe color dark gray. | public static final Color | blackThe color black. | public static final Color | BLACKThe color black. | public static final Color | redThe color red. | public static final Color | REDThe color red. | public static final Color | pinkThe color pink. | public static final Color | PINKThe color pink. | public static final Color | orangeThe color orange. | public static final Color | ORANGEThe color orange. | public static final Color | yellowThe color yellow. | public static final Color | YELLOWThe color yellow. | public static final Color | greenThe color green. | public static final Color | GREENThe color green. | public static final Color | magentaThe color magenta. | public static final Color | MAGENTAThe color magenta. | public static final Color | cyanThe color cyan. | public static final Color | CYANThe color cyan. | public static final Color | blueThe color blue. | public static final Color | BLUEThe color blue. | int | valueinteger RGB value. | private float[] | frgbvalueFloat sRGB value. | private float[] | fvalueColor in an arbitrary color space with float components. If
null, other value should be used. | private float | falphaFloat alpha value. If frgbvalue is null, this is not valid data. | private ColorSpace | csThe color's color space if applicable. | private static final double | SCALE_FACTORThe Constant SCALE_FACTOR. | private static final int | MIN_SCALABLEThe Constant MIN_SCALABLE. | private transient PaintContext | currentPaintContextThe current paint context. |
Constructors Summary |
---|
public Color(ColorSpace cspace, float[] components, float alpha)Creates a color in the specified ColorSpace, the specified color
components and the specified alpha.
int nComps = cspace.getNumComponents();
float comp;
fvalue = new float[nComps];
for (int i = 0; i < nComps; i++) {
comp = components[i];
if (comp < 0.0f || comp > 1.0f) {
// awt.107=Color parameter outside of expected range: component
// {0}.
throw new IllegalArgumentException(Messages.getString("awt.107", i)); //$NON-NLS-1$
}
fvalue[i] = components[i];
}
if (alpha < 0.0f || alpha > 1.0f) {
// awt.108=Alpha value outside of expected range.
throw new IllegalArgumentException(Messages.getString("awt.108")); //$NON-NLS-1$
}
falpha = alpha;
cs = cspace;
frgbvalue = cs.toRGB(fvalue);
value = ((int)(frgbvalue[2] * 255 + 0.5)) | (((int)(frgbvalue[1] * 255 + 0.5)) << 8)
| (((int)(frgbvalue[0] * 255 + 0.5)) << 16) | (((int)(falpha * 255 + 0.5)) << 24);
| public Color(int rgba, boolean hasAlpha)Instantiates a new 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, the alpha has default value
- 255.
if (!hasAlpha) {
value = rgba | 0xFF000000;
} else {
value = rgba;
}
| public Color(int r, int g, int b, int a)Instantiates a new color with the specified red, green, blue and alpha
components.
if ((r & 0xFF) != r || (g & 0xFF) != g || (b & 0xFF) != b || (a & 0xFF) != a) {
// awt.109=Color parameter outside of expected range.
throw new IllegalArgumentException(Messages.getString("awt.109")); //$NON-NLS-1$
}
value = b | (g << 8) | (r << 16) | (a << 24);
| public Color(int r, int g, int b)Instantiates a new opaque sRGB color with the specified red, green, and
blue values. The Alpha component is set to the default - 1.0.
if ((r & 0xFF) != r || (g & 0xFF) != g || (b & 0xFF) != b) {
// awt.109=Color parameter outside of expected range.
throw new IllegalArgumentException(Messages.getString("awt.109")); //$NON-NLS-1$
}
// 0xFF for alpha channel
value = b | (g << 8) | (r << 16) | 0xFF000000;
| public Color(int rgb)Instantiates a new sRGB color with the specified 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. Alpha has default value - 255.
value = rgb | 0xFF000000;
| public Color(float r, float g, float b, float a)Instantiates a new color with the specified red, green, blue and alpha
components.
this((int)(r * 255 + 0.5), (int)(g * 255 + 0.5), (int)(b * 255 + 0.5), (int)(a * 255 + 0.5));
falpha = a;
fvalue = new float[3];
fvalue[0] = r;
fvalue[1] = g;
fvalue[2] = b;
frgbvalue = fvalue;
| public Color(float r, float g, float b)Instantiates a new color with the specified red, green, and blue
components and default alpha value - 1.0.
this(r, g, b, 1.0f);
|
Methods Summary |
---|
public static int | HSBtoRGB(float hue, float saturation, float brightness)Converts the Color specified by the HSB model to an equivalent color in
the default RGB model.
float fr, fg, fb;
if (saturation == 0) {
fr = fg = fb = brightness;
} else {
float H = (hue - (float)Math.floor(hue)) * 6;
int I = (int)Math.floor(H);
float F = H - I;
float M = brightness * (1 - saturation);
float N = brightness * (1 - saturation * F);
float K = brightness * (1 - saturation * (1 - F));
switch (I) {
case 0:
fr = brightness;
fg = K;
fb = M;
break;
case 1:
fr = N;
fg = brightness;
fb = M;
break;
case 2:
fr = M;
fg = brightness;
fb = K;
break;
case 3:
fr = M;
fg = N;
fb = brightness;
break;
case 4:
fr = K;
fg = M;
fb = brightness;
break;
case 5:
fr = brightness;
fg = M;
fb = N;
break;
default:
fr = fb = fg = 0; // impossible, to supress compiler error
}
}
int r = (int)(fr * 255. + 0.5);
int g = (int)(fg * 255. + 0.5);
int b = (int)(fb * 255. + 0.5);
return (r << 16) | (g << 8) | b | 0xFF000000;
| public static float[] | RGBtoHSB(int r, int g, int b, float[] hsbvals)Converts the Color specified by the RGB model to an equivalent color in
the HSB model.
if (hsbvals == null) {
hsbvals = new float[3];
}
int V = Math.max(b, Math.max(r, g));
int temp = Math.min(b, Math.min(r, g));
float H, S, B;
B = V / 255.f;
if (V == temp) {
H = S = 0;
} else {
S = (V - temp) / ((float)V);
float Cr = (V - r) / (float)(V - temp);
float Cg = (V - g) / (float)(V - temp);
float Cb = (V - b) / (float)(V - temp);
if (r == V) {
H = Cb - Cg;
} else if (g == V) {
H = 2 + Cr - Cb;
} else {
H = 4 + Cg - Cr;
}
H /= 6.f;
if (H < 0) {
H++;
}
}
hsbvals[0] = H;
hsbvals[1] = S;
hsbvals[2] = B;
return hsbvals;
| public java.awt.Color | brighter()Creates a new Color which is a brighter than this Color.
int r = getRed();
int b = getBlue();
int g = getGreen();
if (r == 0 && b == 0 && g == 0) {
return new Color(MIN_SCALABLE, MIN_SCALABLE, MIN_SCALABLE);
}
if (r < MIN_SCALABLE && r != 0) {
r = MIN_SCALABLE;
} else {
r = (int)(r / SCALE_FACTOR);
r = (r > 255) ? 255 : r;
}
if (b < MIN_SCALABLE && b != 0) {
b = MIN_SCALABLE;
} else {
b = (int)(b / SCALE_FACTOR);
b = (b > 255) ? 255 : b;
}
if (g < MIN_SCALABLE && g != 0) {
g = MIN_SCALABLE;
} else {
g = (int)(g / SCALE_FACTOR);
g = (g > 255) ? 255 : g;
}
return new Color(r, g, b);
| public java.awt.PaintContext | createContext(java.awt.image.ColorModel cm, java.awt.Rectangle r, java.awt.geom.Rectangle2D r2d, java.awt.geom.AffineTransform xform, java.awt.RenderingHints rhs)
if (currentPaintContext != null) {
return currentPaintContext;
}
currentPaintContext = new Color.ColorPaintContext(value);
return currentPaintContext;
| public java.awt.Color | darker()Creates a new Color which is a darker than this Color according to a
fixed scale factor.
return new Color((int)(getRed() * SCALE_FACTOR), (int)(getGreen() * SCALE_FACTOR),
(int)(getBlue() * SCALE_FACTOR));
| public static java.awt.Color | decode(java.lang.String nm)Decodes a String to an integer and returns the specified opaque Color.
Integer integer = Integer.decode(nm);
return new Color(integer.intValue());
| public boolean | equals(java.lang.Object obj)Compares the specified Object to the Color.
if (obj instanceof Color) {
return ((Color)obj).value == this.value;
}
return false;
| public int | getAlpha()Gets the alpha component of the Color in the range 0-255.
return (value >> 24) & 0xFF;
| public int | getBlue()Gets the blue component of the Color in the range 0-255.
return value & 0xFF;
| public static java.awt.Color | getColor(java.lang.String nm, java.awt.Color def)Gets the Color from the specified string, or returns the Color specified
by the second parameter.
Integer integer = Integer.getInteger(nm);
if (integer == null) {
return def;
}
return new Color(integer.intValue());
| public static java.awt.Color | getColor(java.lang.String nm, int def)Gets the Color from the specified string, or returns the Color converted
from the second parameter.
Integer integer = Integer.getInteger(nm);
if (integer == null) {
return new Color(def);
}
return new Color(integer.intValue());
| public static java.awt.Color | getColor(java.lang.String nm)Gets the Color from the specified String.
Integer integer = Integer.getInteger(nm);
if (integer == null) {
return null;
}
return new Color(integer.intValue());
| public float[] | getColorComponents(java.awt.color.ColorSpace colorSpace, float[] components)Returns a float array containing the color components of the Color in the
specified ColorSpace.
float[] cieXYZComponents = getColorSpace().toCIEXYZ(getColorComponents(null));
float[] csComponents = colorSpace.fromCIEXYZ(cieXYZComponents);
if (components == null) {
return csComponents;
}
for (int i = 0; i < csComponents.length; i++) {
components[i] = csComponents[i];
}
return components;
| public float[] | getColorComponents(float[] components)Returns a float array which contains the color components of the Color in
the ColorSpace of the Color.
if (fvalue == null) {
return getRGBColorComponents(components);
}
if (components == null) {
components = new float[fvalue.length];
}
for (int i = 0; i < fvalue.length; i++) {
components[i] = fvalue[i];
}
return components;
| public java.awt.color.ColorSpace | getColorSpace()Gets the ColorSpace of this Color.
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
return cs;
| public float[] | getComponents(java.awt.color.ColorSpace colorSpace, float[] components)Returns a float array containing the color and alpha components of the
Color in the specified ColorSpace.
int nComps = colorSpace.getNumComponents();
if (components == null) {
components = new float[nComps + 1];
}
getColorComponents(colorSpace, components);
if (frgbvalue != null) {
components[nComps] = falpha;
} else {
components[nComps] = getAlpha() / 255f;
}
return components;
| public float[] | getComponents(float[] components)Returns a float array which contains the color and alpha components of
the Color in the ColorSpace of the Color.
if (fvalue == null) {
return getRGBComponents(components);
}
int nColorComps = fvalue.length;
if (components == null) {
components = new float[nColorComps + 1];
}
getColorComponents(components);
components[nColorComps] = falpha;
return components;
| public int | getGreen()Gets the green component of the Color in the range 0-255.
return (value >> 8) & 0xFF;
| public static java.awt.Color | getHSBColor(float h, float s, float b)Gets a Color object using the specified values of the HSB color model.
return new Color(HSBtoRGB(h, s, b));
| public int | getRGB()Gets the RGB value that represents the color in the default sRGB
ColorModel.
return value;
| public float[] | getRGBColorComponents(float[] components)Returns a float array containing the color components of the Color in the
default sRGB color space.
if (components == null) {
components = new float[3];
}
if (frgbvalue != null) {
components[2] = frgbvalue[2];
components[1] = frgbvalue[1];
components[0] = frgbvalue[0];
} else {
components[2] = getBlue() / 255f;
components[1] = getGreen() / 255f;
components[0] = getRed() / 255f;
}
return components;
| public float[] | getRGBComponents(float[] components)Returns a float array containing the color and alpha components of the
Color in the default sRGB color space.
if (components == null) {
components = new float[4];
}
if (frgbvalue != null) {
components[3] = falpha;
} else {
components[3] = getAlpha() / 255f;
}
getRGBColorComponents(components);
return components;
| public int | getRed()Gets the red component of the Color in the range 0-255.
return (value >> 16) & 0xFF;
| public int | getTransparency()
switch (getAlpha()) {
case 0xff:
return Transparency.OPAQUE;
case 0:
return Transparency.BITMASK;
default:
return Transparency.TRANSLUCENT;
}
| public int | hashCode()Returns a hash code of this Color object.
return value;
| public java.lang.String | toString()Returns a string representation of the Color object.
/*
* The format of the string is based on 1.5 release behavior which can
* be revealed using the following code: Color c = new Color(1, 2, 3);
* System.out.println(c);
*/
return getClass().getName() + "[r=" + getRed() + //$NON-NLS-1$
",g=" + getGreen() + //$NON-NLS-1$
",b=" + getBlue() + //$NON-NLS-1$
"]"; //$NON-NLS-1$
|
|