KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Color


1 /*
2  * @(#)Color.java 1.75 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.io.*;
11 import java.lang.*;
12 import java.awt.image.ColorModel JavaDoc;
13 import java.awt.geom.AffineTransform JavaDoc;
14 import java.awt.geom.Rectangle2D JavaDoc;
15 import java.awt.color.ColorSpace JavaDoc;
16
17 /**
18  * The <code>Color</code> class is used to encapsulate colors in the default
19  * sRGB color space or colors in arbitrary color spaces identified by a
20  * {@link ColorSpace}. Every color has an implicit alpha value of 1.0 or
21  * an explicit one provided in the constructor. The alpha value
22  * defines the transparency of a color and can be represented by
23  * a float value in the range 0.0&nbsp;-&nbsp;1.0 or 0&nbsp;-&nbsp;255.
24  * An alpha value of 1.0 or 255 means that the color is completely
25  * opaque and an alpha value of 0 or 0.0 means that the color is
26  * completely transparent.
27  * When constructing a <code>Color</code> with an explicit alpha or
28  * getting the color/alpha components of a <code>Color</code>, the color
29  * components are never premultiplied by the alpha component.
30  * <p>
31  * The default color space for the Java 2D(tm) API is sRGB, a proposed
32  * standard RGB color space. For further information on sRGB,
33  * see <A HREF="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
34  * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html
35  * </A>.
36  * <p>
37  * @version 10 Feb 1997
38  * @author Sami Shaio
39  * @author Arthur van Hoff
40  * @see ColorSpace
41  * @see AlphaComposite
42  */

