]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/graphics/RGBA.java
24d043127e509566c732c03f632b477f6749c530
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / graphics / RGBA.java
1 /*******************************************************************************
2  * Copyright (c) 2015, 2016 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.graphics;
15
16 import java.io.*;
17
18 import org.eclipse.swt.*;
19
20 /**
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.
29  * <p>
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.
34  * </p>
35  * <p>
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.
39  * </p>
40  *
41  * @see Color
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>
44  * @since 3.104
45  */
46 public final class RGBA implements Serializable {
47         /**
48          * the RGB component of the RGBA
49          */
50         public final RGB rgb;
51
52         /**
53          * the alpha component of the RGBA
54          */
55         public int alpha;
56
57         static final long serialVersionUID = 1049467103126495855L;
58
59 /**
60  * Constructs an instance of this class with the given
61  * red, green, blue and alpha values.
62  *
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
67  *
68  * @exception IllegalArgumentException <ul>
69  *    <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li>
70  * </ul>
71  */
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);
75         this.alpha = alpha;
76 }
77
78 /**
79 * Constructs an instance of this class with the given
80 * hue, saturation, and brightness.
81 *
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)
86 *
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>
91 * </ul>
92 *
93 */
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);
98 }
99
100 /**
101  * Returns the hue, saturation, and brightness of the color.
102  *
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>
108  *             </ul>
109  * @see #RGBA(float, float, float, float)
110  */
111 public float[] getHSBA() {
112         float[] hsb = rgb.getHSB();
113         return new float[] {hsb[0], hsb[1], hsb[2], alpha};
114 }
115
116 /**
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.
120  *
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
123  *
124  * @see #hashCode()
125  */
126 @Override
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);
133 }
134
135 /**
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
139  * method.
140  *
141  * @return the receiver's hash
142  *
143  * @see #equals(Object)
144  */
145 @Override
146 public int hashCode() {
147         return (alpha << 24) | (rgb.blue << 16) | (rgb.green << 8) | rgb.red;
148 }
149
150 /**
151  * Returns a string containing a concise, human-readable
152  * description of the receiver.
153  *
154  * @return a string representation of the <code>RGBA</code>
155  */
156 @Override
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$
159 }
160
161 }