1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 ********************************************************************** 28 ********************************************************************** 29 ********************************************************************** 30 *** COPYRIGHT (c) Eastman Kodak Company, 1997 *** 31 *** As an unpublished work pursuant to Title 17 of the United *** 32 *** States Code. All rights reserved. *** 33 ********************************************************************** 34 ********************************************************************** 35 **********************************************************************/ 36 37 package java.awt.color; 38 39 import java.lang.annotation.Native; 40 41 import sun.java2d.cmm.PCMM; 42 import sun.java2d.cmm.CMSManager; 43 44 45 /** 46 * This abstract class is used to serve as a color space tag to identify the 47 * specific color space of a Color object or, via a ColorModel object, 48 * of an Image, a BufferedImage, or a GraphicsDevice. It contains 49 * methods that transform colors in a specific color space to/from sRGB 50 * and to/from a well-defined CIEXYZ color space. 51 * <p> 52 * For purposes of the methods in this class, colors are represented as 53 * arrays of color components represented as floats in a normalized range 54 * defined by each ColorSpace. For many ColorSpaces (e.g. sRGB), this 55 * range is 0.0 to 1.0. However, some ColorSpaces have components whose 56 * values have a different range. Methods are provided to inquire per 57 * component minimum and maximum normalized values. 58 * <p> 59 * Several variables are defined for purposes of referring to color 60 * space types (e.g. TYPE_RGB, TYPE_XYZ, etc.) and to refer to specific 61 * color spaces (e.g. CS_sRGB and CS_CIEXYZ). 62 * sRGB is a proposed standard RGB color space. For more information, 63 * see <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> 64 * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html 65 * </A>. 66 * <p> 67 * The purpose of the methods to transform to/from the well-defined 68 * CIEXYZ color space is to support conversions between any two color 69 * spaces at a reasonably high degree of accuracy. It is expected that 70 * particular implementations of subclasses of ColorSpace (e.g. 71 * ICC_ColorSpace) will support high performance conversion based on 72 * underlying platform color management systems. 73 * <p> 74 * The CS_CIEXYZ space used by the toCIEXYZ/fromCIEXYZ methods can be 75 * described as follows: 76 <pre> 77 78 CIEXYZ 79 viewing illuminance: 200 lux 80 viewing white point: CIE D50 81 media white point: "that of a perfectly reflecting diffuser" -- D50 82 media black point: 0 lux or 0 Reflectance 83 flare: 1 percent 84 surround: 20percent of the media white point 85 media description: reflection print (i.e., RLAB, Hunt viewing media) 86 note: For developers creating an ICC profile for this conversion 87 space, the following is applicable. Use a simple Von Kries 88 white point adaptation folded into the 3X3 matrix parameters 89 and fold the flare and surround effects into the three 90 one-dimensional lookup tables (assuming one uses the minimal 91 model for monitors). 92 93 </pre> 94 * 95 * @see ICC_ColorSpace 96 */ 97 98 public abstract class ColorSpace implements java.io.Serializable { 99 100 static final long serialVersionUID = -409452704308689724L; 101 102 private int type; 103 private int numComponents; 104 private transient String [] compName = null; 105 106 // Cache of singletons for the predefined color spaces. 107 private static ColorSpace sRGBspace; 108 private static ColorSpace XYZspace; 109 private static ColorSpace PYCCspace; 110 private static ColorSpace GRAYspace; 111 private static ColorSpace LINEAR_RGBspace; 112 113 /** 114 * Any of the family of XYZ color spaces. 115 */ 116 @Native public static final int TYPE_XYZ = 0; 117 220 */ 221 @Native public static final int TYPE_CCLR = 22; 222 223 /** 224 * Generic 13 component color spaces. 225 */ 226 @Native public static final int TYPE_DCLR = 23; 227 228 /** 229 * Generic 14 component color spaces. 230 */ 231 @Native public static final int TYPE_ECLR = 24; 232 233 /** 234 * Generic 15 component color spaces. 235 */ 236 @Native public static final int TYPE_FCLR = 25; 237 238 /** 239 * The sRGB color space defined at 240 * <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> 241 * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html 242 * </A>. 243 */ 244 @Native public static final int CS_sRGB = 1000; 245 246 /** 247 * A built-in linear RGB color space. This space is based on the 248 * same RGB primaries as CS_sRGB, but has a linear tone reproduction curve. 249 */ 250 @Native public static final int CS_LINEAR_RGB = 1004; 251 252 /** 253 * The CIEXYZ conversion color space defined above. 254 */ 255 @Native public static final int CS_CIEXYZ = 1001; 256 257 /** 258 * The Photo YCC conversion color space. 259 */ 260 @Native public static final int CS_PYCC = 1002; 261 262 /** 263 * The built-in linear gray scale color space. 264 */ 265 @Native public static final int CS_GRAY = 1003; 266 267 268 /** 269 * Constructs a ColorSpace object given a color space type 270 * and the number of components. 271 * @param type one of the {@code ColorSpace} type constants 272 * @param numcomponents the number of components in the color space 273 */ 274 protected ColorSpace (int type, int numcomponents) { 275 this.type = type; 276 this.numComponents = numcomponents; 277 } 278 279 280 /** 281 * Returns a ColorSpace representing one of the specific 282 * predefined color spaces. 283 * @param colorspace a specific color space identified by one of 284 * the predefined class constants (e.g. CS_sRGB, CS_LINEAR_RGB, 285 * CS_CIEXYZ, CS_GRAY, or CS_PYCC) 286 * @return the requested {@code ColorSpace} object 287 */ 288 // NOTE: This method may be called by privileged threads. 289 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! 290 public static ColorSpace getInstance (int colorspace) 291 { 292 ColorSpace theColorSpace; 293 294 switch (colorspace) { 295 case CS_sRGB: 296 synchronized(ColorSpace.class) { 297 if (sRGBspace == null) { 298 ICC_Profile theProfile = ICC_Profile.getInstance (CS_sRGB); 299 sRGBspace = new ICC_ColorSpace (theProfile); 300 } 301 302 theColorSpace = sRGBspace; 303 } 304 break; 305 346 if (LINEAR_RGBspace == null) { 347 ICC_Profile theProfile = 348 ICC_Profile.getInstance(CS_LINEAR_RGB); 349 LINEAR_RGBspace = new ICC_ColorSpace (theProfile); 350 /* to allow access from java.awt.ColorModel */ 351 CMSManager.LINEAR_RGBspace = LINEAR_RGBspace; 352 } 353 354 theColorSpace = LINEAR_RGBspace; 355 } 356 break; 357 358 359 default: 360 throw new IllegalArgumentException ("Unknown color space"); 361 } 362 363 return theColorSpace; 364 } 365 366 367 /** 368 * Returns true if the ColorSpace is CS_sRGB. 369 * @return {@code true} if this is a {@code CS_sRGB} color 370 * space, {@code false} if it is not 371 */ 372 public boolean isCS_sRGB () { 373 /* REMIND - make sure we know sRGBspace exists already */ 374 return (this == sRGBspace); 375 } 376 377 /** 378 * Transforms a color value assumed to be in this ColorSpace 379 * into a value in the default CS_sRGB color space. 380 * <p> 381 * This method transforms color values using algorithms designed 382 * to produce the best perceptual match between input and output 383 * colors. In order to do colorimetric conversion of color values, 384 * you should use the {@code toCIEXYZ} 385 * method of this color space to first convert from the input 386 * color space to the CS_CIEXYZ color space, and then use the 387 * {@code fromCIEXYZ} method of the CS_sRGB color space to 388 * convert from CS_CIEXYZ to the output color space. 389 * See {@link #toCIEXYZ(float[]) toCIEXYZ} and 390 * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information. 391 * 392 * @param colorvalue a float array with length of at least the number 393 * of components in this ColorSpace 394 * @return a float array of length 3 395 * @throws ArrayIndexOutOfBoundsException if array length is not 396 * at least the number of components in this ColorSpace 397 */ 398 public abstract float[] toRGB(float[] colorvalue); 399 400 401 /** 402 * Transforms a color value assumed to be in the default CS_sRGB 403 * color space into this ColorSpace. 404 * <p> 405 * This method transforms color values using algorithms designed 406 * to produce the best perceptual match between input and output 407 * colors. In order to do colorimetric conversion of color values, 408 * you should use the {@code toCIEXYZ} 409 * method of the CS_sRGB color space to first convert from the input 410 * color space to the CS_CIEXYZ color space, and then use the 411 * {@code fromCIEXYZ} method of this color space to 412 * convert from CS_CIEXYZ to the output color space. 413 * See {@link #toCIEXYZ(float[]) toCIEXYZ} and 414 * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information. 415 * 416 * @param rgbvalue a float array with length of at least 3 417 * @return a float array with length equal to the number of 418 * components in this ColorSpace 419 * @throws ArrayIndexOutOfBoundsException if array length is not 420 * at least 3 421 */ 422 public abstract float[] fromRGB(float[] rgbvalue); 423 424 425 /** 426 * Transforms a color value assumed to be in this ColorSpace 427 * into the CS_CIEXYZ conversion color space. 428 * <p> 429 * This method transforms color values using relative colorimetry, 430 * as defined by the International Color Consortium standard. This 431 * means that the XYZ values returned by this method are represented 432 * relative to the D50 white point of the CS_CIEXYZ color space. 433 * This representation is useful in a two-step color conversion 434 * process in which colors are transformed from an input color 435 * space to CS_CIEXYZ and then to an output color space. This 436 * representation is not the same as the XYZ values that would 437 * be measured from the given color value by a colorimeter. 438 * A further transformation is necessary to compute the XYZ values 439 * that would be measured using current CIE recommended practices. 440 * See the {@link ICC_ColorSpace#toCIEXYZ(float[]) toCIEXYZ} method of 441 * {@code ICC_ColorSpace} for further information. 442 * 443 * @param colorvalue a float array with length of at least the number 444 * of components in this ColorSpace 445 * @return a float array of length 3 446 * @throws ArrayIndexOutOfBoundsException if array length is not 447 * at least the number of components in this ColorSpace. 448 */ 449 public abstract float[] toCIEXYZ(float[] colorvalue); 450 451 452 /** 453 * Transforms a color value assumed to be in the CS_CIEXYZ conversion 454 * color space into this ColorSpace. 455 * <p> 456 * This method transforms color values using relative colorimetry, 457 * as defined by the International Color Consortium standard. This 458 * means that the XYZ argument values taken by this method are represented 459 * relative to the D50 white point of the CS_CIEXYZ color space. 460 * This representation is useful in a two-step color conversion 461 * process in which colors are transformed from an input color 462 * space to CS_CIEXYZ and then to an output color space. The color 463 * values returned by this method are not those that would produce 464 * the XYZ value passed to the method when measured by a colorimeter. 465 * If you have XYZ values corresponding to measurements made using 466 * current CIE recommended practices, they must be converted to D50 467 * relative values before being passed to this method. 468 * See the {@link ICC_ColorSpace#fromCIEXYZ(float[]) fromCIEXYZ} method of 469 * {@code ICC_ColorSpace} for further information. 470 * 471 * @param colorvalue a float array with length of at least 3 472 * @return a float array with length equal to the number of 473 * components in this ColorSpace 474 * @throws ArrayIndexOutOfBoundsException if array length is not 475 * at least 3 476 */ 477 public abstract float[] fromCIEXYZ(float[] colorvalue); 478 479 /** 480 * Returns the color space type of this ColorSpace (for example 481 * TYPE_RGB, TYPE_XYZ, ...). The type defines the 482 * number of components of the color space and the interpretation, 483 * e.g. TYPE_RGB identifies a color space with three components - red, 484 * green, and blue. It does not define the particular color 485 * characteristics of the space, e.g. the chromaticities of the 486 * primaries. 487 * 488 * @return the type constant that represents the type of this 489 * {@code ColorSpace} 490 */ 491 public int getType() { 492 return type; 493 } 494 495 /** 496 * Returns the number of components of this ColorSpace. 497 * @return The number of components in this {@code ColorSpace}. 498 */ 499 public int getNumComponents() { 500 return numComponents; 501 } 502 503 /** 504 * Returns the name of the component given the component index. 505 * 506 * @param idx the component index 507 * @return the name of the component at the specified index 508 * @throws IllegalArgumentException if {@code idx} is 509 * less than 0 or greater than numComponents - 1 510 */ 511 public String getName (int idx) { 512 /* REMIND - handle common cases here */ 513 if ((idx < 0) || (idx > numComponents - 1)) { 514 throw new IllegalArgumentException( 515 "Component index out of range: " + idx); 516 } 517 518 if (compName == null) { 519 switch (type) { 520 case ColorSpace.TYPE_XYZ: 521 compName = new String[] {"X", "Y", "Z"}; 522 break; 523 case ColorSpace.TYPE_Lab: 524 compName = new String[] {"L", "a", "b"}; 525 break; 526 case ColorSpace.TYPE_Luv: 527 compName = new String[] {"L", "u", "v"}; 528 break; 529 case ColorSpace.TYPE_YCbCr: 547 break; 548 case ColorSpace.TYPE_CMYK: 549 compName = new String[] {"Cyan", "Magenta", "Yellow", 550 "Black"}; 551 break; 552 case ColorSpace.TYPE_CMY: 553 compName = new String[] {"Cyan", "Magenta", "Yellow"}; 554 break; 555 default: 556 String [] tmp = new String[numComponents]; 557 for (int i = 0; i < tmp.length; i++) { 558 tmp[i] = "Unnamed color component(" + i + ")"; 559 } 560 compName = tmp; 561 } 562 } 563 return compName[idx]; 564 } 565 566 /** 567 * Returns the minimum normalized color component value for the 568 * specified component. The default implementation in this abstract 569 * class returns 0.0 for all components. Subclasses should override 570 * this method if necessary. 571 * 572 * @param component the component index 573 * @return the minimum normalized component value 574 * @throws IllegalArgumentException if component is less than 0 or 575 * greater than numComponents - 1 576 * @since 1.4 577 */ 578 public float getMinValue(int component) { 579 if ((component < 0) || (component > numComponents - 1)) { 580 throw new IllegalArgumentException( 581 "Component index out of range: " + component); 582 } 583 return 0.0f; 584 } 585 586 /** 587 * Returns the maximum normalized color component value for the 588 * specified component. The default implementation in this abstract 589 * class returns 1.0 for all components. Subclasses should override 590 * this method if necessary. 591 * 592 * @param component the component index 593 * @return the maximum normalized component value 594 * @throws IllegalArgumentException if component is less than 0 or 595 * greater than numComponents - 1 596 * @since 1.4 597 */ 598 public float getMaxValue(int component) { 599 if ((component < 0) || (component > numComponents - 1)) { 600 throw new IllegalArgumentException( 601 "Component index out of range: " + component); 602 } 603 return 1.0f; 604 } 605 606 /* Returns true if cspace is the XYZspace. 607 */ 608 static boolean isCS_CIEXYZ(ColorSpace cspace) { 609 return (cspace == XYZspace); 610 } 611 } | 1 /* 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* ******************************************************************** 27 ********************************************************************** 28 ********************************************************************** 29 *** COPYRIGHT (c) Eastman Kodak Company, 1997 *** 30 *** As an unpublished work pursuant to Title 17 of the United *** 31 *** States Code. All rights reserved. *** 32 ********************************************************************** 33 ********************************************************************** 34 **********************************************************************/ 35 36 package java.awt.color; 37 38 import java.lang.annotation.Native; 39 40 import sun.java2d.cmm.CMSManager; 41 42 /** 43 * This abstract class is used to serve as a color space tag to identify the 44 * specific color space of a {@code Color} object or, via a {@code ColorModel} 45 * object, of an {@code Image}, a {@code BufferedImage}, or a 46 * {@code GraphicsDevice}. It contains methods that transform colors in a 47 * specific color space to/from sRGB and to/from a well-defined CIEXYZ color 48 * space. 49 * <p> 50 * For purposes of the methods in this class, colors are represented as arrays 51 * of color components represented as floats in a normalized range defined by 52 * each {@code ColorSpace}. For many {@code ColorSpaces} (e.g. sRGB), this range 53 * is 0.0 to 1.0. However, some {@code ColorSpaces} have components whose values 54 * have a different range. Methods are provided to inquire per component minimum 55 * and maximum normalized values. 56 * <p> 57 * Several variables are defined for purposes of referring to color space types 58 * (e.g. {@code TYPE_RGB}, {@code TYPE_XYZ}, etc.) and to refer to specific 59 * color spaces (e.g. {@code CS_sRGB} and {@code CS_CIEXYZ}). sRGB is a proposed 60 * standard RGB color space. For more information, see 61 * <a href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> 62 * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html</a>. 63 * <p> 64 * The purpose of the methods to transform to/from the well-defined CIEXYZ color 65 * space is to support conversions between any two color spaces at a reasonably 66 * high degree of accuracy. It is expected that particular implementations of 67 * subclasses of {@code ColorSpace} (e.g. {@code ICC_ColorSpace}) will support 68 * high performance conversion based on underlying platform color management 69 * systems. 70 * <p> 71 * The {@code CS_CIEXYZ} space used by the {@code toCIEXYZ/fromCIEXYZ} methods 72 * can be described as follows: 73 * <pre> 74 * 75 * CIEXYZ 76 * viewing illuminance: 200 lux 77 * viewing white point: CIE D50 78 * media white point: "that of a perfectly reflecting diffuser" -- D50 79 * media black point: 0 lux or 0 Reflectance 80 * flare: 1 percent 81 * surround: 20percent of the media white point 82 * media description: reflection print (i.e., RLAB, Hunt viewing media) 83 * note: For developers creating an ICC profile for this conversion 84 * space, the following is applicable. Use a simple Von Kries 85 * white point adaptation folded into the 3X3 matrix parameters 86 * and fold the flare and surround effects into the three 87 * one-dimensional lookup tables (assuming one uses the minimal 88 * model for monitors). 89 * 90 * </pre> 91 * 92 * @see ICC_ColorSpace 93 */ 94 public abstract class ColorSpace implements java.io.Serializable { 95 96 static final long serialVersionUID = -409452704308689724L; 97 98 private int type; 99 private int numComponents; 100 private transient String [] compName = null; 101 102 // Cache of singletons for the predefined color spaces. 103 private static ColorSpace sRGBspace; 104 private static ColorSpace XYZspace; 105 private static ColorSpace PYCCspace; 106 private static ColorSpace GRAYspace; 107 private static ColorSpace LINEAR_RGBspace; 108 109 /** 110 * Any of the family of XYZ color spaces. 111 */ 112 @Native public static final int TYPE_XYZ = 0; 113 216 */ 217 @Native public static final int TYPE_CCLR = 22; 218 219 /** 220 * Generic 13 component color spaces. 221 */ 222 @Native public static final int TYPE_DCLR = 23; 223 224 /** 225 * Generic 14 component color spaces. 226 */ 227 @Native public static final int TYPE_ECLR = 24; 228 229 /** 230 * Generic 15 component color spaces. 231 */ 232 @Native public static final int TYPE_FCLR = 25; 233 234 /** 235 * The sRGB color space defined at 236 * <a href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> 237 * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html</a>. 238 */ 239 @Native public static final int CS_sRGB = 1000; 240 241 /** 242 * A built-in linear RGB color space. This space is based on the same RGB 243 * primaries as {@code CS_sRGB}, but has a linear tone reproduction curve. 244 */ 245 @Native public static final int CS_LINEAR_RGB = 1004; 246 247 /** 248 * The CIEXYZ conversion color space defined above. 249 */ 250 @Native public static final int CS_CIEXYZ = 1001; 251 252 /** 253 * The Photo YCC conversion color space. 254 */ 255 @Native public static final int CS_PYCC = 1002; 256 257 /** 258 * The built-in linear gray scale color space. 259 */ 260 @Native public static final int CS_GRAY = 1003; 261 262 /** 263 * Constructs a {@code ColorSpace} object given a color space type and the 264 * number of components. 265 * 266 * @param type one of the {@code ColorSpace} type constants 267 * @param numcomponents the number of components in the color space 268 */ 269 protected ColorSpace(int type, int numcomponents) { 270 this.type = type; 271 this.numComponents = numcomponents; 272 } 273 274 /** 275 * Returns a {@code ColorSpace} representing one of the specific predefined 276 * color spaces. 277 * 278 * @param colorspace a specific color space identified by one of the 279 * predefined class constants (e.g. {@code CS_sRGB}, 280 * {@code CS_LINEAR_RGB}, {@code CS_CIEXYZ}, {@code CS_GRAY}, or 281 * {@code CS_PYCC}) 282 * @return the requested {@code ColorSpace} object 283 */ 284 // NOTE: This method may be called by privileged threads. 285 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! 286 public static ColorSpace getInstance (int colorspace) 287 { 288 ColorSpace theColorSpace; 289 290 switch (colorspace) { 291 case CS_sRGB: 292 synchronized(ColorSpace.class) { 293 if (sRGBspace == null) { 294 ICC_Profile theProfile = ICC_Profile.getInstance (CS_sRGB); 295 sRGBspace = new ICC_ColorSpace (theProfile); 296 } 297 298 theColorSpace = sRGBspace; 299 } 300 break; 301 342 if (LINEAR_RGBspace == null) { 343 ICC_Profile theProfile = 344 ICC_Profile.getInstance(CS_LINEAR_RGB); 345 LINEAR_RGBspace = new ICC_ColorSpace (theProfile); 346 /* to allow access from java.awt.ColorModel */ 347 CMSManager.LINEAR_RGBspace = LINEAR_RGBspace; 348 } 349 350 theColorSpace = LINEAR_RGBspace; 351 } 352 break; 353 354 355 default: 356 throw new IllegalArgumentException ("Unknown color space"); 357 } 358 359 return theColorSpace; 360 } 361 362 /** 363 * Returns true if the {@code ColorSpace} is {@code CS_sRGB}. 364 * 365 * @return {@code true} if this is a {@code CS_sRGB} color space, 366 * {@code false} if it is not 367 */ 368 public boolean isCS_sRGB () { 369 /* REMIND - make sure we know sRGBspace exists already */ 370 return (this == sRGBspace); 371 } 372 373 /** 374 * Transforms a color value assumed to be in this {@code ColorSpace} into a 375 * value in the default {@code CS_sRGB} color space. 376 * <p> 377 * This method transforms color values using algorithms designed to produce 378 * the best perceptual match between input and output colors. In order to do 379 * colorimetric conversion of color values, you should use the 380 * {@code toCIEXYZ} method of this color space to first convert from the 381 * input color space to the CS_CIEXYZ color space, and then use the 382 * {@code fromCIEXYZ} method of the {@code CS_sRGB} color space to convert 383 * from {@code CS_CIEXYZ} to the output color space. See 384 * {@link #toCIEXYZ(float[]) toCIEXYZ} and 385 * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information. 386 * 387 * @param colorvalue a float array with length of at least the number of 388 * components in this {@code ColorSpace} 389 * @return a float array of length 3 390 * @throws ArrayIndexOutOfBoundsException if array length is not at least 391 * the number of components in this {@code ColorSpace} 392 */ 393 public abstract float[] toRGB(float[] colorvalue); 394 395 /** 396 * Transforms a color value assumed to be in the default {@code CS_sRGB} 397 * color space into this {@code ColorSpace}. 398 * <p> 399 * This method transforms color values using algorithms designed to produce 400 * the best perceptual match between input and output colors. In order to do 401 * colorimetric conversion of color values, you should use the 402 * {@code toCIEXYZ} method of the {@code CS_sRGB} color space to first 403 * convert from the input color space to the {@code CS_CIEXYZ} color space, 404 * and then use the {@code fromCIEXYZ} method of this color space to convert 405 * from {@code CS_CIEXYZ} to the output color space. See 406 * {@link #toCIEXYZ(float[]) toCIEXYZ} and 407 * {@link #fromCIEXYZ(float[]) fromCIEXYZ} for further information. 408 * 409 * @param rgbvalue a float array with length of at least 3 410 * @return a float array with length equal to the number of components in 411 * this {@code ColorSpace} 412 * @throws ArrayIndexOutOfBoundsException if array length is not at least 3 413 */ 414 public abstract float[] fromRGB(float[] rgbvalue); 415 416 /** 417 * Transforms a color value assumed to be in this {@code ColorSpace} into 418 * the {@code CS_CIEXYZ} conversion color space. 419 * <p> 420 * This method transforms color values using relative colorimetry, as 421 * defined by the International Color Consortium standard. This means that 422 * the XYZ values returned by this method are represented relative to the 423 * D50 white point of the {@code CS_CIEXYZ} color space. This representation 424 * is useful in a two-step color conversion process in which colors are 425 * transformed from an input color space to {@code CS_CIEXYZ} and then to an 426 * output color space. This representation is not the same as the XYZ values 427 * that would be measured from the given color value by a colorimeter. A 428 * further transformation is necessary to compute the XYZ values that would 429 * be measured using current CIE recommended practices. See the 430 * {@link ICC_ColorSpace#toCIEXYZ(float[]) toCIEXYZ} method of 431 * {@code ICC_ColorSpace} for further information. 432 * 433 * @param colorvalue a float array with length of at least the number of 434 * components in this {@code ColorSpace} 435 * @return a float array of length 3 436 * @throws ArrayIndexOutOfBoundsException if array length is not at least 437 * the number of components in this {@code ColorSpace}. 438 */ 439 public abstract float[] toCIEXYZ(float[] colorvalue); 440 441 /** 442 * Transforms a color value assumed to be in the {@code CS_CIEXYZ} 443 * conversion color space into this {@code ColorSpace}. 444 * <p> 445 * This method transforms color values using relative colorimetry, as 446 * defined by the International Color Consortium standard. This means that 447 * the XYZ argument values taken by this method are represented relative to 448 * the D50 white point of the {@code CS_CIEXYZ} color space. This 449 * representation is useful in a two-step color conversion process in which 450 * colors are transformed from an input color space to {@code CS_CIEXYZ} and 451 * then to an output color space. The color values returned by this method 452 * are not those that would produce the XYZ value passed to the method when 453 * measured by a colorimeter. If you have XYZ values corresponding to 454 * measurements made using current CIE recommended practices, they must be 455 * converted to D50 relative values before being passed to this method. See 456 * the {@link ICC_ColorSpace#fromCIEXYZ(float[]) fromCIEXYZ} method of 457 * {@code ICC_ColorSpace} for further information. 458 * 459 * @param colorvalue a float array with length of at least 3 460 * @return a float array with length equal to the number of components in 461 * this {@code ColorSpace} 462 * @throws ArrayIndexOutOfBoundsException if array length is not at least 3 463 */ 464 public abstract float[] fromCIEXYZ(float[] colorvalue); 465 466 /** 467 * Returns the color space type of this {@code ColorSpace} (for example 468 * {@code TYPE_RGB}, {@code TYPE_XYZ}, ...). The type defines the number of 469 * components of the color space and the interpretation, e.g. 470 * {@code TYPE_RGB} identifies a color space with three components - red, 471 * green, and blue. It does not define the particular color characteristics 472 * of the space, e.g. the chromaticities of the primaries. 473 * 474 * @return the type constant that represents the type of this 475 * {@code ColorSpace} 476 */ 477 public int getType() { 478 return type; 479 } 480 481 /** 482 * Returns the number of components of this ColorSpace. 483 * 484 * @return the number of components in this {@code ColorSpace} 485 */ 486 public int getNumComponents() { 487 return numComponents; 488 } 489 490 /** 491 * Returns the name of the component given the component index. 492 * 493 * @param idx the component index 494 * @return the name of the component at the specified index 495 * @throws IllegalArgumentException if {@code idx} is less than 0 or greater 496 * than {@code numComponents - 1} 497 */ 498 public String getName (int idx) { 499 /* REMIND - handle common cases here */ 500 if ((idx < 0) || (idx > numComponents - 1)) { 501 throw new IllegalArgumentException( 502 "Component index out of range: " + idx); 503 } 504 505 if (compName == null) { 506 switch (type) { 507 case ColorSpace.TYPE_XYZ: 508 compName = new String[] {"X", "Y", "Z"}; 509 break; 510 case ColorSpace.TYPE_Lab: 511 compName = new String[] {"L", "a", "b"}; 512 break; 513 case ColorSpace.TYPE_Luv: 514 compName = new String[] {"L", "u", "v"}; 515 break; 516 case ColorSpace.TYPE_YCbCr: 534 break; 535 case ColorSpace.TYPE_CMYK: 536 compName = new String[] {"Cyan", "Magenta", "Yellow", 537 "Black"}; 538 break; 539 case ColorSpace.TYPE_CMY: 540 compName = new String[] {"Cyan", "Magenta", "Yellow"}; 541 break; 542 default: 543 String [] tmp = new String[numComponents]; 544 for (int i = 0; i < tmp.length; i++) { 545 tmp[i] = "Unnamed color component(" + i + ")"; 546 } 547 compName = tmp; 548 } 549 } 550 return compName[idx]; 551 } 552 553 /** 554 * Returns the minimum normalized color component value for the specified 555 * component. The default implementation in this abstract class returns 0.0 556 * for all components. Subclasses should override this method if necessary. 557 * 558 * @param component the component index 559 * @return the minimum normalized component value 560 * @throws IllegalArgumentException if component is less than 0 or greater 561 * than {@code numComponents - 1} 562 * @since 1.4 563 */ 564 public float getMinValue(int component) { 565 if ((component < 0) || (component > numComponents - 1)) { 566 throw new IllegalArgumentException( 567 "Component index out of range: " + component); 568 } 569 return 0.0f; 570 } 571 572 /** 573 * Returns the maximum normalized color component value for the specified 574 * component. The default implementation in this abstract class returns 1.0 575 * for all components. Subclasses should override this method if necessary. 576 * 577 * @param component the component index 578 * @return the maximum normalized component value 579 * @throws IllegalArgumentException if component is less than 0 or greater 580 * than {@code numComponents - 1} 581 * @since 1.4 582 */ 583 public float getMaxValue(int component) { 584 if ((component < 0) || (component > numComponents - 1)) { 585 throw new IllegalArgumentException( 586 "Component index out of range: " + component); 587 } 588 return 1.0f; 589 } 590 591 /* 592 * Returns {@code true} if {@code cspace} is the XYZspace. 593 */ 594 static boolean isCS_CIEXYZ(ColorSpace cspace) { 595 return (cspace == XYZspace); 596 } 597 } |