43 public class Color implements Paint JavaDoc, java.io.Serializable JavaDoc {
44     
45     /**
46      * The color white. In the default sRGB space.
47      */

48     public final static Color JavaDoc white = new Color JavaDoc(255, 255, 255);
49
50     /**
51      * The color white. In the default sRGB space.
52      */

53     public final static Color JavaDoc WHITE = white;
54
55     /**
56      * The color light gray. In the default sRGB space.
57      */

58     public final static Color JavaDoc lightGray = new Color JavaDoc(192, 192, 192);
59
60     /**
61      * The color light gray. In the default sRGB space.
62      */

63     public final static Color JavaDoc LIGHT_GRAY = lightGray;
64
65     /**
66      * The color gray. In the default sRGB space.
67      */

68     public final static Color JavaDoc gray = new Color JavaDoc(128, 128, 128);
69
70     /**
71      * The color gray. In the default sRGB space.
72      */

73     public final static Color JavaDoc GRAY = gray;
74
75     /**
76      * The color dark gray. In the default sRGB space.
77      */

78     public final static Color JavaDoc darkGray = new Color JavaDoc(64, 64, 64);
79
80     /**
81      * The color dark gray. In the default sRGB space.
82      */

83     public final static Color JavaDoc DARK_GRAY = darkGray;
84
85     /**
86      * The color black. In the default sRGB space.
87      */

88     public final static Color JavaDoc black = new Color JavaDoc(0, 0, 0);
89     
90     /**
91      * The color black. In the default sRGB space.
92      */

93     public final static Color JavaDoc BLACK = black;
94     
95     /**
96      * The color red. In the default sRGB space.
97      */

98     public final static Color JavaDoc red = new Color JavaDoc(255, 0, 0);
99
100     /**
101      * The color red. In the default sRGB space.
102      */

103     public final static Color JavaDoc RED = red;
104
105     /**
106      * The color pink. In the default sRGB space.
107      */

108     public final static Color JavaDoc pink = new Color JavaDoc(255, 175, 175);
109
110     /**
111      * The color pink. In the default sRGB space.
112      */

113     public final static Color JavaDoc PINK = pink;
114
115     /**
116      * The color orange. In the default sRGB space.
117      */

118     public final static Color JavaDoc orange = new Color JavaDoc(255, 200, 0);
119
120     /**
121      * The color orange. In the default sRGB space.
122      */

123     public final static Color JavaDoc ORANGE = orange;
124
125     /**
126      * The color yellow. In the default sRGB space.
127      */

128     public final static Color JavaDoc yellow = new Color JavaDoc(255, 255, 0);
129
130     /**
131      * The color yellow. In the default sRGB space.
132      */

133     public final static Color JavaDoc YELLOW = yellow;
134
135     /**
136      * The color green. In the default sRGB space.
137      */

138     public final static Color JavaDoc green = new Color JavaDoc(0, 255, 0);
139
140     /**
141      * The color green. In the default sRGB space.
142      */

143     public final static Color JavaDoc GREEN = green;
144
145     /**
146      * The color magenta. In the default sRGB space.
147      */

148     public final static Color JavaDoc magenta = new Color JavaDoc(255, 0, 255);
149
150     /**
151      * The color magenta. In the default sRGB space.
152      */

153     public final static Color JavaDoc MAGENTA = magenta;
154
155     /**
156      * The color cyan. In the default sRGB space.
157      */

158     public final static Color JavaDoc cyan = new Color JavaDoc(0, 255, 255);
159
160     /**
161      * The color cyan. In the default sRGB space.
162      */

163     public final static Color JavaDoc CYAN = cyan;
164
165     /**
166      * The color blue. In the default sRGB space.
167      */

168     public final static Color JavaDoc blue = new Color JavaDoc(0, 0, 255);
169
170     /**
171      * The color blue. In the default sRGB space.
172      */

173     public final static Color JavaDoc BLUE = blue;
174
175     /**
176      * Private data.
177      */

178     transient private long pData;
179
180     /**
181      * The color value.
182      * @serial
183      * @see #getRGB
184      */

185     int value;
186
187     /**
188      * The color value in the default sRGB <code>ColorSpace</code> as
189      * <code>float</code> components (no alpha).
190      * If <code>null</code> after object construction, this must be an
191      * sRGB color constructed with 8-bit precision, so compute from the
192      * <code>int</code> color value.
193      * @serial
194      * @see #getRGBColorComponents
195      * @see #getRGBComponents
196      */

197     private float frgbvalue[] = null;
198
199     /**
200      * The color value in the native <code>ColorSpace</code> as
201      * <code>float</code> components (no alpha).
202      * If <code>null</code> after object construction, this must be an
203      * sRGB color constructed with 8-bit precision, so compute from the
204      * <code>int</code> color value.
205      * @serial
206      * @see #getRGBColorComponents
207      * @see #getRGBComponents
208      */

209     private float fvalue[] = null;
210
211     /**
212      * The alpha value as a <code>float</code> component.
213      * If <code>frgbvalue</code> is <code>null</code>, this is not valid
214      * data, so compute from the <code>int</code> color value.
215      * @serial
216      * @see #getRGBComponents
217      * @see #getComponents
218      */

219     private float falpha = 0.0f;
220
221     /**
222      * The <code>ColorSpace</code>. If <code>null</code>, then it's
223      * default is sRGB.
224      * @serial
225      * @see #getColor
226      * @see #getColorSpace
227      * @see #getColorComponents
228      */

229     private ColorSpace JavaDoc cs = null;
230
231     /*
232      * JDK 1.1 serialVersionUID
233      */

234      private static final long serialVersionUID = 118526816881161077L;
235
236     /**
237      * Initialize JNI field and method IDs
238      */

239     private static native void initIDs();
240
241     static {
242         /** 4112352 - Calling getDefaultToolkit()
243          ** here can cause this class to be accessed before it is fully
244          ** initialized. DON'T DO IT!!!
245          **
246          ** Toolkit.getDefaultToolkit();
247          **/

248
249         /* ensure that the necessary native libraries are loaded */
250     Toolkit.loadLibraries();
251         if (!GraphicsEnvironment.isHeadless()) {
252             initIDs();
253         }
254     }
255
256     /**
257      * Checks the color integer components supplied for validity.
258      * Throws an {@link IllegalArgumentException} if the value is out of
259      * range.
260      * @param r the Red component
261      * @param g the Green component
262      * @param b the Blue component
263      **/

264     private static void testColorValueRange(int r, int g, int b, int a) {
265         boolean rangeError = false;
266     String JavaDoc badComponentString = "";
267     
268     if ( a < 0 || a > 255) {
269         rangeError = true;
270         badComponentString = badComponentString + " Alpha";
271     }
272         if ( r < 0 || r > 255) {
273         rangeError = true;
274         badComponentString = badComponentString + " Red";
275     }
276     if ( g < 0 || g > 255) {
277         rangeError = true;
278         badComponentString = badComponentString + " Green";
279     }
280     if ( b < 0 || b > 255) {
281         rangeError = true;
282         badComponentString = badComponentString + " Blue";
283     }
284     if ( rangeError == true ) {
285     throw new IllegalArgumentException JavaDoc("Color parameter outside of expected range:"
286                        + badComponentString);
287     }
288     }
289
290     /**
291      * Checks the color <code>float</code> components supplied for
292      * validity.
293      * Throws an <code>IllegalArgumentException</code> if the value is out
294      * of range.
295      * @param r the Red component
296      * @param g the Green component
297      * @param b the Blue component
298      **/

299     private static void testColorValueRange(float r, float g, float b, float a) {
300         boolean rangeError = false;
301     String JavaDoc badComponentString = "";
302     if ( a < 0.0 || a > 1.0) {
303         rangeError = true;
304         badComponentString = badComponentString + " Alpha";
305     }
306     if ( r < 0.0 || r > 1.0) {
307         rangeError = true;
308         badComponentString = badComponentString + " Red";
309     }
310     if ( g < 0.0 || g > 1.0) {
311         rangeError = true;
312         badComponentString = badComponentString + " Green";
313     }
314     if ( b < 0.0 || b > 1.0) {
315         rangeError = true;
316         badComponentString = badComponentString + " Blue";
317     }
318     if ( rangeError == true ) {
319     throw new IllegalArgumentException JavaDoc("Color parameter outside of expected range:"
320                        + badComponentString);
321     }
322     }
323
324     /**
325      * Creates an opaque sRGB color with the specified red, green,
326      * and blue values in the range (0 - 255).
327      * The actual color used in rendering depends
328      * on finding the best match given the color space
329      * available for a given output device.
330      * Alpha is defaulted to 255.
331      *
332      * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
333      * or <code>b</code> are outside of the range
334      * 0 to 255, inclusive
335      * @param r the red component
336      * @param g the green component
337      * @param b the blue component
338      * @see #getRed
339      * @see #getGreen
340      * @see #getBlue
341      * @see #getRGB
342      */

343     public Color(int r, int g, int b) {
344         this(r, g, b, 255);
345     }
346
347     /**
348      * Creates an sRGB color with the specified red, green, blue, and alpha
349      * values in the range (0 - 255).
350      *
351      * @throws IllegalArgumentException if <code>r</code>, <code>g</code>,
352      * <code>b</code> or <code>a</code> are outside of the range
353      * 0 to 255, inclusive
354      * @param r the red component
355      * @param g the green component
356      * @param b the blue component
357      * @param a the alpha component
358      * @see #getRed
359      * @see #getGreen
360      * @see #getBlue
361      * @see #getAlpha
362      * @see #getRGB
363      */

364     public Color(int r, int g, int b, int a) {
365         value = ((a & 0xFF) << 24) |
366                 ((r & 0xFF) << 16) |
367                 ((g & 0xFF) << 8) |
368                 ((b & 0xFF) << 0);
369     testColorValueRange(r,g,b,a);
370     }
371
372     /**
373      * Creates an opaque sRGB color with the specified combined RGB value
374      * consisting of the red component in bits 16-23, the green component
375      * in bits 8-15, and the blue component in bits 0-7. The actual color
376      * used in rendering depends on finding the best match given the
377      * color space available for a particular output device. Alpha is
378      * defaulted to 255.
379      *
380      * @param rgb the combined RGB components
381      * @see java.awt.image.ColorModel#getRGBdefault
382      * @see #getRed
383      * @see #getGreen
384      * @see #getBlue
385      * @see #getRGB
386      */

387     public Color(int rgb) {
388         value = 0xff000000 | rgb;
389     }
390
391     /**
392      * Creates an sRGB color with the specified combined RGBA value consisting
393      * of the alpha component in bits 24-31, the red component in bits 16-23,
394      * the green component in bits 8-15, and the blue component in bits 0-7.
395      * If the <code>hasalpha</code> argument is <code>false</code>, alpha
396      * is defaulted to 255.
397      *
398      * @param rgba the combined RGBA components
399      * @param hasalpha <code>true</code> if the alpha bits are valid;
400      * <code>false</code> otherwise
401      * @see java.awt.image.ColorModel#getRGBdefault
402      * @see #getRed
403      * @see #getGreen
404      * @see #getBlue
405      * @see #getAlpha
406      * @see #getRGB
407      */

408     public Color(int rgba, boolean hasalpha) {
409         if (hasalpha) {
410             value = rgba;
411         } else {
412             value = 0xff000000 | rgba;
413         }
414     }
415
416     /**
417      * Creates an opaque sRGB color with the specified red, green, and blue
418      * values in the range (0.0 - 1.0). Alpha is defaulted to 1.0. The
419      * actual color used in rendering depends on finding the best
420      * match given the color space available for a particular output
421      * device.
422      *
423      * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
424      * or <code>b</code> are outside of the range
425      * 0.0 to 1.0, inclusive
426      * @param r the red component
427      * @param g the green component
428      * @param b the blue component
429      * @see #getRed
430      * @see #getGreen
431      * @see #getBlue
432      * @see #getRGB
433      */

434     public Color(float r, float g, float b) {
435         this( (int) (r*255+0.5), (int) (g*255+0.5), (int) (b*255+0.5));
436         testColorValueRange(r,g,b,1.0f);
437         frgbvalue = new float[3];
438         frgbvalue[0] = r;
439         frgbvalue[1] = g;
440         frgbvalue[2] = b;
441         falpha = 1.0f;
442         fvalue = frgbvalue;
443     }
444
445     /**
446      * Creates an sRGB color with the specified red, green, blue, and
447      * alpha values in the range (0.0 - 1.0). The actual color
448      * used in rendering depends on finding the best match given the
449      * color space available for a particular output device.
450      * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
451      * <code>b</code> or <code>a</code> are outside of the range
452      * 0.0 to 1.0, inclusive
453      * @param r the red component
454      * @param g the green component
455      * @param b the blue component
456      * @param a the alpha component
457      * @see #getRed
458      * @see #getGreen
459      * @see #getBlue
460      * @see #getAlpha
461      * @see #getRGB
462      */

463     public Color(float r, float g, float b, float a) {
464         this((int)(r*255+0.5), (int)(g*255+0.5), (int)(b*255+0.5), (int)(a*255+0.5));
465         frgbvalue = new float[3];
466         frgbvalue[0] = r;
467         frgbvalue[1] = g;
468         frgbvalue[2] = b;
469         falpha = a;
470         fvalue = frgbvalue;
471     }
472
473     /**
474      * Creates a color in the specified <code>ColorSpace</code>
475      * with the color components specified in the <code>float</code>
476      * array and the specified alpha. The number of components is
477      * determined by the type of the <code>ColorSpace</code>. For
478      * example, RGB requires 3 components, but CMYK requires 4
479      * components.
480      * @param cspace the <code>ColorSpace</code> to be used to
481      * interpret the components
482      * @param components an arbitrary number of color components
483      * that is compatible with the
484      * @param alpha alpha value
485      * @throws IllegalArgumentException if any of the values in the
486      * <code>components</code> array or <code>alpha</code> is
487      * outside of the range 0.0 to 1.0
488      * @see #getComponents
489      * @see #getColorComponents
490      */

491     public Color(ColorSpace JavaDoc cspace, float components[], float alpha) {
492         boolean rangeError = false;
493         String JavaDoc badComponentString = "";
494         int n = cspace.getNumComponents();
495         fvalue = new float[n];
496         for (int i = 0; i < n; i++) {
497             if (components[i] < 0.0 || components[i] > 1.0) {
498                 rangeError = true;
499                 badComponentString = badComponentString + "Component " + i
500                                      + " ";
501             } else {
502                 fvalue[i] = components[i];
503             }
504         }
505         if (alpha < 0.0 || alpha > 1.0) {
506             rangeError = true;
507             badComponentString = badComponentString + "Alpha";
508         } else {
509             falpha = alpha;
510         }
511         if (rangeError) {
512             throw new IllegalArgumentException JavaDoc(
513                 "Color parameter outside of expected range: " +
514                 badComponentString);
515         }
516         frgbvalue = cspace.toRGB(fvalue);
517     cs = cspace;
518         value = ((((int)(falpha*255)) & 0xFF) << 24) |
519                 ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
520                 ((((int)(frgbvalue[1]*255)) & 0xFF) << 8) |
521                 ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
522     }
523
524     /**
525      * Returns the red component in the range 0-255 in the default sRGB
526      * space.
527      * @return the red component.
528      * @see #getRGB
529      */

530     public int getRed() {
531     return (getRGB() >> 16) & 0xFF;
532     }
533
534     /**
535      * Returns the green component in the range 0-255 in the default sRGB
536      * space.
537      * @return the green component.
538      * @see #getRGB
539      */

540     public int getGreen() {
541     return (getRGB() >> 8) & 0xFF;
542     }
543
544     /**
545      * Returns the blue component in the range 0-255 in the default sRGB
546      * space.
547      * @return the blue component.
548      * @see #getRGB
549      */

550     public int getBlue() {
551     return (getRGB() >> 0) & 0xFF;
552     }
553
554     /**
555      * Returns the alpha component in the range 0-255.
556      * @return the alpha component.
557      * @see #getRGB
558      */

559     public int getAlpha() {
560         return (getRGB() >> 24) & 0xff;
561     }
562
563     /**
564      * Returns the RGB value representing the color in the default sRGB
565      * {@link ColorModel}.
566      * (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are
567      * blue).
568      * @return the RGB value of the color in the default sRGB
569      * <code>ColorModel</code>.
570      * @see java.awt.image.ColorModel#getRGBdefault
571      * @see #getRed
572      * @see #getGreen
573      * @see #getBlue
574      * @since JDK1.0
575      */

576     public int getRGB() {
577     return value;
578     }
579
580     private static final double FACTOR = 0.7;
581
582     /**
583      * Creates a new <code>Color</code> that is a brighter version of this
584      * <code>Color</code>.
585      * <p>
586      * This method applies an arbitrary scale factor to each of the three RGB
587      * components of this <code>Color</code> to create a brighter version
588      * of this <code>Color</code>. Although <code>brighter</code> and
589      * <code>darker</code> are inverse operations, the results of a
590      * series of invocations of these two methods might be inconsistent
591      * because of rounding errors.
592      * @return a new <code>Color</code> object that is
593      * a brighter version of this <code>Color</code>.
594      * @see java.awt.Color#darker
595      * @since JDK1.0
596      */

597     public Color JavaDoc brighter() {
598         int r = getRed();
599         int g = getGreen();
600         int b = getBlue();
601
602         /* From 2D group:
603          * 1. black.brighter() should return grey
604          * 2. applying brighter to blue will always return blue, brighter
605          * 3. non pure color (non zero rgb) will eventually return white
606          */

607         int i = (int)(1.0/(1.0-FACTOR));
608         if ( r == 0 && g == 0 && b == 0) {
609            return new Color JavaDoc(i, i, i);
610         }
611         if ( r > 0 && r < i ) r = i;
612         if ( g > 0 && g < i ) g = i;
613         if ( b > 0 && b < i ) b = i;
614
615         return new Color JavaDoc(Math.min((int)(r/FACTOR), 255),
616                          Math.min((int)(g/FACTOR), 255),
617                          Math.min((int)(b/FACTOR), 255));
618     }
619
620     /**
621      * Creates a new <code>Color</code> that is a darker version of this
622      * <code>Color</code>.
623      * <p>
624      * This method applies an arbitrary scale factor to each of the three RGB
625      * components of this <code>Color</code> to create a darker version of
626      * this <code>Color</code>. Although <code>brighter</code> and
627      * <code>darker</code> are inverse operations, the results of a series
628      * of invocations of these two methods might be inconsistent because
629      * of rounding errors.
630      * @return a new <code>Color</code> object that is
631      * a darker version of this <code>Color</code>.
632      * @see java.awt.Color#brighter
633      * @since JDK1.0
634      */

635     public Color JavaDoc darker() {
636     return new Color JavaDoc(Math.max((int)(getRed() *FACTOR), 0),
637              Math.max((int)(getGreen()*FACTOR), 0),
638              Math.max((int)(getBlue() *FACTOR), 0));
639     }
640
641     /**
642      * Computes the hash code for this <code>Color</code>.
643      * @return a hash code value for this object.
644      * @since JDK1.0
645      */

646     public int hashCode() {
647     return value;
648     }
649
650     /**
651      * Determines whether another object is equal to this
652      * <code>Color</code>.
653      * <p>
654      * The result is <code>true</code> if and only if the argument is not
655      * <code>null</code> and is a <code>Color</code> object that has the same
656      * red, green, blue, and alpha values as this object.
657      * @param obj the object to test for equality with this
658      * <code>Color</code>
659      * @return <code>true</code> if the objects are the same;
660      * <code>false</code> otherwise.
661      * @since JDK1.0
662      */

663     public boolean equals(Object JavaDoc obj) {
664         return obj instanceof Color JavaDoc && ((Color JavaDoc)obj).value == this.value;
665     }
666
667     /**
668      * Returns a string representation of this <code>Color</code>. This
669      * method is intended to be used only for debugging purposes. The
670      * content and format of the returned string might vary between
671      * implementations. The returned string might be empty but cannot
672      * be <code>null</code>.
673      *
674      * @return a string representation of this <code>Color</code>.
675      */

676     public String JavaDoc toString() {
677         return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]";
678     }
679
680     /**
681      * Converts a <code>String</code> to an integer and returns the
682      * specified opaque <code>Color</code>. This method handles string
683      * formats that are used to represent octal and hexidecimal numbers.
684      * @param nm a <code>String</code> that represents
685      * an opaque color as a 24-bit integer
686      * @return the new <code>Color</code> object.
687      * @see java.lang.Integer#decode
688      * @exception NumberFormatException if the specified string cannot
689      * be interpreted as a decimal,
690      * octal, or hexidecimal integer.
691      * @since JDK1.1
692      */

