1/*
2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.awt.*;
25import java.awt.color.ColorSpace;
26
27/*
28 * @test
29 * @summary Check Color constructors and methods works correctly in headless mode
30 * @run main/othervm -Djava.awt.headless=true HeadlessColor
31 */
32
33public class HeadlessColor {
34    public static void main(String args[]) {
35        Color c;
36
37        // Constructors without exceptions
38        c = new Color(1, 2, 3);
39        c = new Color(1, 2, 3, 4);
40        c = new Color((1 << 16) | (2 << 8) | (3));
41        c = new Color((1 << 24) | (1 << 16) | (2 << 8) | (3));
42        c = new Color((1 << 24) | (2 << 16) | (3 << 8) | (4), true);
43        c = new Color((2 << 16) | (3 << 8) | (4), false);
44        c = new Color(0.8f, 0.8f, 0.3f);
45        c = new Color(0.999f, 0.8f, 0.8f, 0.3f);
46        c = new Color(ColorSpace.getInstance(ColorSpace.CS_sRGB),
47                new float[]{0.8f, 0.8f, 0.3f}, 1f);
48
49        // Constructors with exceptions
50        boolean exceptions = false;
51        try {
52            c = new Color(409, 400, 400);
53        } catch (IllegalArgumentException e) {
54            exceptions = true;
55        }
56        if (!exceptions)
57            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
58                    "when expected in headless mode");
59
60        exceptions = false;
61        try {
62            c = new Color(400, 3003, 400, 400);
63        } catch (IllegalArgumentException e) {
64            exceptions = true;
65        }
66        if (!exceptions)
67            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
68                    "when expected in headless mode");
69
70        exceptions = false;
71        try {
72            c = new Color(8f, 8f, 3f);
73        } catch (IllegalArgumentException e) {
74            exceptions = true;
75        }
76        if (!exceptions)
77            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
78                    "when expected in headless mode");
79
80        exceptions = false;
81        try {
82            c = new Color(-8f, -8f, -3f);
83        } catch (IllegalArgumentException e) {
84            exceptions = true;
85        }
86        if (!exceptions)
87            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
88                    "when expected in headless mode");
89
90        exceptions = false;
91        try {
92            c = new Color(0.999f, 8f, 8f, 3f);
93        } catch (IllegalArgumentException e) {
94            exceptions = true;
95        }
96        if (!exceptions)
97            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
98                    "when expected in headless mode");
99
100        exceptions = false;
101        try {
102            c = new Color(20f, 8f, 8f, 3f);
103        } catch (IllegalArgumentException e) {
104            exceptions = true;
105        }
106        if (!exceptions)
107            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
108                    "when expected in headless mode");
109
110        exceptions = false;
111        try {
112            c = new Color(-20f, -8f, -8f, -3f);
113        } catch (IllegalArgumentException e) {
114            exceptions = true;
115        }
116        if (!exceptions)
117            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
118                    "when expected in headless mode");
119
120        exceptions = false;
121        try {
122            c = new Color(ColorSpace.getInstance(ColorSpace.CS_sRGB),
123                    new float[]{-8f, -8f, -3f}, 1f);
124        } catch (IllegalArgumentException e) {
125            exceptions = true;
126        }
127        if (!exceptions)
128            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
129                    "when expected in headless mode");
130
131        exceptions = false;
132        try {
133            c = new Color(ColorSpace.getInstance(ColorSpace.CS_sRGB),
134                    new float[]{-8f, -8f, -3f}, -1f);
135        } catch (IllegalArgumentException e) {
136            exceptions = true;
137        }
138        if (!exceptions)
139            throw new RuntimeException("Constructor did not throw IllegalArgumentException " +
140                    "when expected in headless mode");
141
142
143        c = new Color(1, 2, 3);
144        c.hashCode();
145        c.toString();
146        if (c.getRed() != 1)
147            throw new RuntimeException("Incorrect red value");
148        if (c.getGreen() != 2)
149            throw new RuntimeException("Incorrect green value");
150        if (c.getBlue() != 3)
151            throw new RuntimeException("Incorrect bluevalue");
152        if (c.getAlpha() != 255)
153            throw new RuntimeException("Incorrect alpha value");
154        if (c.getRGB() != ((255 << 24) | (1 << 16) | (2 << 8) | (3)))
155            throw new RuntimeException("Incorrect rgb value");
156
157        int rgb = c.getRGB();
158        c.brighter();
159        if (rgb != c.getRGB())
160            throw new RuntimeException("Color object changed RGB value after brighter() called");
161
162        rgb = c.getRGB();
163        c.darker();
164        if (rgb != c.getRGB())
165            throw new RuntimeException("Color object changed RGB value after brighter() called");
166
167        c = new Color(1, 2, 3, 4);
168        c.hashCode();
169        c.toString();
170        if (c.getRed() != 1)
171            throw new RuntimeException("Incorrect red value");
172        if (c.getGreen() != 2)
173            throw new RuntimeException("Incorrect green value");
174        if (c.getBlue() != 3)
175            throw new RuntimeException("Incorrect bluevalue");
176        if (c.getAlpha() != 4)
177            throw new RuntimeException("Incorrect alpha value");
178        if (c.getRGB() != ((4 << 24) | (1 << 16) | (2 << 8) | (3)))
179            throw new RuntimeException("Incorrect rgb value");
180
181        rgb = c.getRGB();
182        c.brighter();
183        if (rgb != c.getRGB())
184            throw new RuntimeException("Color object changed RGB value after brighter() called");
185
186        rgb = c.getRGB();
187        c.darker();
188        if (rgb != c.getRGB())
189            throw new RuntimeException("Color object changed RGB value after brighter() called");
190
191
192        if (!(new Color(1, 2, 3).equals(new Color(1, 2, 3))))
193            throw new RuntimeException("Inequality in colors when equality expected");
194        if (new Color(1, 2, 3).equals(new Color(3, 2, 1)))
195            throw new RuntimeException("Equality in colors when NO equality expected");
196
197        if (!(new Color(1, 2, 3, 4).equals(new Color(1, 2, 3, 4))))
198            throw new RuntimeException("Inequality in colors when equality expected");
199        if (new Color(1, 2, 3, 4).equals(new Color(4, 3, 2, 1)))
200            throw new RuntimeException("Equality in colors when NO equality expected");
201
202        c = Color.decode("0xffffff");
203        c = Color.getColor("65535");
204        c = Color.getColor("65535", Color.black);
205        c = Color.getColor("65535", 0xffffff);
206
207        int hsb_value = Color.HSBtoRGB(0.1f, 0.2f, 0.3f);
208        float[] rgb_value = Color.RGBtoHSB(1, 2, 3, null);
209
210        c = Color.getHSBColor(0.3f, 0.4f, 0.6f);
211        c = Color.getHSBColor(-0.3f, -0.4f, -0.6f);
212        c = Color.getHSBColor(30, 40, 60);
213
214        float[] comps;
215        comps = Color.black.getRGBComponents(null);
216        comps = Color.black.getRGBColorComponents(null);
217        comps = Color.black.getComponents(null);
218        comps = Color.black.getColorComponents(null);
219        comps = Color.black.getComponents(ColorSpace.getInstance(ColorSpace.CS_sRGB), null);
220        comps = Color.black.getColorComponents(ColorSpace.getInstance(ColorSpace.CS_sRGB), null);
221
222        Color.black.getColorSpace();
223        Color.black.getTransparency();
224    }
225}
226