1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Oleg V. Khaschansky
19 * @version $Revision$
20 */
21package java.awt.color;
22
23import java.io.Serializable;
24
25import org.apache.harmony.awt.gl.color.LUTColorConverter;
26import org.apache.harmony.awt.internal.nls.Messages;
27
28/**
29 * The ColorSpace class defines a color space type for a Color and provides
30 * methods for arrays of color component operations.
31 *
32 * @since Android 1.0
33 */
34public abstract class ColorSpace implements Serializable {
35
36    /** The Constant serialVersionUID. */
37    private static final long serialVersionUID = -409452704308689724L;
38
39    /**
40     * The Constant TYPE_XYZ indicates XYZ color space type.
41     */
42    public static final int TYPE_XYZ = 0;
43
44    /**
45     * The Constant TYPE_Lab indicates Lab color space type.
46     */
47    public static final int TYPE_Lab = 1;
48
49    /**
50     * The Constant TYPE_Luv indicates Luv color space type.
51     */
52    public static final int TYPE_Luv = 2;
53
54    /**
55     * The Constant TYPE_YCbCr indicates YCbCr color space type.
56     */
57    public static final int TYPE_YCbCr = 3;
58
59    /**
60     * The Constant TYPE_Yxy indicates Yxy color space type.
61     */
62    public static final int TYPE_Yxy = 4;
63
64    /**
65     * The Constant TYPE_RGB indicates RGB color space type.
66     */
67    public static final int TYPE_RGB = 5;
68
69    /**
70     * The Constant TYPE_GRAY indicates Gray color space type.
71     */
72    public static final int TYPE_GRAY = 6;
73
74    /**
75     * The Constant TYPE_HSV indicates HSV color space type.
76     */
77    public static final int TYPE_HSV = 7;
78
79    /**
80     * The Constant TYPE_HLS indicates HLS color space type.
81     */
82    public static final int TYPE_HLS = 8;
83
84    /**
85     * The Constant TYPE_CMYK indicates CMYK color space type.
86     */
87    public static final int TYPE_CMYK = 9;
88
89    /**
90     * The Constant TYPE_CMY indicates CMY color space type.
91     */
92    public static final int TYPE_CMY = 11;
93
94    /**
95     * The Constant TYPE_2CLR indicates color spaces with 2 components.
96     */
97    public static final int TYPE_2CLR = 12;
98
99    /**
100     * The Constant TYPE_3CLR indicates color spaces with 3 components.
101     */
102    public static final int TYPE_3CLR = 13;
103
104    /**
105     * The Constant TYPE_4CLR indicates color spaces with 4 components.
106     */
107    public static final int TYPE_4CLR = 14;
108
109    /**
110     * The Constant TYPE_5CLR indicates color spaces with 5 components.
111     */
112    public static final int TYPE_5CLR = 15;
113
114    /**
115     * The Constant TYPE_6CLR indicates color spaces with 6 components.
116     */
117    public static final int TYPE_6CLR = 16;
118
119    /**
120     * The Constant TYPE_7CLR indicates color spaces with 7 components.
121     */
122    public static final int TYPE_7CLR = 17;
123
124    /**
125     * The Constant TYPE_8CLR indicates color spaces with 8 components.
126     */
127    public static final int TYPE_8CLR = 18;
128
129    /**
130     * The Constant TYPE_9CLR indicates color spaces with 9 components.
131     */
132    public static final int TYPE_9CLR = 19;
133
134    /**
135     * The Constant TYPE_ACLR indicates color spaces with 10 components.
136     */
137    public static final int TYPE_ACLR = 20;
138
139    /**
140     * The Constant TYPE_BCLR indicates color spaces with 11 components.
141     */
142    public static final int TYPE_BCLR = 21;
143
144    /**
145     * The Constant TYPE_CCLR indicates color spaces with 12 components.
146     */
147    public static final int TYPE_CCLR = 22;
148
149    /**
150     * The Constant TYPE_DCLR indicates color spaces with 13 components.
151     */
152    public static final int TYPE_DCLR = 23;
153
154    /**
155     * The Constant TYPE_ECLR indicates color spaces with 14 components.
156     */
157    public static final int TYPE_ECLR = 24;
158
159    /**
160     * The Constant TYPE_FCLR indicates color spaces with 15 components.
161     */
162    public static final int TYPE_FCLR = 25;
163
164    /**
165     * The Constant CS_sRGB indicates standard RGB color space.
166     */
167    public static final int CS_sRGB = 1000;
168
169    /**
170     * The Constant CS_LINEAR_RGB indicates linear RGB color space.
171     */
172    public static final int CS_LINEAR_RGB = 1004;
173
174    /**
175     * The Constant CS_CIEXYZ indicates CIEXYZ conversion color space.
176     */
177    public static final int CS_CIEXYZ = 1001;
178
179    /**
180     * The Constant CS_PYCC indicates Photo YCC conversion color space.
181     */
182    public static final int CS_PYCC = 1002;
183
184    /**
185     * The Constant CS_GRAY indicates linear gray scale color space.
186     */
187    public static final int CS_GRAY = 1003;
188
189    /**
190     * The cs_ gray.
191     */
192    private static ColorSpace cs_Gray = null;
193
194    /**
195     * The cs_ pycc.
196     */
197    private static ColorSpace cs_PYCC = null;
198
199    /**
200     * The cs_ ciexyz.
201     */
202    private static ColorSpace cs_CIEXYZ = null;
203
204    /**
205     * The cs_ lrgb.
206     */
207    private static ColorSpace cs_LRGB = null;
208
209    /**
210     * The cs_s rgb.
211     */
212    private static ColorSpace cs_sRGB = null;
213
214    /**
215     * The type.
216     */
217    private int type;
218
219    /**
220     * The num components.
221     */
222    private int numComponents;
223
224    /**
225     * Instantiates a ColorSpace with the specified ColorSpace type and number
226     * of components.
227     *
228     * @param type
229     *            the type of color space.
230     * @param numcomponents
231     *            the number of components.
232     */
233    protected ColorSpace(int type, int numcomponents) {
234        this.numComponents = numcomponents;
235        this.type = type;
236    }
237
238    /**
239     * Gets the name of the component for the specified component index.
240     *
241     * @param idx
242     *            the index of the component.
243     * @return the name of the component.
244     */
245    public String getName(int idx) {
246        if (idx < 0 || idx > numComponents - 1) {
247            // awt.16A=Invalid component index: {0}
248            throw new IllegalArgumentException(Messages.getString("awt.16A", idx)); //$NON-NLS-1$
249        }
250
251      return "Unnamed color component #" + idx; //$NON-NLS-1$
252    }
253
254    /**
255     * Performs the transformation of a color from this ColorSpace into the RGB
256     * color space.
257     *
258     * @param colorvalue
259     *            the color value in this ColorSpace.
260     * @return the float array with color components in the RGB color space.
261     */
262    public abstract float[] toRGB(float[] colorvalue);
263
264    /**
265     * Performs the transformation of a color from this ColorSpace into the
266     * CS_CIEXYZ color space.
267     *
268     * @param colorvalue
269     *            the color value in this ColorSpace.
270     * @return the float array with color components in the CS_CIEXYZ color
271     *         space.
272     */
273    public abstract float[] toCIEXYZ(float[] colorvalue);
274
275    /**
276     * Performs the transformation of a color from the RGB color space into this
277     * ColorSpace.
278     *
279     * @param rgbvalue
280     *            the float array representing a color in the RGB color space.
281     * @return the float array with the transformed color components.
282     */
283    public abstract float[] fromRGB(float[] rgbvalue);
284
285    /**
286     * Performs the transformation of a color from the CS_CIEXYZ color space
287     * into this ColorSpace.
288     *
289     * @param colorvalue
290     *            the float array representing a color in the CS_CIEXYZ color
291     *            space.
292     * @return the float array with the transformed color components.
293     */
294    public abstract float[] fromCIEXYZ(float[] colorvalue);
295
296    /**
297     * Gets the minimum normalized color component value for the specified
298     * component.
299     *
300     * @param component
301     *            the component to determine the minimum value.
302     * @return the minimum normalized value of the component.
303     */
304    public float getMinValue(int component) {
305        if (component < 0 || component > numComponents - 1) {
306            // awt.16A=Invalid component index: {0}
307            throw new IllegalArgumentException(Messages.getString("awt.16A", component)); //$NON-NLS-1$
308        }
309        return 0;
310    }
311
312    /**
313     * Gets the maximum normalized color component value for the specified
314     * component.
315     *
316     * @param component
317     *            the component to determine the maximum value.
318     * @return the maximum normalized value of the component.
319     */
320    public float getMaxValue(int component) {
321        if (component < 0 || component > numComponents - 1) {
322            // awt.16A=Invalid component index: {0}
323            throw new IllegalArgumentException(Messages.getString("awt.16A", component)); //$NON-NLS-1$
324        }
325        return 1;
326    }
327
328    /**
329     * Checks if this ColorSpace has CS_sRGB type or not.
330     *
331     * @return true, if this ColorSpace has CS_sRGB type, false otherwise.
332     */
333    public boolean isCS_sRGB() {
334        // If our color space is sRGB, then cs_sRGB
335        // is already initialized
336        return (this == cs_sRGB);
337    }
338
339    /**
340     * Gets the type of the ColorSpace.
341     *
342     * @return the type of the ColorSpace.
343     */
344    public int getType() {
345        return type;
346    }
347
348    /**
349     * Gets the number of components for this ColorSpace.
350     *
351     * @return the number of components.
352     */
353    public int getNumComponents() {
354        return numComponents;
355    }
356
357
358    /**
359     * Gets the single instance of ColorSpace with the specified ColorSpace:
360     * CS_sRGB, CS_LINEAR_RGB, CS_CIEXYZ, CS_GRAY, or CS_PYCC.
361     *
362     * @param colorspace
363     *            the identifier of the specified Colorspace.
364     * @return the single instance of the desired ColorSpace.
365     */
366    public static ColorSpace getInstance(int colorspace) {
367        switch (colorspace) {
368            case CS_sRGB:
369                if (cs_sRGB == null) {
370                    cs_sRGB = new ICC_ColorSpace(
371                            new ICC_ProfileStub(CS_sRGB));
372                    LUTColorConverter.sRGB_CS = cs_sRGB;
373                            //ICC_Profile.getInstance (CS_sRGB));
374                }
375                return cs_sRGB;
376            case CS_CIEXYZ:
377                if (cs_CIEXYZ == null) {
378                    cs_CIEXYZ = new ICC_ColorSpace(
379                            new ICC_ProfileStub(CS_CIEXYZ));
380                            //ICC_Profile.getInstance (CS_CIEXYZ));
381                }
382                return cs_CIEXYZ;
383            case CS_GRAY:
384                if (cs_Gray == null) {
385                    cs_Gray = new ICC_ColorSpace(
386                            new ICC_ProfileStub(CS_GRAY));
387                    LUTColorConverter.LINEAR_GRAY_CS = cs_Gray;
388                            //ICC_Profile.getInstance (CS_GRAY));
389                }
390                return cs_Gray;
391            case CS_PYCC:
392                if (cs_PYCC == null) {
393                    cs_PYCC = new ICC_ColorSpace(
394                            new ICC_ProfileStub(CS_PYCC));
395                            //ICC_Profile.getInstance (CS_PYCC));
396                }
397                return cs_PYCC;
398            case CS_LINEAR_RGB:
399                if (cs_LRGB == null) {
400                    cs_LRGB = new ICC_ColorSpace(
401                            new ICC_ProfileStub(CS_LINEAR_RGB));
402                    LUTColorConverter.LINEAR_GRAY_CS = cs_Gray;
403                            //ICC_Profile.getInstance (CS_LINEAR_RGB));
404                }
405                return cs_LRGB;
406            default:
407        }
408
409        // Unknown argument passed
410        // awt.16B=Not a predefined colorspace
411        throw new IllegalArgumentException(Messages.getString("Not a predefined colorspace")); //$NON-NLS-1$
412    }
413
414}