693     public static Color JavaDoc decode(String JavaDoc nm) throws NumberFormatException JavaDoc {
694     Integer JavaDoc intval = Integer.decode(nm);
695     int i = intval.intValue();
696     return new Color JavaDoc((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
697     }
698
699     /**
700      * Finds a color in the system properties.
701      * <p>
702      * The argument is treated as the name of a system property to
703      * be obtained. The string value of this property is then interpreted
704      * as an integer which is then converted to a <code>Color</code>
705      * object.
706      * <p>
707      * If the specified property is not found or could not be parsed as
708      * an integer then <code>null</code> is returned.
709      * @param nm the name of the color property
710      * @return the <code>Color</code> converted from the system
711      * property.
712      * @see java.lang.System#getProperty(java.lang.String)
713      * @see java.lang.Integer#getInteger(java.lang.String)
714      * @see java.awt.Color#Color(int)
715      * @since JDK1.0
716      */

717     public static Color JavaDoc getColor(String JavaDoc nm) {
718     return getColor(nm, null);
719     }
720
721     /**
722      * Finds a color in the system properties.
723      * <p>
724      * The first argument is treated as the name of a system property to
725      * be obtained. The string value of this property is then interpreted
726      * as an integer which is then converted to a <code>Color</code>
727      * object.
728      * <p>
729      * If the specified property is not found or cannot be parsed as
730      * an integer then the <code>Color</code> specified by the second
731      * argument is returned instead.
732      * @param nm the name of the color property
733      * @param v the default <code>Color</code>
734      * @return the <code>Color</code> converted from the system
735      * property, or the specified <code>Color</code>.
736      * @see java.lang.System#getProperty(java.lang.String)
737      * @see java.lang.Integer#getInteger(java.lang.String)
738      * @see java.awt.Color#Color(int)
739      * @since JDK1.0
740      */

741     public static Color JavaDoc getColor(String JavaDoc nm, Color JavaDoc v) {
742     Integer JavaDoc intval = Integer.getInteger(nm);
743     if (intval == null) {
744         return v;
745     }
746     int i = intval.intValue();
747     return new Color JavaDoc((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
748     }
749
750     /**
751      * Finds a color in the system properties.
752      * <p>
753      * The first argument is treated as the name of a system property to
754      * be obtained. The string value of this property is then interpreted
755      * as an integer which is then converted to a <code>Color</code>
756      * object.
757      * <p>
758      * If the specified property is not found or could not be parsed as
759      * an integer then the integer value <code>v</code> is used instead,
760      * and is converted to a <code>Color</code> object.
761      * @param nm the name of the color property
762      * @param v the default color value, as an integer
763      * @return the <code>Color</code> converted from the system
764      * property or the <code>Color</code> converted from
765      * the specified integer.
766      * @see java.lang.System#getProperty(java.lang.String)
767      * @see java.lang.Integer#getInteger(java.lang.String)
768      * @see java.awt.Color#Color(int)
769      * @since JDK1.0
770      */

771     public static Color JavaDoc getColor(String JavaDoc nm, int v) {
772     Integer JavaDoc intval = Integer.getInteger(nm);
773     int i = (intval != null) ? intval.intValue() : v;
774     return new Color JavaDoc((i >> 16) & 0xFF, (i >> 8) & 0xFF, (i >> 0) & 0xFF);
775     }
776
777     /**
778      * Converts the components of a color, as specified by the HSB
779      * model, to an equivalent set of values for the default RGB model.
780      * <p>
781      * The <code>saturation</code> and <code>brightness</code> components
782      * should be floating-point values between zero and one
783      * (numbers in the range 0.0-1.0). The <code>hue</code> component
784      * can be any floating-point number. The floor of this number is
785      * subtracted from it to create a fraction between 0 and 1. This
786      * fractional number is then multiplied by 360 to produce the hue
787      * angle in the HSB color model.
788      * <p>
789      * The integer that is returned by <code>HSBtoRGB</code> encodes the
790      * value of a color in bits 0-23 of an integer value that is the same
791      * format used by the method {@link #getRGB() <code>getRGB</code>}.
792      * This integer can be supplied as an argument to the
793      * <code>Color</code> constructor that takes a single integer argument.
794      * @param hue the hue component of the color
795      * @param saturation the saturation of the color
796      * @param brightness the brightness of the color
797      * @return the RGB value of the color with the indicated hue,
798      * saturation, and brightness.
799      * @see java.awt.Color#getRGB()
800      * @see java.awt.Color#Color(int)
801      * @see java.awt.image.ColorModel#getRGBdefault()
802      * @since JDK1.0
803      */

804     public static int HSBtoRGB(float hue, float saturation, float brightness) {
805     int r = 0, g = 0, b = 0;
806         if (saturation == 0) {
807         r = g = b = (int) (brightness * 255.0f + 0.5f);
808     } else {
809         float h = (hue - (float)Math.floor(hue)) * 6.0f;
810         float f = h - (float)java.lang.Math.floor(h);
811         float p = brightness * (1.0f - saturation);
812         float q = brightness * (1.0f - saturation * f);
813         float t = brightness * (1.0f - (saturation * (1.0f - f)));
814         switch ((int) h) {
815         case 0:
816         r = (int) (brightness * 255.0f + 0.5f);
817         g = (int) (t * 255.0f + 0.5f);
818         b = (int) (p * 255.0f + 0.5f);
819         break;
820         case 1:
821         r = (int) (q * 255.0f + 0.5f);
822         g = (int) (brightness * 255.0f + 0.5f);
823         b = (int) (p * 255.0f + 0.5f);
824         break;
825         case 2:
826         r = (int) (p * 255.0f + 0.5f);
827         g = (int) (brightness * 255.0f + 0.5f);
828         b = (int) (t * 255.0f + 0.5f);
829         break;
830         case 3:
831         r = (int) (p * 255.0f + 0.5f);
832         g = (int) (q * 255.0f + 0.5f);
833         b = (int) (brightness * 255.0f + 0.5f);
834         break;
835         case 4:
836         r = (int) (t * 255.0f + 0.5f);
837         g = (int) (p * 255.0f + 0.5f);
838         b = (int) (brightness * 255.0f + 0.5f);
839         break;
840         case 5:
841         r = (int) (brightness * 255.0f + 0.5f);
842         g = (int) (p * 255.0f + 0.5f);
843         b = (int) (q * 255.0f + 0.5f);
844         break;
845         }
846     }
847     return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
848     }
849
850     /**
851      * Converts the components of a color, as specified by the default RGB
852      * model, to an equivalent set of values for hue, saturation, and
853      * brightness that are the three components of the HSB model.
854      * <p>
855      * If the <code>hsbvals</code> argument is <code>null</code>, then a
856      * new array is allocated to return the result. Otherwise, the method
857      * returns the array <code>hsbvals</code>, with the values put into
858      * that array.
859      * @param r the red component of the color
860      * @param g the green component of the color
861      * @param b the blue component of the color
862      * @param hsbvals the array used to return the
863      * three HSB values, or <code>null</code>
864      * @return an array of three elements containing the hue, saturation,
865      * and brightness (in that order), of the color with
866      * the indicated red, green, and blue components.
867      * @see java.awt.Color#getRGB()
868      * @see java.awt.Color#Color(int)
869      * @see java.awt.image.ColorModel#getRGBdefault()
870      * @since JDK1.0
871      */

872     public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
873     float hue, saturation, brightness;
874     if (hsbvals == null) {
875         hsbvals = new float[3];
876     }
877         int cmax = (r > g) ? r : g;
878     if (b > cmax) cmax = b;
879     int cmin = (r < g) ? r : g;
880     if (b < cmin) cmin = b;
881
882     brightness = ((float) cmax) / 255.0f;
883     if (cmax != 0)
884         saturation = ((float) (cmax - cmin)) / ((float) cmax);
885     else
886         saturation = 0;
887     if (saturation == 0)
888         hue = 0;
889     else {
890         float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
891         float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
892         float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
893         if (r == cmax)
894         hue = bluec - greenc;
895         else if (g == cmax)
896             hue = 2.0f + redc - bluec;
897             else
898         hue = 4.0f + greenc - redc;
899         hue = hue / 6.0f;
900         if (hue < 0)
901         hue = hue + 1.0f;
902     }
903     hsbvals[0] = hue;
904     hsbvals[1] = saturation;
905     hsbvals[2] = brightness;
906     return hsbvals;
907     }
908
909     /**
910      * Creates a <code>Color</code> object based on the specified values
911      * for the HSB color model.
912      * <p>
913      * The <code>s</code> and <code>b</code> components should be
914      * floating-point values between zero and one
915      * (numbers in the range 0.0-1.0). The <code>h</code> component
916      * can be any floating-point number. The floor of this number is
917      * subtracted from it to create a fraction between 0 and 1. This
918      * fractional number is then multiplied by 360 to produce the hue
919      * angle in the HSB color model.
920      * @param h the hue component
921      * @param s the saturation of the color
922      * @param b the brightness of the color
923      * @return a <code>Color</code> object with the specified hue,
924      * saturation, and brightness.
925      * @since JDK1.0
926      */

927     public static Color JavaDoc getHSBColor(float h, float s, float b) {
928     return new Color JavaDoc(HSBtoRGB(h, s, b));
929     }
930
931     /**
932      * Returns a <code>float</code> array containing the color and alpha
933      * components of the <code>Color</code>, as represented in the default
934      * sRGB color space.
935      * If <code>compArray</code> is <code>null</code>, an array of length
936      * 4 is created for the return value. Otherwise,
937      * <code>compArray</code> must have length 4 or greater,
938      * and it is filled in with the components and returned.
939      * @param compArray an array that this method fills with
940      * color and alpha components and returns
941      * @return the RGBA components in a <code>float</code> array.
942      */

943     public float[] getRGBComponents(float[] compArray) {
944         float[] f;
945         if (compArray == null) {
946             f = new float[4];
947         } else {
948             f = compArray;
949         }
950         if (frgbvalue == null) {
951             f[0] = ((float)getRed())/255f;
952             f[1] = ((float)getGreen())/255f;
953             f[2] = ((float)getBlue())/255f;
954             f[3] = ((float)getAlpha())/255f;
955         } else {
956             f[0] = frgbvalue[0];
957             f[1] = frgbvalue[1];
958             f[2] = frgbvalue[2];
959             f[3] = falpha;
960         }
961         return f;
962     }
963
964     /**
965      * Returns a <code>float</code> array containing only the color
966      * components of the <code>Color</code>, in the default sRGB color
967      * space. If <code>compArray</code> is <code>null</code>, an array of
968      * length 3 is created for the return value. Otherwise,
969      * <code>compArray</code> must have length 3 or greater, and it is
970      * filled in with the components and returned.
971      * @param compArray an array that this method fills with color
972      * components and returns
973      * @return the RGB components in a <code>float</code> array.
974      */

975     public float[] getRGBColorComponents(float[] compArray) {
976         float[] f;
977         if (compArray == null) {
978             f = new float[3];
979         } else {
980             f = compArray;
981         }
982         if (frgbvalue == null) {
983             f[0] = ((float)getRed())/255f;
984             f[1] = ((float)getGreen())/255f;
985             f[2] = ((float)getBlue())/255f;
986         } else {
987             f[0] = frgbvalue[0];
988             f[1] = frgbvalue[1];
989             f[2] = frgbvalue[2];
990         }
991         return f;
992     }
993
994     /**
995      * Returns a <code>float</code> array containing the color and alpha
996      * components of the <code>Color</code>, in the
997      * <code>ColorSpace</code> of the <code>Color</code>.
998      * If <code>compArray</code> is <code>null</code>, an array with
999      * length equal to the number of components in the associated
1000     * <code>ColorSpace</code> plus one is created for
1001     * the return value. Otherwise, <code>compArray</code> must have at
1002     * least this length and it is filled in with the components and
1003     * returned.
1004     * @param compArray an array that this method fills with the color and
1005     * alpha components of this <code>Color</code> in its
1006     * <code>ColorSpace</code> and returns
1007     * @return the color and alpha components in a <code>float</code>
1008     * array.
1009     */

1010    public float[] getComponents(float[] compArray) {
1011        if (fvalue == null)
1012            return getRGBComponents(compArray);
1013        float[] f;
1014        int n = fvalue.length;
1015        if (compArray == null) {
1016            f = new float[n + 1];
1017        } else {
1018            f = compArray;
1019        }
1020        for (int i = 0; i < n; i++) {
1021            f[i] = fvalue[i];
1022        }
1023        f[n] = falpha;
1024        return f;
1025    }
1026
1027    /**
1028     * Returns a <code>float</code> array containing only the color
1029     * components of the <code>Color</code>, in the
1030     * <code>ColorSpace</code> of the <code>Color</code>.
1031     * If <code>compArray</code> is <code>null</code>, an array with
1032     * length equal to the number of components in the associated
1033     * <code>ColorSpace</code> is created for
1034     * the return value. Otherwise, <code>compArray</code> must have at
1035     * least this length and it is filled in with the components and
1036     * returned.
1037     * @param compArray an array that this method fills with the color
1038     * components of this <code>Color</code> in its
1039     * <code>ColorSpace</code> and returns
1040     * @return the color components in a <code>float</code> array.
1041     */

1042    public float[] getColorComponents(float[] compArray) {
1043        if (fvalue == null)
1044            return getRGBColorComponents(compArray);
1045        float[] f;
1046        int n = fvalue.length;
1047        if (compArray == null) {
1048            f = new float[n];
1049        } else {
1050            f = compArray;
1051        }
1052        for (int i = 0; i < n; i++) {
1053            f[i] = fvalue[i];
1054        }
1055        return f;
1056    }
1057
1058    /**
1059     * Returns a <code>float</code> array containing the color and alpha
1060     * components of the <code>Color</code>, in the
1061     * <code>ColorSpace</code> specified by the <code>cspace</code>
1062     * parameter. If <code>compArray</code> is <code>null</code>, an
1063     * array with length equal to the number of components in
1064     * <code>cspace</code> plus one is created for the return value.
1065     * Otherwise, <code>compArray</code> must have at least this
1066     * length, and it is filled in with the components and returned.
1067     * @param cspace a specified <code>ColorSpace</code>
1068     * @param compArray an array that this method fills with the
1069     * color and alpha components of this <code>Color</code> in
1070     * the specified <code>ColorSpace</code> and returns
1071     * @return the color and alpha components in a <code>float</code>
1072     * array.
1073     */

1074    public float[] getComponents(ColorSpace JavaDoc cspace, float[] compArray) {
1075        if (cs == null) {
1076            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1077        }
1078        float f[];
1079        if (fvalue == null) {
1080            f = new float[3];
1081            f[0] = ((float)getRed())/255f;
1082            f[1] = ((float)getGreen())/255f;
1083            f[2] = ((float)getBlue())/255f;
1084        } else {
1085            f = fvalue;
1086        }
1087        float tmp[] = cs.toCIEXYZ(f);
1088        float tmpout[] = cspace.fromCIEXYZ(tmp);
1089        if (compArray == null) {
1090            compArray = new float[tmpout.length + 1];
1091        }
1092        for (int i = 0 ; i < tmpout.length ; i++) {
1093            compArray[i] = tmpout[i];
1094        }
1095        if (fvalue == null) {
1096            compArray[tmpout.length] = ((float)getAlpha())/255f;
1097        } else {
1098            compArray[tmpout.length] = falpha;
1099        }
1100        return compArray;
1101    }
1102
1103    /**
1104     * Returns a <code>float</code> array containing only the color
1105     * components of the <code>Color</code> in the
1106     * <code>ColorSpace</code> specified by the <code>cspace</code>
1107     * parameter. If <code>compArray</code> is <code>null</code>, an array
1108     * with length equal to the number of components in
1109     * <code>cspace</code> is created for the return value. Otherwise,
1110     * <code>compArray</code> must have at least this length, and it is
1111     * filled in with the components and returned.
1112     * @param cspace a specified <code>ColorSpace</code>
1113     * @param compArray an array that this method fills with the color
1114     * components of this <code>Color</code> in the specified
1115     * <code>ColorSpace</code>
1116     * @return the color components in a <code>float</code> array.
1117     */

1118    public float[] getColorComponents(ColorSpace JavaDoc cspace, float[] compArray) {
1119        if (cs == null) {
1120            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1121        }
1122        float f[];
1123        if (fvalue == null) {
1124            f = new float[3];
1125            f[0] = ((float)getRed())/255f;
1126            f[1] = ((float)getGreen())/255f;
1127            f[2] = ((float)getBlue())/255f;
1128        } else {
1129            f = fvalue;
1130        }
1131        float tmp[] = cs.toCIEXYZ(f);
1132        float tmpout[] = cspace.fromCIEXYZ(tmp);
1133        if (compArray == null) {
1134            return tmpout;
1135        }
1136        for (int i = 0 ; i < tmpout.length ; i++) {
1137            compArray[i] = tmpout[i];
1138        }
1139        return compArray;
1140    }
1141
1142    /**
1143     * Returns the <code>ColorSpace</code> of this <code>Color</code>.
1144     * @return this <code>Color</code> object's <code>ColorSpace</code>.
1145     */

1146    public ColorSpace JavaDoc getColorSpace() {
1147        if (cs == null) {
1148            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1149        }
1150        return cs;
1151    }
1152
1153    // REMIND: this should really be a Ref!
1154
/**
1155     * The paint context used to generate a solid color pattern.
1156     * @see createContext()
1157     */

1158    transient private PaintContext JavaDoc theContext;
1159
1160    /**
1161     * Creates and returns a {@link PaintContext} used to generate a solid
1162     * color pattern. This enables a <code>Color</code> object to be used
1163     * as an argument to any method requiring an object implementing the
1164     * {@link Paint} interface.
1165     * The same <code>PaintContext</code> is returned, regardless of
1166     * whether or not <code>r</code>, <code>r2d</code>,
1167     * <code>xform</code>, or <code>hints</code> are <code>null</code>.
1168     * @param cm the specified <code>ColorModel</code>
1169     * @param r the specified {@link Rectangle}
1170     * @param r2d the specified {@link Rectangle2D}
1171     * @param xform the specified {@link AffineTransform}
1172     * @param hints the specified {@link RenderingHints}
1173     * @return a <code>PaintContext</code> that is used to generate a
1174     * solid color pattern.
1175     * @see Paint
1176     * @see PaintContext
1177     * @see Graphics2D#setPaint
1178     */

1179    public synchronized PaintContext JavaDoc createContext(ColorModel JavaDoc cm, Rectangle JavaDoc r,
1180                           Rectangle2D JavaDoc r2d,
1181                           AffineTransform JavaDoc xform,
1182                                                   RenderingHints JavaDoc hints) {
1183    PaintContext JavaDoc pc = theContext;
1184    if (pc == null) {
1185        pc = new ColorPaintContext JavaDoc(value, cm);
1186        theContext = pc;
1187    }
1188    return pc;
1189    }
1190
1191    /**
1192     * Returns the transparency mode for this <code>Color</code>. This is
1193     * required to implement the <code>Paint</code> interface.
1194     * @return this <code>Color</code> object's transparency mode.
1195     * @see Paint
1196     * @see Transparency
1197     * @see #createContext
1198     */

1199    public int getTransparency() {
1200        int alpha = getAlpha();
1201        if (alpha == 0xff) {
1202            return Transparency.OPAQUE;
1203        }
1204        else if (alpha == 0) {
1205            return Transparency.BITMASK;
1206        }
1207        else {
1208            return Transparency.TRANSLUCENT;
1209        }
1210    }
1211
1212}
1213
Popular Tags