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