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 28import java.awt.Component; 29import javax.swing.UIManager; 30 31class ColorModel { 32 33 private final String prefix; 34 private final String[] labels; 35 36 ColorModel(String name, String... labels) { 37 this.prefix = "ColorChooser." + name; // NON-NLS: default prefix 38 this.labels = labels; 39 } 40 41 ColorModel() { 42 this("rgb", "Red", "Green", "Blue", "Alpha"); // NON-NLS: components 43 } 44 45 void setColor(int color, float[] model) { 46 model[0] = normalize(color >> 16); 47 model[1] = normalize(color >> 8); 48 model[2] = normalize(color); 49 model[3] = normalize(color >> 24); 50 } 51 52 int getColor(float[] model) { 53 return to8bit(model[2]) | (to8bit(model[1]) << 8) | (to8bit(model[0]) << 16) | (to8bit(model[3]) << 24); 54 } 55 56 int getCount() { 57 return this.labels.length; 58 } 59 60 int getMinimum(int index) { 61 return 0; 62 } 63 64 int getMaximum(int index) { 65 return 255; 66 } 67 68 float getDefault(int index) { 69 return 0.0f; 70 } 71 72 final String getLabel(Component component, int index) { 73 return getText(component, this.labels[index]); 74 } 75 76 private static float normalize(int value) { 77 return (float) (value & 0xFF) / 255.0f; 78 } 79 80 private static int to8bit(float value) { 81 return (int) (255.0f * value); 82 } 83 84 final String getText(Component component, String suffix) { 85 return UIManager.getString(this.prefix + suffix + "Text", component.getLocale()); // NON-NLS: default postfix 86 } 87 88 final int getInteger(Component component, String suffix) { 89 Object value = UIManager.get(this.prefix + suffix, component.getLocale()); 90 if (value instanceof Integer) { 91 return (Integer) value; 92 } 93 if (value instanceof String) { 94 try { 95 return Integer.parseInt((String) value); 96 } 97 catch (NumberFormatException exception) { 98 } 99 } 100 return -1; 101 } 102} 103