KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGColor


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.batik.ext.awt.g2d.GraphicContext;
26
27 /**
28  * Utility class that converts a Color object into a set of
29  * corresponding SVG attributes.
30  *
31  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
32  * @version $Id: SVGColor.java,v 1.14 2004/08/18 07:14:59 vhardy Exp $
33  * @see org.apache.batik.svggen.DOMTreeManager
34  */

35 public class SVGColor extends AbstractSVGConverter{
36     /**
37      * Predefined CSS colors
38      */

39     public static final Color JavaDoc aqua = new Color JavaDoc(0x00, 0xff, 0xff);
40     public static final Color JavaDoc black = Color.black;
41     public static final Color JavaDoc blue = Color.blue;
42     public static final Color JavaDoc fuchsia = new Color JavaDoc(0xff, 0x00, 0xff);
43     public static final Color JavaDoc gray = new Color JavaDoc(0x80, 0x80, 0x80);
44     public static final Color JavaDoc green = new Color JavaDoc(0x00, 0x80, 0x00);
45     public static final Color JavaDoc lime = new Color JavaDoc(0x00, 0xff, 0x00);
46     public static final Color JavaDoc maroon = new Color JavaDoc(0x80, 0x00, 0x00);
47     public static final Color JavaDoc navy = new Color JavaDoc(0x00, 0x00, 0x80);
48     public static final Color JavaDoc olive = new Color JavaDoc(0x80, 0x80, 00);
49     public static final Color JavaDoc purple = new Color JavaDoc(0x80, 0x00, 0x80);
50     public static final Color JavaDoc red = new Color JavaDoc(0xff, 0x00, 0x00);
51     public static final Color JavaDoc silver = new Color JavaDoc(0xc0, 0xc0, 0xc0);
52     public static final Color JavaDoc teal = new Color JavaDoc(0x00, 0x80, 0x80);
53     public static final Color JavaDoc white = Color.white;
54     public static final Color JavaDoc yellow = Color.yellow;
55
56     /**
57      * Color map maps Color values to HTML 4.0 color names
58      */

59     private static Map JavaDoc colorMap = new HashMap JavaDoc();
60
61     static {
62         colorMap.put(black, "black");
63         colorMap.put(silver, "silver");
64         colorMap.put(gray, "gray");
65         colorMap.put(white, "white");
66         colorMap.put(maroon, "maroon");
67         colorMap.put(red, "red");
68         colorMap.put(purple, "purple");
69         colorMap.put(fuchsia, "fuchsia");
70         colorMap.put(green, "green");
71         colorMap.put(lime, "lime");
72         colorMap.put(olive, "olive");
73         colorMap.put(yellow, "yellow");
74         colorMap.put(navy, "navy");
75         colorMap.put(blue, "blue");
76         colorMap.put(teal, "teal");
77         colorMap.put(aqua, "aqua");
78     }
79
80     /**
81      * @param generatorContext used by converter to handle precision
82      * or to create elements.
83      */

84     public SVGColor(SVGGeneratorContext generatorContext) {
85         super(generatorContext);
86     }
87
88     /**
89      * Converts part or all of the input GraphicContext into
90      * a set of attribute/value pairs and related definitions
91      *
92      * @param gc GraphicContext to be converted
93      * @return descriptor of the attributes required to represent
94      * some or all of the GraphicContext state, along
95      * with the related definitions
96      * @see org.apache.batik.svggen.SVGDescriptor
97      */

98     public SVGDescriptor toSVG(GraphicContext gc) {
99         Paint JavaDoc paint = gc.getPaint();
100         return toSVG((Color JavaDoc)paint, generatorContext);
101     }
102
103     /**
104      * Converts a Color object to a set of two corresponding
105      * values: a CSS color string and an opacity value.
106      */

107     public static SVGPaintDescriptor toSVG(Color JavaDoc color, SVGGeneratorContext gc) {
108         //
109
// First, convert the color value
110
//
111
String JavaDoc cssColor = (String JavaDoc)colorMap.get(color);
112         if (cssColor==null) {
113             // color is not one of the predefined colors
114
StringBuffer JavaDoc cssColorBuffer = new StringBuffer JavaDoc(RGB_PREFIX);
115             cssColorBuffer.append(color.getRed());
116             cssColorBuffer.append(COMMA);
117             cssColorBuffer.append(color.getGreen());
118             cssColorBuffer.append(COMMA);
119             cssColorBuffer.append(color.getBlue());
120             cssColorBuffer.append(RGB_SUFFIX);
121             cssColor = cssColorBuffer.toString();
122         }
123
124         //
125
// Now, convert the alpha value, if needed
126
//
127
float alpha = color.getAlpha()/255f;
128
129         String JavaDoc alphaString = gc.doubleString(alpha);
130
131         return new SVGPaintDescriptor(cssColor, alphaString);
132     }
133 }
134
Popular Tags