KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > datatypes > ColorType


1 /*
2  * $Id: ColorType.java,v 1.16.2.1 2003/02/25 10:48:28 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.datatypes;
52
53 import java.util.*;
54 import org.apache.fop.messaging.MessageHandler;
55
56 /**
57  * a colour quantity in XSL
58  */

59 public class ColorType {
60
61     /**
62      * the red component
63      */

64     protected float red;
65
66     /**
67      * the green component
68      */

69     protected float green;
70
71     /**
72      * the blue component
73      */

74     protected float blue;
75
76     /**
77      * the alpha component
78      */

79     protected float alpha = 0;
80
81     public ColorType(float red, float green, float blue) {
82         this.red = red;
83         this.green = green;
84         this.blue = blue;
85     }
86
87     /**
88      * set the colour given a particular String specifying either a
89      * colour name or #RGB or #RRGGBB
90      */

91     public ColorType(String JavaDoc value) {
92         if (value.startsWith("#")) {
93             try {
94                 if (value.length() == 4) {
95                     // note: divide by 15 so F = FF = 1 and so on
96
this.red = Integer.parseInt(value.substring(1, 2), 16)
97                                / 15f;
98                     this.green = Integer.parseInt(value.substring(2, 3), 16)
99                                  / 15f;
100                     this.blue = Integer.parseInt(value.substring(3), 16)
101                                 / 15f;
102                 } else if (value.length() == 7) {
103                     // note: divide by 255 so FF = 1
104
this.red = Integer.parseInt(value.substring(1, 3), 16)
105                                / 255f;
106                     this.green = Integer.parseInt(value.substring(3, 5), 16)
107                                  / 255f;
108                     this.blue = Integer.parseInt(value.substring(5), 16)
109                                 / 255f;
110                 } else {
111                     this.red = 0;
112                     this.green = 0;
113                     this.blue = 0;
114                     MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB");
115                 }
116             } catch (Exception JavaDoc e) {
117                 this.red = 0;
118                 this.green = 0;
119                 this.blue = 0;
120                 MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB");
121             }
122         } else if (value.startsWith("rgb(")) {
123             int poss = value.indexOf("(");
124             int pose = value.indexOf(")");
125             if (poss != -1 && pose != -1) {
126                 value = value.substring(poss + 1, pose);
127                 StringTokenizer st = new StringTokenizer(value, ",");
128                 try {
129                     if (st.hasMoreTokens()) {
130                         String JavaDoc str = st.nextToken().trim();
131                         if (str.endsWith("%")) {
132                             this.red =
133                                 Integer.parseInt(str.substring(0, str.length() - 1))
134                                 * 2.55f;
135                         } else {
136                             this.red = Integer.parseInt(str) / 255f;
137                         }
138                     }
139                     if (st.hasMoreTokens()) {
140                         String JavaDoc str = st.nextToken().trim();
141                         if (str.endsWith("%")) {
142                             this.green =
143                                 Integer.parseInt(str.substring(0, str.length() - 1))
144                                 * 2.55f;
145                         } else {
146                             this.green = Integer.parseInt(str) / 255f;
147                         }
148                     }
149                     if (st.hasMoreTokens()) {
150                         String JavaDoc str = st.nextToken().trim();
151                         if (str.endsWith("%")) {
152                             this.blue =
153                                 Integer.parseInt(str.substring(0, str.length() - 1))
154                                 * 2.55f;
155                         } else {
156                             this.blue = Integer.parseInt(str) / 255f;
157                         }
158                     }
159                 } catch (Exception JavaDoc e) {
160                     this.red = 0;
161                     this.green = 0;
162                     this.blue = 0;
163                     MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB");
164                 }
165             }
166         } else if (value.startsWith("url(")) {
167             // refers to a gradient
168
} else {
169             if (value.toLowerCase().equals("transparent")) {
170                 this.red = 0;
171                 this.green = 0;
172                 this.blue = 0;
173                 this.alpha = 1;
174             } else {
175                 boolean found = false;
176                 for (int count = 0; count < names.length; count++) {
177                     if (value.toLowerCase().equals(names[count])) {
178                         this.red = vals[count][0] / 255f;
179                         this.green = vals[count][1] / 255f;
180                         this.blue = vals[count][2] / 255f;
181                         found = true;
182                         break;
183                     }
184                 }
185                 if (!found) {
186                     this.red = 0;
187                     this.green = 0;
188                     this.blue = 0;
189                     MessageHandler.errorln("unknown colour name: "
190                                            + value);
191                 }
192             }
193         }
194     }
195
196     public float blue() {
197         return this.blue;
198     }
199
200     public float green() {
201         return this.green;
202     }
203
204     public float red() {
205         return this.red;
206     }
207
208     public float alpha() {
209         return this.alpha;
210     }
211
212     static final String JavaDoc[] names = {
213         "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige",
214         "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown",
215         "burlywood", "cadetblue", "chartreuse", "chocolate", "coral",
216         "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue",
217         "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey",
218         "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange",
219         "darkorchid", "darkred", "darksalmon", "darkseagreen",
220         "darkslateblue", "darkslategray", "darkslategrey", "darkturquoise",
221         "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey",
222         "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia",
223         "gainsboro", "lightpink", "lightsalmon", "lightseagreen",
224         "lightskyblue", "lightslategray", "lightslategrey", "lightsteelblue",
225         "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon",
226         "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple",
227         "mediumseagreen", "mediumslateblue", "mediumspringgreen",
228         "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream",
229         "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive",
230         "olivedrab", "orange", "orangered", "orchid", "palegoldenrod",
231         "palegreen", "paleturquoise", "palevioletred", "papayawhip",
232         "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red",
233         "rosybrown", "royalblue", "saddlebrown", "salmon", "ghostwhite",
234         "gold", "goldenrod", "gray", "grey", "green", "greenyellow",
235         "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki",
236         "lavender", "lavenderblush", "lawngreen", "lemonchiffon",
237         "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow",
238         "lightgray", "lightgreen", "lightgrey", "sandybrown", "seagreen",
239         "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray",
240         "slategrey", "snow", "springgreen", "steelblue", "tan", "teal",
241         "thistle", "tomato", "turquoise", "violet", "wheat", "white",
242         "whitesmoke", "yellow", "yellowgreen"
243     };
244
245     static final int[][] vals = {
246          {
247             240, 248, 255
248         }, {
249             250, 235, 215
250         }, {
251             0, 255, 255
252         }, {
253             127, 255, 212
254         }, {
255             240, 255, 255
256         }, {
257             245, 245, 220
258         }, {
259             255, 228, 196
260         }, {
261             0, 0, 0
262         }, {
263             255, 235, 205
264         }, {
265             0, 0, 255
266         }, {
267             138, 43, 226
268         }, {
269             165, 42, 42
270         }, {
271             222, 184, 135
272         }, {
273             95, 158, 160
274         }, {
275             127, 255, 0
276         }, {
277             210, 105, 30
278         }, {
279             255, 127, 80
280         }, {
281             100, 149, 237
282         }, {
283             255, 248, 220
284         }, {
285             220, 20, 60
286         }, {
287             0, 255, 255
288         }, {
289             0, 0, 139
290         }, {
291             0, 139, 139
292         }, {
293             184, 134, 11
294         }, {
295             169, 169, 169
296         }, {
297             0, 100, 0
298         }, {
299             169, 169, 169
300         }, {
301             189, 183, 107
302         }, {
303             139, 0, 139
304         }, {
305             85, 107, 47
306         }, {
307             255, 140, 0
308         }, {
309             153, 50, 204
310         }, {
311             139, 0, 0
312         }, {
313             233, 150, 122
314         }, {
315             143, 188, 143
316         }, {
317             72, 61, 139
318         }, {
319             47, 79, 79
320         }, {
321             47, 79, 79
322         }, {
323             0, 206, 209
324         }, {
325             148, 0, 211
326         }, {
327             255, 20, 147
328         }, {
329             0, 191, 255
330         }, {
331             105, 105, 105
332         }, {
333             105, 105, 105
334         }, {
335             30, 144, 255
336         }, {
337             178, 34, 34
338         }, {
339             255, 250, 240
340         }, {
341             34, 139, 34
342         }, {
343             255, 0, 255
344         }, {
345             220, 220, 220
346         }, {
347             255, 182, 193
348         }, {
349             255, 160, 122
350         }, {
351             32, 178, 170
352         }, {
353             135, 206, 250
354         }, {
355             119, 136, 153
356         }, {
357             119, 136, 153
358         }, {
359             176, 196, 222
360         }, {
361             255, 255, 224
362         }, {
363             0, 255, 0
364         }, {
365             50, 205, 50
366         }, {
367             250, 240, 230
368         }, {
369             255, 0, 255
370         }, {
371             128, 0, 0
372         }, {
373             102, 205, 170
374         }, {
375             0, 0, 205
376         }, {
377             186, 85, 211
378         }, {
379             147, 112, 219
380         }, {
381             60, 179, 113
382         }, {
383             123, 104, 238
384         }, {
385             0, 250, 154
386         }, {
387             72, 209, 204
388         }, {
389             199, 21, 133
390         }, {
391             25, 25, 112
392         }, {
393             245, 255, 250
394         }, {
395             255, 228, 225
396         }, {
397             255, 228, 181
398         }, {
399             255, 222, 173
400         }, {
401             0, 0, 128
402         }, {
403             253, 245, 230
404         }, {
405             128, 128, 0
406         }, {
407             107, 142, 35
408         }, {
409             255, 165, 0
410         }, {
411             255, 69, 0
412         }, {
413             218, 112, 214
414         }, {
415             238, 232, 170
416         }, {
417             152, 251, 152
418         }, {
419             175, 238, 238
420         }, {
421             219, 112, 147
422         }, {
423             255, 239, 213
424         }, {
425             255, 218, 185
426         }, {
427             205, 133, 63
428         }, {
429             255, 192, 203
430         }, {
431             221, 160, 221
432         }, {
433             176, 224, 230
434         }, {
435             128, 0, 128
436         }, {
437             255, 0, 0
438         }, {
439             188, 143, 143
440         }, {
441             65, 105, 225
442         }, {
443             139, 69, 19
444         }, {
445             250, 128, 114
446         }, {
447             248, 248, 255
448         }, {
449             255, 215, 0
450         }, {
451             218, 165, 32
452         }, {
453             128, 128, 128
454         }, {
455             128, 128, 128
456         }, {
457             0, 128, 0
458         }, {
459             173, 255, 47
460         }, {
461             240, 255, 240
462         }, {
463             255, 105, 180
464         }, {
465             205, 92, 92
466         }, {
467             75, 0, 130
468         }, {
469             255, 255, 240
470         }, {
471             240, 230, 140
472         }, {
473             230, 230, 250
474         }, {
475             255, 240, 245
476         }, {
477             124, 252, 0
478         }, {
479             255, 250, 205
480         }, {
481             173, 216, 230
482         }, {
483             240, 128, 128
484         }, {
485             224, 255, 255
486         }, {
487             250, 250, 210
488         }, {
489             211, 211, 211
490         }, {
491             144, 238, 144
492         }, {
493             211, 211, 211
494         }, {
495             244, 164, 96
496         }, {
497             46, 139, 87
498         }, {
499             255, 245, 238
500         }, {
501             160, 82, 45
502         }, {
503             192, 192, 192
504         }, {
505             135, 206, 235
506         }, {
507             106, 90, 205
508         }, {
509             112, 128, 144
510         }, {
511             112, 128, 144
512         }, {
513             255, 250, 250
514         }, {
515             0, 255, 127
516         }, {
517             70, 130, 180
518         }, {
519             210, 180, 140
520         }, {
521             0, 128, 128
522         }, {
523             216, 191, 216
524         }, {
525             255, 99, 71
526         }, {
527             64, 224, 208
528         }, {
529             238, 130, 238
530         }, {
531             245, 222, 179
532         }, {
533             255, 255, 255
534         }, {
535             245, 245, 245
536         }, {
537             255, 255, 0
538         }, {
539             154, 205, 50
540         }
541     };
542 }
543
544 /*
545  * aliceblue rgb(240, 248, 255)
546  * antiquewhite rgb(250, 235, 215)
547  * aqua rgb( 0, 255, 255)
548  * aquamarine rgb(127, 255, 212)
549  * azure rgb(240, 255, 255)
550  * beige rgb(245, 245, 220)
551  * bisque rgb(255, 228, 196)
552  * black rgb( 0, 0, 0)
553  * blanchedalmond rgb(255, 235, 205)
554  * blue rgb( 0, 0, 255)
555  * blueviolet rgb(138, 43, 226)
556  * brown rgb(165, 42, 42)
557  * burlywood rgb(222, 184, 135)
558  * cadetblue rgb( 95, 158, 160)
559  * chartreuse rgb(127, 255, 0)
560  * chocolate rgb(210, 105, 30)
561  * coral rgb(255, 127, 80)
562  * cornflowerblue rgb(100, 149, 237)
563  * cornsilk rgb(255, 248, 220)
564  * crimson rgb(220, 20, 60)
565  * cyan rgb( 0, 255, 255)
566  * darkblue rgb( 0, 0, 139)
567  * darkcyan rgb( 0, 139, 139)
568  * darkgoldenrod rgb(184, 134, 11)
569  * darkgray rgb(169, 169, 169)
570  * darkgreen rgb( 0, 100, 0)
571  * darkgrey rgb(169, 169, 169)
572  * darkkhaki rgb(189, 183, 107)
573  * darkmagenta rgb(139, 0, 139)
574  * darkolivegreen rgb( 85, 107, 47)
575  * darkorange rgb(255, 140, 0)
576  * darkorchid rgb(153, 50, 204)
577  * darkred rgb(139, 0, 0)
578  * darksalmon rgb(233, 150, 122)
579  * darkseagreen rgb(143, 188, 143)
580  * darkslateblue rgb( 72, 61, 139)
581  * darkslategray rgb( 47, 79, 79)
582  * darkslategrey rgb( 47, 79, 79)
583  * darkturquoise rgb( 0, 206, 209)
584  * darkviolet rgb(148, 0, 211)
585  * deeppink rgb(255, 20, 147)
586  * deepskyblue rgb( 0, 191, 255)
587  * dimgray rgb(105, 105, 105)
588  * dimgrey rgb(105, 105, 105)
589  * dodgerblue rgb( 30, 144, 255)
590  * firebrick rgb(178, 34, 34)
591  * floralwhite rgb(255, 250, 240)
592  * forestgreen rgb( 34, 139, 34)
593  * fuchsia rgb(255, 0, 255)
594  * gainsboro rgb(220, 220, 220)
595  * lightpink rgb(255, 182, 193)
596  * lightsalmon rgb(255, 160, 122)
597  * lightseagreen rgb( 32, 178, 170)
598  * lightskyblue rgb(135, 206, 250)
599  * lightslategray rgb(119, 136, 153)
600  * lightslategrey rgb(119, 136, 153)
601  * lightsteelblue rgb(176, 196, 222)
602  * lightyellow rgb(255, 255, 224)
603  * lime rgb( 0, 255, 0)
604  * limegreen rgb( 50, 205, 50)
605  * linen rgb(250, 240, 230)
606  * magenta rgb(255, 0, 255)
607  * maroon rgb(128, 0, 0)
608  * mediumaquamarine rgb(102, 205, 170)
609  * mediumblue rgb( 0, 0, 205)
610  * mediumorchid rgb(186, 85, 211)
611  * mediumpurple rgb(147, 112, 219)
612  * mediumseagreen rgb( 60, 179, 113)
613  * mediumslateblue rgb(123, 104, 238)
614  * mediumspringgreen rgb( 0, 250, 154)
615  * mediumturquoise rgb( 72, 209, 204)
616  * mediumvioletred rgb(199, 21, 133)
617  * midnightblue rgb( 25, 25, 112)
618  * mintcream rgb(245, 255, 250)
619  * mistyrose rgb(255, 228, 225)
620  * moccasin rgb(255, 228, 181)
621  * navajowhite rgb(255, 222, 173)
622  * navy rgb( 0, 0, 128)
623  * oldlace rgb(253, 245, 230)
624  * olive rgb(128, 128, 0)
625  * olivedrab rgb(107, 142, 35)
626  * orange rgb(255, 165, 0)
627  * orangered rgb(255, 69, 0)
628  * orchid rgb(218, 112, 214)
629  * palegoldenrod rgb(238, 232, 170)
630  * palegreen rgb(152, 251, 152)
631  * paleturquoise rgb(175, 238, 238)
632  * palevioletred rgb(219, 112, 147)
633  * papayawhip rgb(255, 239, 213)
634  * peachpuff rgb(255, 218, 185)
635  * peru rgb(205, 133, 63)
636  * pink rgb(255, 192, 203)
637  * plum rgb(221, 160, 221)
638  * powderblue rgb(176, 224, 230)
639  * purple rgb(128, 0, 128)
640  * red rgb(255, 0, 0)
641  * rosybrown rgb(188, 143, 143)
642  * royalblue rgb( 65, 105, 225)
643  * saddlebrown rgb(139, 69, 19)
644  * salmon rgb(250, 128, 114)
645  * ghostwhite rgb(248, 248, 255)
646  * gold rgb(255, 215, 0)
647  * goldenrod rgb(218, 165, 32)
648  * gray rgb(128, 128, 128)
649  * grey rgb(128, 128, 128)
650  * green rgb( 0, 128, 0)
651  * greenyellow rgb(173, 255, 47)
652  * honeydew rgb(240, 255, 240)
653  * hotpink rgb(255, 105, 180)
654  * indianred rgb(205, 92, 92)
655  * indigo rgb( 75, 0, 130)
656  * ivory rgb(255, 255, 240)
657  * khaki rgb(240, 230, 140)
658  * lavender rgb(230, 230, 250)
659  * lavenderblush rgb(255, 240, 245)
660  * lawngreen rgb(124, 252, 0)
661  * lemonchiffon rgb(255, 250, 205)
662  * lightblue rgb(173, 216, 230)
663  * lightcoral rgb(240, 128, 128)
664  * lightcyan rgb(224, 255, 255)
665  * lightgoldenrodyellow rgb(250, 250, 210)
666  * lightgray rgb(211, 211, 211)
667  * lightgreen rgb(144, 238, 144)
668  * lightgrey rgb(211, 211, 211)
669  * sandybrown rgb(244, 164, 96)
670  * seagreen rgb( 46, 139, 87)
671  * seashell rgb(255, 245, 238)
672  * sienna rgb(160, 82, 45)
673  * silver rgb(192, 192, 192)
674  * skyblue rgb(135, 206, 235)
675  * slateblue rgb(106, 90, 205)
676  * slategray rgb(112, 128, 144)
677  * slategrey rgb(112, 128, 144)
678  * snow rgb(255, 250, 250)
679  * springgreen rgb( 0, 255, 127)
680  * steelblue rgb( 70, 130, 180)
681  * tan rgb(210, 180, 140)
682  * teal rgb( 0, 128, 128)
683  * thistle rgb(216, 191, 216)
684  * tomato rgb(255, 99, 71)
685  * turquoise rgb( 64, 224, 208)
686  * violet rgb(238, 130, 238)
687  * wheat rgb(245, 222, 179)
688  * white rgb(255, 255, 255)
689  * whitesmoke rgb(245, 245, 245)
690  * yellow rgb(255, 255, 0)
691  * yellowgreen rgb(154, 205, 50)
692  */

693
Popular Tags