1 /*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.graphics;
18 import org.eclipse.swt.*;
21 * Instances of this class are descriptions of colors in
22 * terms of the primary additive color model (red, green and
23 * blue). A color may be described in terms of the relative
24 * intensities of these three primary colors. The brightness
25 * of each color is specified by a value in the range 0 to 255,
26 * where 0 indicates no color (blackness) and 255 indicates
29 * The hashCode() method in this class uses the values of the public
30 * fields to compute the hash value. When storing instances of the
31 * class in hashed collections, do not modify these fields after the
32 * object has been inserted.
35 * Application code does <em>not</em> need to explicitly release the
36 * resources managed by each instance when those instances are no longer
37 * required, and thus no <code>dispose()</code> method is provided.
41 * @see <a href="http://www.eclipse.org/swt/snippets/#color">Color and RGB snippets</a>
42 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
45 public final class RGB implements Serializable {
48 * the red component of the RGB
53 * the green component of the RGB
58 * the blue component of the RGB
62 static final long serialVersionUID = 3258415023461249074L;
65 * Constructs an instance of this class with the given
66 * red, green and blue values.
68 * @param red the red component of the new instance
69 * @param green the green component of the new instance
70 * @param blue the blue component of the new instance
72 * @exception IllegalArgumentException <ul>
73 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
76 public RGB(int red, int green, int blue) {
77 if ((red > 255) || (red < 0) ||
78 (green > 255) || (green < 0) ||
79 (blue > 255) || (blue < 0))
80 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
87 * Constructs an instance of this class with the given
88 * hue, saturation, and brightness.
90 * @param hue the hue value for the HSB color (from 0 to 360)
91 * @param saturation the saturation value for the HSB color (from 0 to 1)
92 * @param brightness the brightness value for the HSB color (from 0 to 1)
94 * @exception IllegalArgumentException <ul>
95 * <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
96 * the saturation or brightness is not between 0 and 1</li>
101 public RGB(float hue, float saturation, float brightness) {
102 if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1 ||
103 brightness < 0 || brightness > 1) {
104 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
107 if (saturation == 0) {
108 r = g = b = brightness;
110 if (hue == 360) hue = 0;
114 float p = brightness * (1 - saturation);
115 float q = brightness * (1 - saturation * f);
116 float t = brightness * (1 - saturation * (1 - f));
151 red = (int)(r * 255 + 0.5);
152 green = (int)(g * 255 + 0.5);
153 blue = (int)(b * 255 + 0.5);
157 * Returns the hue, saturation, and brightness of the color.
159 * @return color space values in float format:<ul>
160 * <li>hue (from 0 to 360)</li>
161 * <li>saturation (from 0 to 1)</li>
162 * <li>brightness (from 0 to 1)</li>
164 * @see #RGB(float, float, float)
168 public float[] getHSB() {
169 float r = red / 255f;
170 float g = green / 255f;
171 float b = blue / 255f;
172 float max = Math.max(Math.max(r, g), b);
173 float min = Math.min(Math.min(r, g), b);
174 float delta = max - min;
176 float brightness = max;
177 float saturation = max == 0 ? 0 : (max - min) / max;
180 hue = (g - b) / delta;
183 hue = 2 + (b - r) / delta;
185 hue = 4 + (r - g) / delta;
189 if (hue < 0) hue += 360;
191 return new float[] {hue, saturation, brightness};
195 * Compares the argument to the receiver, and returns true
196 * if they represent the <em>same</em> object using a class
197 * specific comparison.
199 * @param object the object to compare with this object
200 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
205 public boolean equals(Object object) {
206 if (object == this) return true;
207 if (!(object instanceof RGB)) return false;
208 RGB rgb = (RGB)object;
209 return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue);
213 * Returns an integer hash code for the receiver. Any two
214 * objects that return <code>true</code> when passed to
215 * <code>equals</code> must return the same value for this
218 * @return the receiver's hash
220 * @see #equals(Object)
223 public int hashCode() {
224 return (blue << 16) | (green << 8) | red;
228 * Returns a string containing a concise, human-readable
229 * description of the receiver.
231 * @return a string representation of the <code>RGB</code>
234 public String toString() {
235 return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$