1/* 2 * Copyright (c) 2008, 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 26package javax.swing.colorchooser; 27 28final class ColorModelCMYK extends ColorModel { 29 30 ColorModelCMYK() { 31 super("cmyk", "Cyan", "Magenta", "Yellow", "Black", "Alpha"); // NON-NLS: components 32 } 33 34 @Override 35 void setColor(int color, float[] space) { 36 super.setColor(color, space); 37 space[4] = space[3]; 38 RGBtoCMYK(space, space); 39 } 40 41 @Override 42 int getColor(float[] space) { 43 CMYKtoRGB(space, space); 44 space[3] = space[4]; 45 return super.getColor(space); 46 } 47 48 /** 49 * Converts CMYK components of a color to a set of RGB components. 50 * 51 * @param cmyk a float array with length equal to 52 * the number of CMYK components 53 * @param rgb a float array with length of at least 3 54 * that contains RGB components of a color 55 * @return a float array that contains RGB components 56 */ 57 private static float[] CMYKtoRGB(float[] cmyk, float[] rgb) { 58 if (rgb == null) { 59 rgb = new float[3]; 60 } 61 rgb[0] = 1.0f + cmyk[0] * cmyk[3] - cmyk[3] - cmyk[0]; 62 rgb[1] = 1.0f + cmyk[1] * cmyk[3] - cmyk[3] - cmyk[1]; 63 rgb[2] = 1.0f + cmyk[2] * cmyk[3] - cmyk[3] - cmyk[2]; 64 return rgb; 65 } 66 67 /** 68 * Converts RGB components of a color to a set of CMYK components. 69 * 70 * @param rgb a float array with length of at least 3 71 * that contains RGB components of a color 72 * @param cmyk a float array with length equal to 73 * the number of CMYK components 74 * @return a float array that contains CMYK components 75 */ 76 private static float[] RGBtoCMYK(float[] rgb, float[] cmyk) { 77 if (cmyk == null) { 78 cmyk = new float[4]; 79 } 80 float max = ColorModelHSL.max(rgb[0], rgb[1], rgb[2]); 81 if (max > 0.0f) { 82 cmyk[0] = 1.0f - rgb[0] / max; 83 cmyk[1] = 1.0f - rgb[1] / max; 84 cmyk[2] = 1.0f - rgb[2] / max; 85 } 86 else { 87 cmyk[0] = 0.0f; 88 cmyk[1] = 0.0f; 89 cmyk[2] = 0.0f; 90 } 91 cmyk[3] = 1.0f - max; 92 return cmyk; 93 } 94} 95