1. /*
  2. * @(#)ColorSpace.java 1.28 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. /**********************************************************************
  11. **********************************************************************
  12. **********************************************************************
  13. *** COPYRIGHT (c) Eastman Kodak Company, 1997 ***
  14. *** As an unpublished work pursuant to Title 17 of the United ***
  15. *** States Code. All rights reserved. ***
  16. **********************************************************************
  17. **********************************************************************
  18. **********************************************************************/
  19. package java.awt.color;
  20. /**
  21. * This abstract class is used to serve as a color space tag to identify the
  22. * specific color space of a Color object or, via a ColorModel object,
  23. * of an Image, a BufferedImage, or a GraphicsDevice. It contains
  24. * methods that transform Colors in a specific color space to/from sRGB
  25. * and to/from a well-defined CIEXYZ color space.
  26. * <p>Several variables are defined for purposes of referring to color
  27. * space types (e.g. TYPE_RGB, TYPE_XYZ, etc.) and to refer to specific
  28. * color spaces (e.g. CS_sRGB and CS_CIEXYZ).
  29. * sRGB is a proposed standard RGB color space. For more information,
  30. * see <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
  31. * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html
  32. * </A>.
  33. * <p>The purpose of the methods to transform to/from the well-defined
  34. * CIEXYZ color space is to support conversions between any two color
  35. * spaces at a reasonably high degree of accuracy. It is expected that
  36. * particular implementations of subclasses of ColorSpace (e.g.
  37. * ICC_ColorSpace) will support high performance conversion based on
  38. * underlying platform color management systems.
  39. * <p>The CS_CIEXYZ space used by the toCIEXYZ/fromCIEXYZ methods can be
  40. * described as follows:
  41. <pre>
  42.   CIEXYZ
  43.   viewing illuminance: 200 lux
  44.   viewing white point: CIE D50
  45.   media white point: "that of a perfectly reflecting diffuser" -- D50
  46.   media black point: 0 lux or 0 Reflectance
  47.   flare: 1 percent
  48.   surround: 20percent of the media white point
  49.   media description: reflection print (i.e., RLAB, Hunt viewing media)
  50.   note: For developers creating an ICC profile for this conversion
  51.   space, the following is applicable. Use a simple Von Kries
  52.   white point adaptation folded into the 3X3 matrix parameters
  53.   and fold the flare and surround effects into the three
  54.   one-dimensional lookup tables (assuming one uses the minimal
  55.   model for monitors).
  56. </pre>
  57. *
  58. * <p>
  59. * @see ICC_ColorSpace
  60. * @version 10 Feb 1997
  61. */
  62. public abstract class ColorSpace implements java.io.Serializable {
  63. private int type;
  64. private int numComponents;
  65. // Cache of singletons for the predefined color spaces.
  66. private static ColorSpace sRGBspace;
  67. private static ColorSpace XYZspace;
  68. private static ColorSpace PYCCspace;
  69. private static ColorSpace GRAYspace;
  70. private static ColorSpace LINEAR_RGBspace;
  71. /**
  72. * Any of the family of XYZ color spaces.
  73. */
  74. public static final int TYPE_XYZ = 0;
  75. /**
  76. * Any of the family of Lab color spaces.
  77. */
  78. public static final int TYPE_Lab = 1;
  79. /**
  80. * Any of the family of Luv color spaces.
  81. */
  82. public static final int TYPE_Luv = 2;
  83. /**
  84. * Any of the family of YCbCr color spaces.
  85. */
  86. public static final int TYPE_YCbCr = 3;
  87. /**
  88. * Any of the family of Yxy color spaces.
  89. */
  90. public static final int TYPE_Yxy = 4;
  91. /**
  92. * Any of the family of RGB color spaces.
  93. */
  94. public static final int TYPE_RGB = 5;
  95. /**
  96. * Any of the family of GRAY color spaces.
  97. */
  98. public static final int TYPE_GRAY = 6;
  99. /**
  100. * Any of the family of HSV color spaces.
  101. */
  102. public static final int TYPE_HSV = 7;
  103. /**
  104. * Any of the family of HLS color spaces.
  105. */
  106. public static final int TYPE_HLS = 8;
  107. /**
  108. * Any of the family of CMYK color spaces.
  109. */
  110. public static final int TYPE_CMYK = 9;
  111. /**
  112. * Any of the family of CMY color spaces.
  113. */
  114. public static final int TYPE_CMY = 11;
  115. /**
  116. * Generic 2 component color spaces.
  117. */
  118. public static final int TYPE_2CLR = 12;
  119. /**
  120. * Generic 3 component color spaces.
  121. */
  122. public static final int TYPE_3CLR = 13;
  123. /**
  124. * Generic 4 component color spaces.
  125. */
  126. public static final int TYPE_4CLR = 14;
  127. /**
  128. * Generic 5 component color spaces.
  129. */
  130. public static final int TYPE_5CLR = 15;
  131. /**
  132. * Generic 6 component color spaces.
  133. */
  134. public static final int TYPE_6CLR = 16;
  135. /**
  136. * Generic 7 component color spaces.
  137. */
  138. public static final int TYPE_7CLR = 17;
  139. /**
  140. * Generic 8 component color spaces.
  141. */
  142. public static final int TYPE_8CLR = 18;
  143. /**
  144. * Generic 9 component color spaces.
  145. */
  146. public static final int TYPE_9CLR = 19;
  147. /**
  148. * Generic 10 component color spaces.
  149. */
  150. public static final int TYPE_ACLR = 20;
  151. /**
  152. * Generic 11 component color spaces.
  153. */
  154. public static final int TYPE_BCLR = 21;
  155. /**
  156. * Generic 12 component color spaces.
  157. */
  158. public static final int TYPE_CCLR = 22;
  159. /**
  160. * Generic 13 component color spaces.
  161. */
  162. public static final int TYPE_DCLR = 23;
  163. /**
  164. * Generic 14 component color spaces.
  165. */
  166. public static final int TYPE_ECLR = 24;
  167. /**
  168. * Generic 15 component color spaces.
  169. */
  170. public static final int TYPE_FCLR = 25;
  171. /**
  172. * The sRGB color space defined at
  173. * <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
  174. * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html
  175. * </A>.
  176. */
  177. public static final int CS_sRGB = 1000;
  178. /**
  179. * A built-in linear RGB color space. This space is based on the
  180. * same RGB primaries as CS_sRGB, but has a linear tone reproduction curve.
  181. */
  182. public static final int CS_LINEAR_RGB = 1004;
  183. /**
  184. * The CIEXYZ conversion color space defined above.
  185. */
  186. public static final int CS_CIEXYZ = 1001;
  187. /**
  188. * The Photo YCC conversion color space.
  189. */
  190. public static final int CS_PYCC = 1002;
  191. /**
  192. * The built-in linear gray scale color space.
  193. */
  194. public static final int CS_GRAY = 1003;
  195. /**
  196. * Constructs a ColorSpace object given a color space type
  197. * and the number of components.
  198. */
  199. protected ColorSpace (int type, int numcomponents) {
  200. this.type = type;
  201. this.numComponents = numcomponents;
  202. }
  203. /**
  204. * Returns a ColorSpace representing one of the specific
  205. * predefined color spaces.
  206. * @param colorspace a specific color space identified by one of
  207. * the predefined class constants (e.g. CS_sRGB, CS_LINEAR_RGB,
  208. * CS_CIEXYZ, CS_GRAY, or CS_PYCC)
  209. */
  210. // NOTE: This method may be called by privileged threads.
  211. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  212. public static ColorSpace getInstance (int colorspace)
  213. {
  214. ColorSpace theColorSpace;
  215. switch (colorspace) {
  216. case CS_sRGB:
  217. if (sRGBspace == null) {
  218. ICC_Profile theProfile = ICC_Profile.getInstance (CS_sRGB);
  219. sRGBspace = new ICC_ColorSpace (theProfile);
  220. }
  221. theColorSpace = sRGBspace;
  222. break;
  223. case CS_CIEXYZ:
  224. if (XYZspace == null) {
  225. ICC_Profile theProfile = ICC_Profile.getInstance (CS_CIEXYZ);
  226. XYZspace = new ICC_ColorSpace (theProfile);
  227. }
  228. theColorSpace = XYZspace;
  229. break;
  230. case CS_PYCC:
  231. if (PYCCspace == null) {
  232. ICC_Profile theProfile = ICC_Profile.getInstance (CS_PYCC);
  233. PYCCspace = new ICC_ColorSpace (theProfile);
  234. }
  235. theColorSpace = PYCCspace;
  236. break;
  237. case CS_GRAY:
  238. if (GRAYspace == null) {
  239. ICC_Profile theProfile = ICC_Profile.getInstance (CS_GRAY);
  240. GRAYspace = new ICC_ColorSpace (theProfile);
  241. }
  242. theColorSpace = GRAYspace;
  243. break;
  244. case CS_LINEAR_RGB:
  245. if (LINEAR_RGBspace == null) {
  246. ICC_Profile theProfile = ICC_Profile.getInstance(CS_LINEAR_RGB);
  247. LINEAR_RGBspace = new ICC_ColorSpace (theProfile);
  248. }
  249. theColorSpace = LINEAR_RGBspace;
  250. break;
  251. default:
  252. throw new IllegalArgumentException ("Unknown color space");
  253. }
  254. return theColorSpace;
  255. }
  256. /**
  257. * Returns true if the ColorSpace is CS_sRGB.
  258. */
  259. public boolean isCS_sRGB () {
  260. /* REMIND - make sure we know sRGBspace exists already */
  261. return (this == sRGBspace);
  262. }
  263. /**
  264. * Transforms a color value assumed to be in this ColorSpace
  265. * into a value in the default CS_sRGB color space.
  266. * <p>
  267. * This method transforms color values using algorithms designed
  268. * to produce the best perceptual match between input and output
  269. * colors. In order to do colorimetric conversion of color values,
  270. * you should use the <code>toCIEXYZ</code>
  271. * method of this color space to first convert from the input
  272. * color space to the CS_CIEXYZ color space, and then use the
  273. * <code>fromCIEXYZ</code> method of the CS_sRGB color space to
  274. * convert from CS_CIEXYZ to the output color space.
  275. * See {@link #toCIEXYZ(float[]) toCIEXYZ} and
  276. * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information.
  277. * <p>
  278. * @param colorvalue a float array with length of at least the number
  279. * of components in this ColorSpace
  280. * @return a float array of length 3
  281. */
  282. public abstract float[] toRGB(float[] colorvalue);
  283. /**
  284. * Transforms a color value assumed to be in the default CS_sRGB
  285. * color space into this ColorSpace.
  286. * <p>
  287. * This method transforms color values using algorithms designed
  288. * to produce the best perceptual match between input and output
  289. * colors. In order to do colorimetric conversion of color values,
  290. * you should use the <code>toCIEXYZ</code>
  291. * method of the CS_sRGB color space to first convert from the input
  292. * color space to the CS_CIEXYZ color space, and then use the
  293. * <code>fromCIEXYZ</code> method of this color space to
  294. * convert from CS_CIEXYZ to the output color space.
  295. * See {@link #toCIEXYZ(float[]) toCIEXYZ} and
  296. * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information.
  297. * <p>
  298. * @param rgbvalue a float array with length of at least 3
  299. * @return a float array with length equal to the number of
  300. * components in this ColorSpace
  301. */
  302. public abstract float[] fromRGB(float[] rgbvalue);
  303. /**
  304. * Transforms a color value assumed to be in this ColorSpace
  305. * into the CS_CIEXYZ conversion color space.
  306. * <p>
  307. * This method transforms color values using relative colorimetry,
  308. * as defined by the International Color Consortium standard. This
  309. * means that the XYZ values returned by this method are represented
  310. * relative to the D50 white point of the CS_CIEXYZ color space.
  311. * This representation is useful in a two-step color conversion
  312. * process in which colors are transformed from an input color
  313. * space to CS_CIEXYZ and then to an output color space. This
  314. * representation is not the same as the XYZ values that would
  315. * be measured from the given color value by a colorimeter.
  316. * A further transformation is necessary to compute the XYZ values
  317. * that would be measured using current CIE recommended practices.
  318. * See the {@link ICC_ColorSpace#toCIEXYZ(float[]) toCIEXYZ} method of
  319. * <code>ICC_ColorSpace</code> for further information.
  320. * <p>
  321. * @param colorvalue a float array with length of at least the number
  322. * of components in this ColorSpace
  323. * @return a float array of length 3
  324. */
  325. public abstract float[] toCIEXYZ(float[] colorvalue);
  326. /**
  327. * Transforms a color value assumed to be in the CS_CIEXYZ conversion
  328. * color space into this ColorSpace.
  329. * <p>
  330. * This method transforms color values using relative colorimetry,
  331. * as defined by the International Color Consortium standard. This
  332. * means that the XYZ argument values taken by this method are represented
  333. * relative to the D50 white point of the CS_CIEXYZ color space.
  334. * This representation is useful in a two-step color conversion
  335. * process in which colors are transformed from an input color
  336. * space to CS_CIEXYZ and then to an output color space. The color
  337. * values returned by this method are not those that would produce
  338. * the XYZ value passed to the method when measured by a colorimeter.
  339. * If you have XYZ values corresponding to measurements made using
  340. * current CIE recommended practices, they must be converted to D50
  341. * relative values before being passed to this method.
  342. * See the {@link ICC_ColorSpace#fromCIEXYZ(float[]) fromCIEXYZ} method of
  343. * <code>ICC_ColorSpace</code> for further information.
  344. * <p>
  345. * @param colorvalue a float array with length of at least 3
  346. * @return a float array with length equal to the number of
  347. * components in this ColorSpace
  348. */
  349. public abstract float[] fromCIEXYZ(float[] colorvalue);
  350. /**
  351. * Returns the color space type of this ColorSpace (for example
  352. * TYPE_RGB, TYPE_XYZ, ...). The type defines the
  353. * number of components of the color space and the interpretation,
  354. * e.g. TYPE_RGB identifies a color space with three components - red,
  355. * green, and blue. It does not define the particular color
  356. * characteristics of the space, e.g. the chromaticities of the
  357. * primaries.
  358. */
  359. public int getType() {
  360. return type;
  361. }
  362. /**
  363. * Returns the number of components of this ColorSpace.
  364. */
  365. public int getNumComponents() {
  366. return numComponents;
  367. }
  368. /**
  369. * Returns the name of the component given the component index
  370. */
  371. public String getName (int idx) {
  372. /* REMIND - handle common cases here */
  373. return new String("Unnamed color component("+idx+")");
  374. }
  375. /* Returns true if cspace is the XYZspace.
  376. */
  377. static boolean isCS_CIEXYZ(ColorSpace cspace) {
  378. return (cspace == XYZspace);
  379. }
  380. }