1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 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, blue
23 * and alpha). 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
27 * maximum intensity and for alpha 0 indicates transparent and
28 * 255 indicates opaque.
30 * The hashCode() method in this class uses the values of the public
31 * fields to compute the hash value. When storing instances of the
32 * class in hashed collections, do not modify these fields after the
33 * object has been inserted.
36 * Application code does <em>not</em> need to explicitly release the
37 * resources managed by each instance when those instances are no longer
38 * required, and thus no <code>dispose()</code> method is provided.
42 * @see <a href="http://www.eclipse.org/swt/snippets/#color">Color and RGB snippets</a>
43 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
46 public final class RGBA implements Serializable {
48 * the RGB component of the RGBA
53 * the alpha component of the RGBA
57 static final long serialVersionUID = 1049467103126495855L;
60 * Constructs an instance of this class with the given
61 * red, green, blue and alpha values.
63 * @param red the red component of the new instance
64 * @param green the green component of the new instance
65 * @param blue the blue component of the new instance
66 * @param alpha the alpha component of the new instance
68 * @exception IllegalArgumentException <ul>
69 * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li>
72 public RGBA(int red, int green, int blue, int alpha) {
73 if ((alpha > 255) || (alpha < 0)) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
74 this.rgb = new RGB (red, green, blue);
79 * Constructs an instance of this class with the given
80 * hue, saturation, and brightness.
82 * @param hue the hue value for the HSBA color (from 0 to 360)
83 * @param saturation the saturation value for the HSBA color (from 0 to 1)
84 * @param brightness the brightness value for the HSBA color (from 0 to 1)
85 * @param alpha the alpha value for the HSBA color (from 0 to 255)
87 * @exception IllegalArgumentException <ul>
88 * <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
89 * the saturation or brightness is not between 0 and 1 or if the alpha
90 * is not between 0 and 255</li>
94 public RGBA(float hue, float saturation, float brightness, float alpha) {
95 if ((alpha > 255) || (alpha < 0)) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
96 rgb = new RGB(hue, saturation, brightness);
97 this.alpha = (int)(alpha + 0.5);
101 * Returns the hue, saturation, and brightness of the color.
103 * @return color space values in float format:<ul>
104 * <li>hue (from 0 to 360)</li>
105 * <li>saturation (from 0 to 1)</li>
106 * <li>brightness (from 0 to 1)</li>
107 * <li>alpha (from 0 to 255)</li>
109 * @see #RGBA(float, float, float, float)
111 public float[] getHSBA() {
112 float[] hsb = rgb.getHSB();
113 return new float[] {hsb[0], hsb[1], hsb[2], alpha};
117 * Compares the argument to the receiver, and returns true
118 * if they represent the <em>same</em> object using a class
119 * specific comparison.
121 * @param object the object to compare with this object
122 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
127 public boolean equals(Object object) {
128 if (object == this) return true;
129 if (!(object instanceof RGBA)) return false;
130 RGBA rgba = (RGBA)object;
131 return (rgba.rgb.red == this.rgb.red) && (rgba.rgb.green == this.rgb.green) && (rgba.rgb.blue == this.rgb.blue)
132 && (rgba.alpha == this.alpha);
136 * Returns an integer hash code for the receiver. Any two
137 * objects that return <code>true</code> when passed to
138 * <code>equals</code> must return the same value for this
141 * @return the receiver's hash
143 * @see #equals(Object)
146 public int hashCode() {
147 return (alpha << 24) | (rgb.blue << 16) | (rgb.green << 8) | rgb.red;
151 * Returns a string containing a concise, human-readable
152 * description of the receiver.
154 * @return a string representation of the <code>RGBA</code>
157 public String toString() {
158 return "RGBA {" + rgb.red + ", " + rgb.green + ", " + rgb.blue + ", " + alpha + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$