KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > util > HSBColor


1 /*
2  * HSBColor.java, translates RGB colors into Hue-Saturation-Brightness
3  * colors.
4  * Copyright (C) Achim Westermann, created on 19.05.2005, 22:01:51
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * If you modify or optimize the code in a useful way please let me know.
21  * Achim.Westermann@gmx.de
22  *
23  */

24 package info.monitorenter.gui.util;
25
26 /**
27  * Color that internally works with the Hue Saturation Luminance color space.
28  * <p>
29  * <p>
30  *
31  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
32  *
33  * @version $Revision: 1.1 $
34  */

35 public class HSBColor implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
36
37   /**
38    * Generated <code>serialVersionUID</code>.
39    */

40   private static final long serialVersionUID = 3257288036910903863L;
41
42   /**
43    * Inspired by
44    * <code>float[] java.awt.Color.RGBtoHSB(int r,int g, int b, float[]hsbvals)</code>
45    * except that algorithm is tuned <br>
46    * Testing results showed about 25% speed up. Therefore the sources have
47    * become harder to understand.
48    *
49    * @param color
50    * the <code>java.awt.Color</code> (that follows the RGB model) and
51    * should be transformed to a color instance in the
52    * hue-saturation-luminance model.
53    *
54    * @return the transformed values of the RGB colors in that order:
55    * hue,saturation,brightness.
56    */

57   public static HSBColor rgbToHSB(final java.awt.Color JavaDoc color) {
58     int rgb = color.getRGB();
59     int r = (rgb & 0xFF0000) >> 16;
60     int g = (rgb & 0xFF00) >> 8;
61     int b = (rgb & 0xFF);
62     HSBColor ret = new HSBColor();
63
64     int cmax = (r >= g) ? (r >= b) ? r : b : (g >= b) ? g : b;
65     int cmin = (r <= g) ? (r <= b) ? r : b : (g <= b) ? g : b;
66     ret.m_lum = ((float) cmax) / 255f;
67     if (cmax != cmin) {
68       float difference = (cmax - cmin);
69       ret.m_sat = difference / ((float) cmax);
70       if (r == cmax) {
71         ret.m_hue = (g - b) / difference;
72       } else if (g == cmax) {
73         ret.m_hue = (b - r) / difference + 2.0f;
74       } else {
75         ret.m_hue = (r - g) / difference + 4.0f;
76       }
77       ret.m_hue /= 6.0f;
78       if (ret.m_hue < 0) {
79         ret.m_hue += 1.0f;
80       }
81     } else {
82       ret.m_sat = 0;
83       ret.m_hue = 0;
84     }
85     return ret;
86   }
87
88   /** Hue value between 0.0 and 1.0. */
89   protected float m_hue;
90
91   /** Luminance value between 0.0 and 1.0. */
92   protected float m_lum;
93
94   /** Saturation value between 0.0 and 1.0. */
95   protected float m_sat;
96
97   /**
98    * Constructor for internal use only.
99    * <p>
100    */

101   private HSBColor() {
102     // nop
103
}
104
105   /**
106    * Creates an instance with the given values for hue saturation and luminance.
107    * <p>
108    *
109    * @param hue
110    * the hue component of the HSBColor
111    * @param saturation
112    * the saturation component of the HSBColor
113    * @param brightness
114    * the brightness component of the HSBColor
115    */

116   HSBColor(final float hue, final float saturation, final float brightness) {
117     this.m_hue = hue;
118     this.m_sat = saturation;
119     this.m_lum = brightness;
120   }
121
122   /**
123    * Creates an instance transformed from the rgb color.
124    * <p>
125    *
126    * @param rgbcolor
127    * standard java rgb color.
128    */

129   public HSBColor(final java.awt.Color JavaDoc rgbcolor) {
130     int rgb = rgbcolor.getRGB();
131     int r = (rgb & 0xFF0000) >> 16;
132     int g = (rgb & 0xFF00) >> 8;
133     int b = (rgb & 0xFF);
134     int cmax = (r >= g) ? (r >= b) ? r : b : (g >= b) ? g : b;
135     int cmin = (r <= g) ? (r <= b) ? r : b : (g <= b) ? g : b;
136     this.m_lum = ((float) cmax) / 255f;
137     if (cmax != cmin) {
138       float difference = (cmax - cmin);
139       this.m_sat = difference / ((float) cmax);
140       if (r == cmax) {
141         this.m_hue = (g - b) / difference;
142       } else if (g == cmax) {
143         this.m_hue = (b - r) / difference + 2.0f;
144       } else {
145         this.m_hue = (r - g) / difference + 4.0f;
146       }
147       this.m_hue /= 6.0f;
148       if (this.m_hue < 0) {
149         this.m_hue += 1.0f;
150       }
151     } else {
152       this.m_sat = 0;
153       this.m_hue = 0;
154     }
155   }
156
157   /**
158    * Clone implementation.
159    * <p>
160    *
161    * Following statements are true: <br>
162    * <code>
163    * x.clone() != x
164    * x.clone().getClass() == x.getClass()
165    * x.clone().equals(x)
166    * </code>
167    * A deep copy of this HSBColor is returned.
168    * <p>
169    *
170    * @return an intance copied from this one.
171    */

172   public Object JavaDoc clone() {
173     return new HSBColor(this.m_hue, this.m_sat, this.m_lum);
174   }
175
176   /**
177    * Equals implementation.
178    * <p>
179    *
180    * Returns true if :<br>
181    * <code>
182    * <nobr>
183    * o.instanceof HSBColor && (this.hue==o.hue) && (this.sat == o.sat) && (this.lum == o.lum)
184    * </nobr>
185    * </code>
186    * <p>
187    *
188    * @param o
189    * the other {@link HSBColor} instance.
190    *
191    * @return true if the colors are judged equal.
192    */

193   public boolean equals(final Object JavaDoc o) {
194     if (!(o instanceof HSBColor)) {
195       return false;
196     }
197     HSBColor other = (HSBColor) o;
198     return (this.m_hue == other.m_hue) && (this.m_sat == other.m_sat)
199         && (this.m_lum == other.m_lum);
200   }
201
202   /**
203    * Returns the transformation of this color to the rgb color.
204    * <p>
205    *
206    * @return the transformation of this color to the rgb color.
207    */

208
209   public java.awt.Color JavaDoc getRGBColor() {
210     return new java.awt.Color JavaDoc(java.awt.Color.HSBtoRGB(this.m_hue, this.m_sat, this.m_lum));
211   }
212
213   /**
214    * @see java.lang.Object#hashCode()
215    */

216   public int hashCode() {
217
218     return (int) (this.m_hue * 10000 + this.m_sat * 1000 + this.m_lum * 100);
219   }
220 }
221
Popular Tags