KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > Color


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import java.io.Serializable JavaDoc;
33
34 /**
35  * A representation of a 24-bit RGB color.
36  */

37 public class Color
38 implements Serializable JavaDoc {
39
40     /** The color black. */
41     public static final Color BLACK = new Color(0x00, 0x00, 0x00);
42
43     /** The color blue. */
44     public static final Color BLUE = new Color(0x00, 0x00, 0xff);
45
46     /** The color green. */
47     public static final Color GREEN = new Color(0x00, 0xff, 0x00);
48
49     /** The color cyan. */
50     public static final Color CYAN = new Color(0x00, 0xff, 0xff);
51
52     /** The color red. */
53     public static final Color RED = new Color(0xff, 0x00, 0x00);
54
55     /** The color magenta. */
56     public static final Color MAGENTA = new Color(0xff, 0x00, 0xff);
57
58     /** The color yellow. */
59     public static final Color YELLOW = new Color(0xff, 0xff, 0x00);
60
61     /** The color white. */
62     public static final Color WHITE = new Color(0xff, 0xff, 0xff);
63
64     /** The color dark gray. */
65     public static final Color DARKGRAY = new Color(0x7f, 0x7f, 0x7f);
66
67     /** The color light gray. */
68     public static final Color LIGHTGRAY = new Color(0xaf, 0xaf, 0xaf);
69
70     /** The color orange. */
71     public static final Color ORANGE = new Color(0xff, 0xaf, 0x00);
72
73     /** The color pink. */
74     public static final Color PINK = new Color(0xff, 0xaf, 0xaf);
75
76     private int rgb;
77     
78     /**
79      * Creates a new color from an integer value.
80      * The value should be of the for 0xRRGGBB.
81      *
82      * @param rgb an integer representation for a color
83      */

84     public Color(int rgb) {
85         this.rgb = rgb;
86     }
87     
88     /**
89      * Creates a new color with specified red, green, and blue values. Each
90      * value may range from 0 to 255.
91      *
92      * @param r the red component value
93      * @param g the green component value
94      * @param b the blue component value
95      */

96     public Color(int r, int g, int b) {
97         this.rgb = ((r & 0xff) << 16) | ((g & 0xff) << 8) | b & 0xff;
98     }
99     
100     /**
101      * @see java.lang.Object#equals(java.lang.Object)
102      */

103     public boolean equals(Object JavaDoc o) {
104         boolean equal;
105     
106         if (this == o) {
107             equal = true;
108         } else if (o instanceof Color) {
109             Color that = (Color) o;
110             equal = this.rgb == that.rgb;
111         } else {
112             equal = false;
113         }
114         
115         return equal;
116     }
117     
118     /**
119      * Returns the blue component value of this color.
120      *
121      * @return the blue component value of this color, from 0 to 255
122      */

123     public int getBlue() {
124         return rgb & 0xff;
125     }
126     
127     /**
128      * Returns the green component value of this color.
129      *
130      * @return the green component value of this color, from 0 to 255
131      */

132     public int getGreen() {
133         return (rgb >> 8) & 0xff;
134     }
135     
136     /**
137      * Returns the red component value of this color.
138      *
139      * @return the red component value of this color, from 0 to 255
140      */

141     public int getRed() {
142         return rgb >> 16;
143     }
144     
145     /**
146      * Returns the color as an RGB value.
147      *
148      * @return the color as an RGB value
149      */

150     public int getRgb() {
151         return rgb;
152     }
153
154     /**
155      * @see java.lang.Object#hashCode()
156      */

157     public int hashCode() {
158         return getRgb();
159     }
160     
161     /**
162      * @see java.lang.Object#toString()
163      */

164     public String JavaDoc toString() {
165         return getClass().getName() + " [r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]";
166     }
167 }
168
Popular Tags