001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.graphics;
011:
012: import org.eclipse.swt.internal.SerializableCompatibility;
013: import org.eclipse.swt.*;
014:
015: /**
016: * Instances of this class are descriptions of colors in
017: * terms of the primary additive color model (red, green and
018: * blue). A color may be described in terms of the relative
019: * intensities of these three primary colors. The brightness
020: * of each color is specified by a value in the range 0 to 255,
021: * where 0 indicates no color (blackness) and 255 indicates
022: * maximum intensity.
023: * <p>
024: * The hashCode() method in this class uses the values of the public
025: * fields to compute the hash value. When storing instances of the
026: * class in hashed collections, do not modify these fields after the
027: * object has been inserted.
028: * </p>
029: * <p>
030: * Application code does <em>not</em> need to explicitly release the
031: * resources managed by each instance when those instances are no longer
032: * required, and thus no <code>dispose()</code> method is provided.
033: * </p>
034: *
035: * @see Color
036: */
037:
038: public final class RGB implements SerializableCompatibility {
039:
040: /**
041: * the red component of the RGB
042: */
043: public int red;
044:
045: /**
046: * the green component of the RGB
047: */
048: public int green;
049:
050: /**
051: * the blue component of the RGB
052: */
053: public int blue;
054:
055: static final long serialVersionUID = 3258415023461249074L;
056:
057: /**
058: * Constructs an instance of this class with the given
059: * red, green and blue values.
060: *
061: * @param red the red component of the new instance
062: * @param green the green component of the new instance
063: * @param blue the blue component of the new instance
064: *
065: * @exception IllegalArgumentException <ul>
066: * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
067: * </ul>
068: */
069: public RGB(int red, int green, int blue) {
070: if ((red > 255) || (red < 0) || (green > 255) || (green < 0)
071: || (blue > 255) || (blue < 0))
072: SWT.error(SWT.ERROR_INVALID_ARGUMENT);
073: this .red = red;
074: this .green = green;
075: this .blue = blue;
076: }
077:
078: /**
079: * Constructs an instance of this class with the given
080: * hue, saturation, and brightness.
081: *
082: * @param hue the hue value for the HSB color (from 0 to 360)
083: * @param saturation the saturation value for the HSB color (from 0 to 1)
084: * @param brightness the brightness value for the HSB color (from 0 to 1)
085: *
086: * @exception IllegalArgumentException <ul>
087: * <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
088: * the saturation or brightness is not between 0 and 1</li>
089: * </ul>
090: *
091: * @since 3.2
092: */
093: public RGB(float hue, float saturation, float brightness) {
094: if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1
095: || brightness < 0 || brightness > 1) {
096: SWT.error(SWT.ERROR_INVALID_ARGUMENT);
097: }
098: float r, g, b;
099: if (saturation == 0) {
100: r = g = b = brightness;
101: } else {
102: if (hue == 360)
103: hue = 0;
104: hue /= 60;
105: int i = (int) hue;
106: float f = hue - i;
107: float p = brightness * (1 - saturation);
108: float q = brightness * (1 - saturation * f);
109: float t = brightness * (1 - saturation * (1 - f));
110: switch (i) {
111: case 0:
112: r = brightness;
113: g = t;
114: b = p;
115: break;
116: case 1:
117: r = q;
118: g = brightness;
119: b = p;
120: break;
121: case 2:
122: r = p;
123: g = brightness;
124: b = t;
125: break;
126: case 3:
127: r = p;
128: g = q;
129: b = brightness;
130: break;
131: case 4:
132: r = t;
133: g = p;
134: b = brightness;
135: break;
136: case 5:
137: default:
138: r = brightness;
139: g = p;
140: b = q;
141: break;
142: }
143: }
144: red = (int) (r * 255 + 0.5);
145: green = (int) (g * 255 + 0.5);
146: blue = (int) (b * 255 + 0.5);
147: }
148:
149: /**
150: * Returns the hue, saturation, and brightness of the color.
151: *
152: * @return color space values in float format (hue, saturation, brightness)
153: *
154: * @since 3.2
155: */
156: public float[] getHSB() {
157: float r = red / 255f;
158: float g = green / 255f;
159: float b = blue / 255f;
160: float max = Math.max(Math.max(r, g), b);
161: float min = Math.min(Math.min(r, g), b);
162: float delta = max - min;
163: float hue = 0;
164: float brightness = max;
165: float saturation = max == 0 ? 0 : (max - min) / max;
166: if (delta != 0) {
167: if (r == max) {
168: hue = (g - b) / delta;
169: } else {
170: if (g == max) {
171: hue = 2 + (b - r) / delta;
172: } else {
173: hue = 4 + (r - g) / delta;
174: }
175: }
176: hue *= 60;
177: if (hue < 0)
178: hue += 360;
179: }
180: return new float[] { hue, saturation, brightness };
181: }
182:
183: /**
184: * Compares the argument to the receiver, and returns true
185: * if they represent the <em>same</em> object using a class
186: * specific comparison.
187: *
188: * @param object the object to compare with this object
189: * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
190: *
191: * @see #hashCode()
192: */
193: public boolean equals(Object object) {
194: if (object == this )
195: return true;
196: if (!(object instanceof RGB))
197: return false;
198: RGB rgb = (RGB) object;
199: return (rgb.red == this .red) && (rgb.green == this .green)
200: && (rgb.blue == this .blue);
201: }
202:
203: /**
204: * Returns an integer hash code for the receiver. Any two
205: * objects that return <code>true</code> when passed to
206: * <code>equals</code> must return the same value for this
207: * method.
208: *
209: * @return the receiver's hash
210: *
211: * @see #equals(Object)
212: */
213: public int hashCode() {
214: return (blue << 16) | (green << 8) | red;
215: }
216:
217: /**
218: * Returns a string containing a concise, human-readable
219: * description of the receiver.
220: *
221: * @return a string representation of the <code>RGB</code>
222: */
223: public String toString() {
224: return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
225: }
226:
227: }
|