1 /**********************************************
2 * Copyright (C) 2010 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import org.vectomatic.dom.svg.impl.SVGPaintParser;
33 import org.vectomatic.dom.svg.utils.SVGConstants;
34
35 import com.google.gwt.core.client.JavaScriptException;
36
37 /**
38 * <p>The {@link org.vectomatic.dom.svg.OMSVGColor} interface corresponds
39 * to color value definition for properties <code>stop-color</code>, <code>flood-color</code>
40 * and <code>lighting-color</code> and is a base class for interface {@link
41 * org.vectomatic.dom.svg.OMSVGPaint}. It incorporates SVG's extended notion
42 * of color, which incorporates ICC-based color specifications.</p> <p>Interface
43 * {@link org.vectomatic.dom.svg.OMSVGColor} does <em>not</em> correspond
44 * to the <a href='types.html#DataTypeColor'><color></a> basic data
45 * type. For the <a href='types.html#DataTypeColor'><color></a> basic
46 * data type, the applicable DOM interfaces are defined in <a href='http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/'>DOM
47 * Level 2 Style</a>; in particular, see the <code>RGBColor</code> interface
48 * ([<a href='refs.html#ref-DOM2STYLE'>DOM2STYLE</a>], section 2.2).</p> <p>Note:
49 * The {@link org.vectomatic.dom.svg.OMSVGColor} interface is deprecated,
50 * and may be dropped from future versions of the SVG specification.</p>
51 */
52 public abstract class OMSVGColor extends OMCSSValue {
53 /**
54 * The color type is not one of predefined types. It is invalid to attempt
55 * to define a new value of this type or to attempt to switch an existing
56 * value to this type.
57 */
58 public static final short SVG_COLORTYPE_UNKNOWN = 0;
59 /**
60 * An sRGB color has been specified without an alterICC color specification.
61 */
62 public static final short SVG_COLORTYPE_RGBCOLOR = 1;
63 /**
64 * An sRGB color has been specified along with an alterICC color specification.
65 */
66 public static final short SVG_COLORTYPE_RGBCOLOR_ICCCOLOR = 2;
67 /**
68 * Corresponds to when keyword <span class='attr-value'>currentColor</span>
69 * has been specified.
70 */
71 public static final short SVG_COLORTYPE_CURRENTCOLOR = 3;
72
73 protected short colorType;
74 protected OMRGBColor rgbColor;
75 protected OMSVGICCColor iccColor;
76
77 protected OMSVGColor() {
78 super(CSS_CUSTOM);
79 }
80
81 // Implementation of the svg::SVGColor W3C IDL interface
82 /**
83 * The type of the value as specified by one of the SVG_COLORTYPE_ constants
84 * defined on this interface.
85 */
86 public final short getColorType() {
87 return this.colorType;
88 }
89 /**
90 * The color specified in the sRGB color space.
91 */
92 public final OMRGBColor getRgbColor() {
93 return rgbColor;
94 }
95 /**
96 * The alternate ICC color specification.
97 */
98 public final OMSVGICCColor getIccColor() {
99 return iccColor;
100 }
101 /**
102 * Modifies the color value to be the specified sRGB color without an alternate
103 * ICC color specification.
104 * @param rgbColor A string that matches <a href='types.html#DataTypeColor'><color></a>,
105 * which specifies the new sRGB color value.
106 * @throws SVGException(SVG_INVALID_VALUE_ERR) Raised if <var>rgbColor</var>
107 * does not match <a href='types.html#DataTypeColor'><color></a>.
108 */
109 public final void setRGBColor(String rgbColor) throws JavaScriptException {
110 setColor(SVG_COLORTYPE_RGBCOLOR, rgbColor, null);
111 }
112 /**
113 * Modifies the color value to be the specified sRGB color with an alternate
114 * ICC color specification.
115 * @param rgbColor A string that matches <a href='types.html#DataTypeColor'><color></a>,
116 * which specifies the new sRGB color value.
117 * @param iccColor A string that matches <a href='types.html#DataTypeICCColor'><icccolor></a>,
118 * which specifies the alternate ICC color specification.
119 * @throws SVGException(SVG_INVALID_VALUE_ERR) Raised if <var>rgbColor</var>
120 * does not match <a href='types.html#DataTypeColor'><color></a> or
121 * if <var>iccColor</var> does not match <a href='types.html#DataTypeICCColor'><icccolor></a>.
122 */
123 public final void setRGBColorICCColor(String rgbColor, String iccColor) throws JavaScriptException {
124 setColor(SVG_COLORTYPE_RGBCOLOR_ICCCOLOR, rgbColor, iccColor);
125 }
126 /**
127 * Sets the color value as specified by the parameters. If <var>colorType</var>
128 * requires an <code>RGBColor</code>, then <var>rgbColor</var> must be a string
129 * that matches <a href='types.html#DataTypeColor'><color></a>; otherwise,
130 * <var>rgbColor</var>. must be null. If <var>colorType</var> requires an
131 * {@link org.vectomatic.dom.svg.OMSVGICCColor}, then <var>iccColor</var>
132 * must be a string that matches <a href='types.html#DataTypeICCColor'><icccolor></a>;
133 * otherwise, <var>iccColor</var> must be null.
134 * @param colorType One of the defined constants for {@link org.vectomatic.dom.svg.OMSVGColor#getColorType()}.
135 * @param rgbColor The specification of an sRGB color, or null.
136 * @param iccColor The specification of an ICC color, or null.
137 * @throws SVGException(SVG_INVALID_VALUE_ERR) Raised if one of the parameters
138 * has an invalid value.
139 */
140 public final void setColor(short colorType, String rgbColor, String iccColor) throws JavaScriptException {
141 // GWT.log("OMSVGColor.setColor(" + colorType + ", '" + rgbColor + "', '" + iccColor + "')");
142 if (colorType == SVG_COLORTYPE_RGBCOLOR_ICCCOLOR && iccColor != null && rgbColor != null) {
143 this.iccColor = SVGPaintParser.INSTANCE.iccColor(iccColor);
144 this.rgbColor = SVGPaintParser.INSTANCE.rgbColor(rgbColor);
145 setCssText(rgbColor.trim() + " " + iccColor);
146 } else if (colorType == SVG_COLORTYPE_RGBCOLOR && rgbColor != null && iccColor == null) {
147 this.iccColor = null;
148 this.rgbColor = SVGPaintParser.INSTANCE.rgbColor(rgbColor);
149 setCssText(rgbColor);
150 } else if (colorType == SVG_COLORTYPE_CURRENTCOLOR && rgbColor == null && iccColor == null) {
151 this.iccColor = null;
152 this.rgbColor = null;
153 setCssText(SVGConstants.CSS_CURRENTCOLOR_VALUE);
154 } else if (colorType == SVG_COLORTYPE_UNKNOWN && rgbColor == null && iccColor == null) {
155 this.iccColor = null;
156 this.rgbColor = null;
157 setCssText(SVGConstants.CSS_NONE_VALUE);
158 } else {
159 throw new JavaScriptException("Invalid color spec");
160 }
161 this.colorType = colorType;
162 }
163
164 }