001: /*
002: * @(#)ColorSpace.java 1.8 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program 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 version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt.color;
029:
030: public abstract class ColorSpace implements java.io.Serializable {
031: static final long serialVersionUID = -409452704308689724L;
032: private int type;
033: private int numComponents;
034: // Cache of singletons for the predefined color spaces.
035: private static ColorSpace sRGBspace;
036: /**
037: * Any of the family of RGB color spaces.
038: */
039: public static final int TYPE_RGB = 5;
040: /**
041: * The sRGB color space defined at
042: * <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
043: * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html
044: * </A>.
045: */
046: public static final int CS_sRGB = 1000;
047:
048: /**
049: * Constructs a ColorSpace object given a color space type
050: * and the number of components.
051: * @param type One of the <CODE>ColorSpace</CODE> type constants.
052: * @param numcomponents The number of components in the color space.
053: */
054: protected ColorSpace(int type, int numcomponents) {
055: this .type = type;
056: this .numComponents = numcomponents;
057: }
058:
059: /**
060: * Returns a ColorSpace representing one of the specific
061: * predefined color spaces.
062: * @param colorspace a specific color space identified by one of
063: * the predefined class constants (e.g. CS_sRGB, CS_LINEAR_RGB,
064: * CS_CIEXYZ, CS_GRAY, or CS_PYCC)
065: * @return The requested <CODE>ColorSpace</CODE> object.
066: */
067: // NOTE: This method may be called by privileged threads.
068: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
069: public static ColorSpace getInstance(int colorspace) {
070: ColorSpace theColorSpace;
071: switch (colorspace) {
072: case CS_sRGB:
073: if (sRGBspace == null) {
074: sRGBspace = new RGBColorSpace();
075: }
076: theColorSpace = sRGBspace;
077: break;
078:
079: default:
080: throw new IllegalArgumentException("Unknown color space");
081: }
082: return theColorSpace;
083: }
084:
085: /**
086: * Returns the name of the component given the component index.
087: * @param idx The component index.
088: * @return The name of the component at the specified index.
089: */
090: public String getName(int idx) {
091: /* TODO - handle common cases here */
092: return new String("Unnamed color component(" + idx + ")");
093: }
094:
095: /**
096: * Returns the number of components of this ColorSpace.
097: * @return The number of components in this <CODE>ColorSpace</CODE>.
098: */
099: public int getNumComponents() {
100: return numComponents;
101: }
102:
103: /**
104: * Returns the color space type of this ColorSpace (for example
105: * TYPE_RGB, TYPE_XYZ, ...). The type defines the
106: * number of components of the color space and the interpretation,
107: * e.g. TYPE_RGB identifies a color space with three components - red,
108: * green, and blue. It does not define the particular color
109: * characteristics of the space, e.g. the chromaticities of the
110: * primaries.
111: * @return The type constant that represents the type of this
112: * <CODE>ColorSpace</CODE>.
113: */
114: public int getType() {
115: return type;
116: }
117:
118: /**
119: * Returns true if the ColorSpace is CS_sRGB.
120: * @return <CODE>true</CODE> if this is a <CODE>CS_sRGB</CODE> color
121: * space, <code>false</code> if it is not.
122: */
123: public boolean isCS_sRGB() {
124: /* NOTE - make sure we know sRGBspace exists already */
125: return (this == sRGBspace);
126: }
127: }
128:
129: class RGBColorSpace extends ColorSpace {
130: public RGBColorSpace() {
131: super (TYPE_RGB, 3);
132: }
133: }
|