001    /* ColorSpace.java -- transforms between color spaces
002       Copyright (C) 2000, 2002 Free Software Foundation
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.awt.color;
040    
041    import java.io.Serializable;
042    
043    /**
044     * NEEDS DOCUMENTATION
045     *
046     * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
047     * @since 1.2
048     */
049    public abstract class ColorSpace implements Serializable
050    {
051      /**
052       * Compatible with JDK 1.2+.
053       */
054      private static final long serialVersionUID = -409452704308689724L;
055    
056      public static final int TYPE_XYZ = 0;
057      public static final int TYPE_Lab = 1;
058      public static final int TYPE_Luv = 2;
059      public static final int TYPE_YCbCr = 3;
060      public static final int TYPE_Yxy = 4;
061      public static final int TYPE_RGB = 5;
062      public static final int TYPE_GRAY = 6;
063      public static final int TYPE_HSV = 7;
064      public static final int TYPE_HLS = 8;
065      public static final int TYPE_CMYK = 9;
066      // mysterious gap in the enumeration sequenece
067      public static final int TYPE_CMY = 11;
068      public static final int TYPE_2CLR = 12;
069      public static final int TYPE_3CLR = 13;
070      public static final int TYPE_4CLR = 14;
071      public static final int TYPE_5CLR = 15;
072      public static final int TYPE_6CLR = 16;
073      public static final int TYPE_7CLR = 17;
074      public static final int TYPE_8CLR = 18;
075      public static final int TYPE_9CLR = 19;
076      public static final int TYPE_ACLR = 20;
077      public static final int TYPE_BCLR = 21;
078      public static final int TYPE_CCLR = 22;
079      public static final int TYPE_DCLR = 23;
080      public static final int TYPE_ECLR = 24;
081      public static final int TYPE_FCLR = 25;
082    
083      public static final int CS_sRGB = 1000;
084      public static final int CS_LINEAR_RGB = 1004;
085      public static final int CS_CIEXYZ = 1001;
086      public static final int CS_PYCC = 1002;
087      public static final int CS_GRAY = 1003;
088    
089      private static final int CS_BASE = CS_sRGB;
090      private static final int CS_END = CS_LINEAR_RGB + 1;
091      private static final int CS_COUNT = CS_END - CS_BASE;
092    
093      // Instances are lazily instantiated
094      private static final ColorSpace[] INSTANCES = new ColorSpace[CS_COUNT];
095    
096      /**
097       * @serial
098       */
099      // Visible in subclass.
100      final int type;
101    
102      /**
103       * @serial
104       */
105      // Visible in subclass.
106      final int numComponents;
107    
108      protected ColorSpace(int type, int numcomponents)
109      {
110        this.type = type;
111        numComponents = numcomponents;
112      }
113    
114      public static ColorSpace getInstance(int colorspace)
115      {
116        if ((colorspace >= CS_BASE) && (colorspace < CS_END))
117          {
118            int instanceIndex = colorspace - CS_BASE;
119            if (INSTANCES[instanceIndex] == null)
120              {
121                ICC_Profile profile = new ICC_Profile(colorspace);
122                INSTANCES[instanceIndex] = new ICC_ColorSpace(profile);
123              }
124            return INSTANCES[instanceIndex];
125          }
126        throw new IllegalArgumentException("unknown/unsupported colorspace");
127      }
128    
129      public boolean isCS_sRGB()
130      {
131        return type == CS_sRGB;
132      }
133    
134      /**
135       * Transforms a color value assumed to be in this ColorSpace into a value in
136       * the default CS_sRGB color space.
137       *
138       * @exception ArrayIndexOutOfBoundsException If array length is not at least
139       * the number of components in this ColorSpace.
140       */
141      public abstract float[] toRGB(float[] colorvalue);
142    
143      public abstract float[] fromRGB(float[] rgbvalue);
144    
145      public abstract float[] toCIEXYZ(float[] colorvalue);
146    
147      public abstract float[] fromCIEXYZ(float[] colorvalue);
148    
149      public int getType()
150      {
151        return type;
152      }
153    
154      public int getNumComponents()
155      {
156        return numComponents;
157      }
158    
159      public String getName(int idx)
160      {
161        return "type " + type;
162      }
163    
164      /**
165       * @since 1.4
166       */
167      public float getMinValue(int idx)
168      {
169        if (idx < 0 || idx >= numComponents)
170          throw new IllegalArgumentException();
171        return 0;
172      }
173    
174      /**
175       * @since 1.4
176       */
177      public float getMaxValue(int idx)
178      {
179        if (idx < 0 || idx >= numComponents)
180          throw new IllegalArgumentException();
181        return 1;
182      }
183    } // class ColorSpace