xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/type/ColorStore.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /*
30  *        Copyright (C) 1996  Active Software, Inc.
31  *                  All rights reserved.
32  *
33  * @(#) ColorStore.java 1.15 - last change made 07/10/97
34  */
35 
36 package sunsoft.jws.visual.rt.type;
37 
38 import sunsoft.jws.visual.rt.base.Global;
39 
40 import java.awt.Color;
41 import java.util.Hashtable;
42 import java.util.Enumeration;
43 
44 /**
45  * Stores colors by name and rgb value.  Names are always stored in
46  * lower case, and searches are done after converting the search key
47  * into lower case.  Two hashtables are used so colors can be accessed
48  * efficiently either by color or name.
49  *
50  * @version 1.15, 07/10/97
51  */
52 public class ColorStore {
53     // COLOR <-> NAME HASHTABLES
54 
55     /**
56      * Storage place for Color RGB values and named colors.
57      * It's like a two-way hashtable.
58      */
59     private static ColorStore defaultColorStore;
60 
61     /**
62      * Returns a reference to the single global instance of ColorStore.
63      */
getDefaultColorStore()64     public static ColorStore getDefaultColorStore() {
65         initColorStore();
66         return (defaultColorStore);
67     }
68 
initColorStore()69     private static void initColorStore() {
70         if (defaultColorStore != null)
71             return;
72         defaultColorStore = new ColorStore();
73         defaultColorStore.initDefaultColorList();
74     }
75 
76 
77     /**
78      * Storage place for Color RGB values (keys) and named colors (values)
79      */
80     private Hashtable rgbkeys;
81 
82     /**
83      * Storage place for named colors (keys) Color RGB values (values)
84      */
85     private Hashtable namekeys;
86 
87     /**
88      * A list sorted by colorName (a-z)
89      */
90 
91     private String[] colorNameArray;
92 
ColorStore()93     public ColorStore() {
94         rgbkeys = new Hashtable();
95         namekeys = new Hashtable();
96     }
97 
98     /**
99      * Returns the name of a color.  If the color name is not in
100      * our table, return #RRGGBB (a string of hex numbers).
101      */
getColorName(Color c)102     public String getColorName(Color c) {
103         return (String) rgbkeys.get(c);
104     }
105 
106     /**
107      * Given the name of a color, return the Color corresponding to it.
108      */
getColor(String name)109     public Color getColor(String name) {
110         if (name == null) {
111             return null;
112         } else {
113             return ((Color) namekeys.get(name.toLowerCase()));
114         }
115     }
116 
117     /**
118      * Given a Color, return the name of the color listed in the
119      * ColorStore closest to that color.
120      */
getClosestColorName(Color c)121     public String getClosestColorName(Color c) {
122 
123         Enumeration e;
124         int difference = 255*3, newDifference;
125         Color bestColor = null, cKey;
126 
127         if (rgbkeys.containsKey(c)) {
128             return (getColorName(c));
129         }
130 
131         e = rgbkeys.keys();
132         while (e.hasMoreElements()) {
133             cKey = (Color) e.nextElement();
134 
135             if ((newDifference =
136 		 (Math.abs(cKey.getRed() - c.getRed())) +
137 		 (Math.abs(cKey.getGreen() - c.getGreen())) +
138 		 (Math.abs(cKey.getBlue() - c.getBlue()))) <=
139 		difference)
140 		{
141 		    difference = newDifference;
142 		    bestColor = cKey;
143 		}
144         }
145         return getColorName(bestColor);
146     }
147 
148     /**
149      * Adds the color/name pair to the color store is it is not already
150      * there and returns the name of the color.  If the color is already
151      * registered, the previous name of the color is returned.
152      */
add(Color c, String name)153     public String add(Color c, String name) {
154         if (rgbkeys.containsKey(c)) {
155             return (getColorName(c));
156         } else {
157             rgbkeys.put(c, name.toLowerCase());
158             namekeys.put(name, c);
159             colorNameArray = null;
160             return (name);
161         }
162     }
163 
164     /**
165      * Returns the list of Colors named in the ColorStore.
166      */
getColorList()167     public String[] getColorList() {
168         int index;
169 
170         if (colorNameArray == null) {
171             colorNameArray = new String[rgbkeys.size()];
172 
173             index = 0;
174             Enumeration e = namekeys.keys();
175 
176             while (e.hasMoreElements()) {
177                 String colorName = (String)(e.nextElement());
178                 colorNameArray[index] = colorName;
179                 index++;
180             }
181 
182             Global.util.qsort(colorNameArray);
183         }
184         return colorNameArray;
185     }
186 
initDefaultColorList()187     private void initDefaultColorList() {
188         // Java.awt.Color constants:
189 
190         add(Color.black, /* NOI18N */"black");
191         add(Color.blue, /* NOI18N */"blue");
192         add(Color.cyan, /* NOI18N */"cyan");
193         add(Color.darkGray, "darkgray");
194         add(Color.gray, /* NOI18N */"gray");
195         add(Color.green, /* NOI18N */"green");
196         add(Color.lightGray, "lightgray");
197         add(Color.magenta, /* NOI18N */"magenta");
198         add(Color.orange, /* NOI18N */"orange");
199         add(Color.pink, /* NOI18N */"pink");
200         add(Color.red, /* NOI18N */"red");
201         add(Color.white, /* NOI18N */"white");
202         add(Color.yellow, /* NOI18N */"yellow");
203 
204         // Colors from rgb.txt:
205 
206         add(new Color(133, 133, 133), /* NOI18N */"gray52");
207         add(new Color(122, 122, 122), /* NOI18N */"gray48");
208         add(new Color(186, 186, 186), /* NOI18N */"gray73");
209         add(new Color(94, 94, 94), /* NOI18N */"gray37");
210         add(new Color(196, 196, 196), /* NOI18N */"gray77");
211         add(new Color(84, 84, 84), /* NOI18N */"gray33");
212         add(new Color(66, 66, 66), /* NOI18N */"gray26");
213         add(new Color(48, 48, 48), /* NOI18N */"gray19");
214         add(new Color(92, 92, 92), /* NOI18N */"gray36");
215         add(new Color(74, 74, 74), /* NOI18N */"gray29");
216         add(new Color(163, 163, 163), /* NOI18N */"gray64");
217         add(new Color(28, 28, 28), /* NOI18N */"gray11");
218         add(new Color(46, 46, 46), /* NOI18N */"gray18");
219         add(new Color(173, 173, 173), /* NOI18N */"gray68");
220         add(new Color(36, 36, 36), /* NOI18N */"gray14");
221         add(new Color(54, 54, 54), /* NOI18N */"gray21");
222         add(new Color(194, 194, 194), /* NOI18N */"gray76");
223         add(new Color(150, 150, 150), /* NOI18N */"gray59");
224         add(new Color(171, 171, 171), /* NOI18N */"gray67");
225         add(new Color(3, 3, 3), /* NOI18N */"gray1");
226         add(new Color(181, 181, 181), /* NOI18N */"gray71");
227         add(new Color(20, 20, 20), /* NOI18N */"gray8");
228         add(new Color(10, 10, 10), /* NOI18N */"gray4");
229         add(new Color(207, 207, 207), /* NOI18N */"gray81");
230         add(new Color(99, 99, 99), /* NOI18N */"gray39");
231         add(new Color(237, 237, 237), /* NOI18N */"gray93");
232         add(new Color(79, 79, 79), /* NOI18N */"gray31");
233         add(new Color(87, 87, 87), /* NOI18N */"gray34");
234         add(new Color(204, 204, 204), /* NOI18N */"gray80");
235         add(new Color(214, 214, 214), /* NOI18N */"gray84");
236         add(new Color(235, 235, 235), /* NOI18N */"gray92");
237         add(new Color(245, 245, 245), /* NOI18N */"gray96");
238         add(new Color(8, 8, 8), /* NOI18N */"gray3");
239         add(new Color(71, 71, 71), /* NOI18N */"gray28");
240         add(new Color(201, 201, 201), /* NOI18N */"gray79");
241         add(new Color(61, 61, 61), /* NOI18N */"gray24");
242         add(new Color(51, 51, 51), /* NOI18N */"gray20");
243         add(new Color(33, 33, 33), /* NOI18N */"gray13");
244         add(new Color(15, 15, 15), /* NOI18N */"gray6");
245         add(new Color(117, 117, 117), /* NOI18N */"gray46");
246         add(new Color(31, 31, 31), /* NOI18N */"gray12");
247         add(new Color(13, 13, 13), /* NOI18N */"gray5");
248         add(new Color(105, 105, 105), /* NOI18N */"gray41");
249         add(new Color(148, 148, 148), /* NOI18N */"gray58");
250         add(new Color(252, 252, 252), /* NOI18N */"gray99");
251         add(new Color(115, 115, 115), /* NOI18N */"gray45");
252         add(new Color(158, 158, 158), /* NOI18N */"gray62");
253         add(new Color(179, 179, 179), /* NOI18N */"gray70");
254         add(new Color(240, 240, 240), /* NOI18N */"gray94");
255         add(new Color(189, 189, 189), /* NOI18N */"gray74");
256         add(new Color(250, 250, 250), /* NOI18N */"gray98");
257         add(new Color(199, 199, 199), /* NOI18N */"gray78");
258         add(new Color(145, 145, 145), /* NOI18N */"gray57");
259         add(new Color(70, 130, 180), /* NOI18N */"steel blue");
260         add(new Color(233, 150, 122), /* NOI18N */"darksalmon");
261         add(new Color(100, 149, 237), /* NOI18N */"cornflowerblue");
262         add(new Color(30, 144, 255), /* NOI18N */"dodgerblue");
263         add(new Color(238, 221, 130), /* NOI18N */"lightgoldenrod");
264         add(new Color(205, 133, 63), /* NOI18N */"peru");
265         add(new Color(154, 205, 50), /* NOI18N */"yellow green");
266         add(new Color(175, 238, 238), /* NOI18N */"paleturquoise");
267         add(new Color(0, 100, 0), /* NOI18N */"darkgreen");
268         add(new Color(160, 82, 45), /* NOI18N */"sienna");
269         add(new Color(143, 188, 143), /* NOI18N */"darkseagreen");
270         add(new Color(255, 228, 196), /* NOI18N */"bisque");
271         add(new Color(147, 112, 219), /* NOI18N */"mediumpurple");
272         add(new Color(148, 0, 211), /* NOI18N */"dark violet");
273         add(new Color(124, 252, 0), /* NOI18N */"lawn green");
274         add(new Color(119, 136, 153), /* NOI18N */"lightslategray");
275         add(new Color(230, 230, 250), /* NOI18N */"lavender");
276         add(new Color(248, 248, 255), /* NOI18N */"ghostwhite");
277         add(new Color(176, 224, 230), /* NOI18N */"powderblue");
278         add(new Color(218, 165, 32), /* NOI18N */"goldenrod");
279         add(new Color(255, 228, 181), /* NOI18N */"moccasin");
280         add(new Color(255, 228, 225), /* NOI18N */"mistyrose");
281         add(new Color(255, 255, 224), /* NOI18N */"light yellow");
282         add(new Color(255, 99, 71), /* NOI18N */"tomato");
283         add(new Color(245, 255, 250), /* NOI18N */"mintcream");
284         add(new Color(138, 43, 226), /* NOI18N */"blueviolet");
285         add(new Color(32, 178, 170), /* NOI18N */"light sea green");
286         add(new Color(255, 240, 245), /* NOI18N */"lavender blush");
287         add(new Color(127, 255, 212), /* NOI18N */"aquamarine");
288         add(new Color(165, 42, 42), /* NOI18N */"brown");
289         add(new Color(219, 112, 147), /* NOI18N */"pale violet red");
290         add(new Color(240, 255, 255), /* NOI18N */"azure");
291         add(new Color(107, 142, 35), /* NOI18N */"olivedrab");
292         add(new Color(47, 79, 79), /* NOI18N */"darkslategray");
293         add(new Color(139, 69, 19), /* NOI18N */"saddle brown");
294         add(new Color(160, 32, 240), /* NOI18N */"purple");
295         add(new Color(186, 85, 211), /* NOI18N */"medium orchid");
296         add(new Color(240, 255, 240), /* NOI18N */"honeydew");
297         add(new Color(176, 196, 222), /* NOI18N */"lightsteelblue");
298         add(new Color(64, 224, 208), /* NOI18N */"turquoise");
299         add(new Color(255, 127, 80), /* NOI18N */"coral");
300         add(new Color(184, 134, 11), /* NOI18N */"darkgoldenrod");
301         add(new Color(60, 179, 113), /* NOI18N */"mediumseagreen");
302         add(new Color(210, 180, 140), /* NOI18N */"tan");
303         add(new Color(255, 222, 173), /* NOI18N */"navajo white");
304         add(new Color(46, 139, 87), /* NOI18N */"sea green");
305         add(new Color(123, 104, 238), /* NOI18N */"mediumslateblue");
306         add(new Color(250, 250, 210), /* NOI18N */
307 	    "light goldenrod yellow");
308         add(new Color(135, 206, 235), /* NOI18N */"sky blue");
309         add(new Color(132, 112, 255), /* NOI18N */"lightslateblue");
310         add(new Color(250, 240, 230), /* NOI18N */"linen");
311         add(new Color(218, 112, 214), /* NOI18N */"orchid");
312         add(new Color(0, 0, 128), /* NOI18N */"navy blue");
313         add(new Color(253, 245, 230), /* NOI18N */"old lace");
314         add(new Color(240, 248, 255), /* NOI18N */"aliceblue");
315         add(new Color(72, 209, 204), /* NOI18N */"mediumturquoise");
316         add(new Color(255, 140, 0), /* NOI18N */"dark orange");
317         add(new Color(72, 61, 139), /* NOI18N */"dark slate blue");
318         add(new Color(255, 160, 122), /* NOI18N */"light salmon");
319         add(new Color(221, 160, 221), /* NOI18N */"plum");
320         add(new Color(238, 130, 238), /* NOI18N */"violet");
321         add(new Color(34, 139, 34), /* NOI18N */"forest green");
322         add(new Color(0, 255, 127), /* NOI18N */"springgreen");
323         add(new Color(85, 107, 47), /* NOI18N */"darkolivegreen");
324         add(new Color(238, 232, 170), /* NOI18N */"pale goldenrod");
325         add(new Color(245, 245, 220), /* NOI18N */"beige");
326         add(new Color(255, 250, 240), /* NOI18N */"floralwhite");
327         add(new Color(255, 218, 185), /* NOI18N */"peach puff");
328         add(new Color(50, 205, 50), /* NOI18N */"limegreen");
329         add(new Color(152, 251, 152), /* NOI18N */"palegreen");
330         add(new Color(240, 230, 140), /* NOI18N */"khaki");
331         add(new Color(188, 143, 143), /* NOI18N */"rosybrown");
332         add(new Color(244, 164, 96), /* NOI18N */"sandybrown");
333         add(new Color(189, 183, 107), /* NOI18N */"darkkhaki");
334         add(new Color(25, 25, 112), /* NOI18N */"midnight blue");
335         add(new Color(255, 235, 205), /* NOI18N */"blanched almond");
336         add(new Color(224, 255, 255), /* NOI18N */"light cyan");
337         add(new Color(255, 182, 193), /* NOI18N */"lightpink");
338         add(new Color(95, 158, 160), /* NOI18N */"cadetblue");
339         add(new Color(106, 90, 205), /* NOI18N */"slate blue");
340         add(new Color(245, 222, 179), /* NOI18N */"wheat");
341         add(new Color(255, 69, 0), /* NOI18N */"orangered");
342         add(new Color(127, 255, 0), /* NOI18N */"chartreuse");
343         add(new Color(255, 255, 255), /* NOI18N */"white");
344         add(new Color(65, 105, 225), /* NOI18N */"royalblue");
345         add(new Color(173, 216, 230), /* NOI18N */"light blue");
346         add(new Color(255, 250, 250), /* NOI18N */"snow");
347         add(new Color(255, 245, 238), /* NOI18N */"seashell");
348         add(new Color(250, 128, 114), /* NOI18N */"salmon");
349         add(new Color(255, 255, 240), /* NOI18N */"ivory");
350         add(new Color(255, 239, 213), /* NOI18N */"papaya whip");
351         add(new Color(153, 50, 204), /* NOI18N */"dark orchid");
352         add(new Color(208, 32, 144), /* NOI18N */"violet red");
353         add(new Color(255, 248, 220), /* NOI18N */"cornsilk");
354         add(new Color(255, 105, 180), /* NOI18N */"hotpink");
355         add(new Color(176, 48, 96), /* NOI18N */"maroon");
356         add(new Color(178, 34, 34), /* NOI18N */"firebrick");
357         add(new Color(240, 128, 128), /* NOI18N */"lightcoral");
358         add(new Color(220, 220, 220), /* NOI18N */"gainsboro");
359         add(new Color(216, 191, 216), /* NOI18N */"thistle");
360         add(new Color(135, 206, 250), /* NOI18N */"light sky blue");
361         add(new Color(210, 105, 30), /* NOI18N */"chocolate");
362         add(new Color(173, 255, 47), /* NOI18N */"green yellow");
363         add(new Color(112, 128, 144), /* NOI18N */"slate gray");
364         add(new Color(0, 191, 255), /* NOI18N */"deepskyblue");
365         add(new Color(255, 250, 205), /* NOI18N */"lemon chiffon");
366         add(new Color(0, 206, 209), /* NOI18N */"dark turquoise");
367         add(new Color(222, 184, 135), /* NOI18N */"burlywood");
368         add(new Color(199, 21, 133), /* NOI18N */"mediumvioletred");
369         add(new Color(250, 235, 215), /* NOI18N */"antique white");
370         add(new Color(255, 215, 0), /* NOI18N */"gold");
371         add(new Color(255, 20, 147), /* NOI18N */"deep pink");
372         add(new Color(205, 92, 92), /* NOI18N */"indianred");
373         add(new Color(0, 250, 154), /* NOI18N */"medium spring green");
374     }
375 }
376