001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.anim.values;
020:
021: import org.apache.batik.dom.anim.AnimationTarget;
022:
023: /**
024: * An SVG paint value in the animation system.
025: *
026: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
027: * @version $Id: AnimatablePaintValue.java 475477 2006-11-15 22:44:28Z cam $
028: */
029: public class AnimatablePaintValue extends AnimatableColorValue {
030:
031: // Constants for paintType.
032: public static final int PAINT_NONE = 0;
033: public static final int PAINT_CURRENT_COLOR = 1;
034: public static final int PAINT_COLOR = 2;
035: public static final int PAINT_URI = 3;
036: public static final int PAINT_URI_NONE = 4;
037: public static final int PAINT_URI_CURRENT_COLOR = 5;
038: public static final int PAINT_URI_COLOR = 6;
039: public static final int PAINT_INHERIT = 7;
040:
041: /**
042: * The type of paint.
043: */
044: protected int paintType;
045:
046: /**
047: * The URI of the referenced paint server.
048: */
049: protected String uri;
050:
051: /**
052: * Creates a new, uninitialized AnimatablePaintValue.
053: */
054: protected AnimatablePaintValue(AnimationTarget target) {
055: super (target);
056: }
057:
058: /**
059: * Creates a new AnimatablePaintValue.
060: */
061: protected AnimatablePaintValue(AnimationTarget target, float r,
062: float g, float b) {
063: super (target, r, g, b);
064: }
065:
066: /**
067: * Creates a new AnimatablePaintValue for a 'none' value.
068: */
069: public static AnimatablePaintValue createNonePaintValue(
070: AnimationTarget target) {
071: AnimatablePaintValue v = new AnimatablePaintValue(target);
072: v.paintType = PAINT_NONE;
073: return v;
074: }
075:
076: /**
077: * Creates a new AnimatablePaintValue for a 'currentColor' value.
078: */
079: public static AnimatablePaintValue createCurrentColorPaintValue(
080: AnimationTarget target) {
081: AnimatablePaintValue v = new AnimatablePaintValue(target);
082: v.paintType = PAINT_CURRENT_COLOR;
083: return v;
084: }
085:
086: /**
087: * Creates a new AnimatablePaintValue for a color value.
088: */
089: public static AnimatablePaintValue createColorPaintValue(
090: AnimationTarget target, float r, float g, float b) {
091: AnimatablePaintValue v = new AnimatablePaintValue(target, r, g,
092: b);
093: v.paintType = PAINT_COLOR;
094: return v;
095: }
096:
097: /**
098: * Creates a new AnimatablePaintValue for a URI reference.
099: */
100: public static AnimatablePaintValue createURIPaintValue(
101: AnimationTarget target, String uri) {
102: AnimatablePaintValue v = new AnimatablePaintValue(target);
103: v.uri = uri;
104: v.paintType = PAINT_URI;
105: return v;
106: }
107:
108: /**
109: * Creates a new AnimatablePaintValue for a URI reference with a
110: * 'none' fallback.
111: */
112: public static AnimatablePaintValue createURINonePaintValue(
113: AnimationTarget target, String uri) {
114: AnimatablePaintValue v = new AnimatablePaintValue(target);
115: v.uri = uri;
116: v.paintType = PAINT_URI_NONE;
117: return v;
118: }
119:
120: /**
121: * Creates a new AnimatablePaintValue for a URI reference with a
122: * 'currentColor' fallback.
123: */
124: public static AnimatablePaintValue createURICurrentColorPaintValue(
125: AnimationTarget target, String uri) {
126: AnimatablePaintValue v = new AnimatablePaintValue(target);
127: v.uri = uri;
128: v.paintType = PAINT_URI_CURRENT_COLOR;
129: return v;
130: }
131:
132: /**
133: * Creates a new AnimatablePaintValue for a URI reference with a
134: * color fallback.
135: */
136: public static AnimatablePaintValue createURIColorPaintValue(
137: AnimationTarget target, String uri, float r, float g,
138: float b) {
139: AnimatablePaintValue v = new AnimatablePaintValue(target, r, g,
140: b);
141: v.uri = uri;
142: v.paintType = PAINT_URI_COLOR;
143: return v;
144: }
145:
146: /**
147: * Creates a new AnimatablePaintValue for a 'inherit' value.
148: */
149: public static AnimatablePaintValue createInheritPaintValue(
150: AnimationTarget target) {
151: AnimatablePaintValue v = new AnimatablePaintValue(target);
152: v.paintType = PAINT_INHERIT;
153: return v;
154: }
155:
156: /**
157: * Performs interpolation to the given value.
158: */
159: public AnimatableValue interpolate(AnimatableValue result,
160: AnimatableValue to, float interpolation,
161: AnimatableValue accumulation, int multiplier) {
162: AnimatablePaintValue res;
163: if (result == null) {
164: res = new AnimatablePaintValue(target);
165: } else {
166: res = (AnimatablePaintValue) result;
167: }
168:
169: if (paintType == PAINT_COLOR) {
170: boolean canInterpolate = true;
171: if (to != null) {
172: AnimatablePaintValue toPaint = (AnimatablePaintValue) to;
173: canInterpolate = toPaint.paintType == PAINT_COLOR;
174: }
175: if (accumulation != null) {
176: AnimatablePaintValue accPaint = (AnimatablePaintValue) accumulation;
177: canInterpolate = canInterpolate
178: && accPaint.paintType == PAINT_COLOR;
179: }
180: if (canInterpolate) {
181: res.paintType = PAINT_COLOR;
182: return super .interpolate(res, to, interpolation,
183: accumulation, multiplier);
184: }
185: }
186:
187: int newPaintType;
188: String newURI;
189: float newRed, newGreen, newBlue;
190:
191: if (to != null && interpolation >= 0.5) {
192: AnimatablePaintValue toValue = (AnimatablePaintValue) to;
193: newPaintType = toValue.paintType;
194: newURI = toValue.uri;
195: newRed = toValue.red;
196: newGreen = toValue.green;
197: newBlue = toValue.blue;
198: } else {
199: newPaintType = paintType;
200: newURI = uri;
201: newRed = red;
202: newGreen = green;
203: newBlue = blue;
204: }
205:
206: if (res.paintType != newPaintType || res.uri == null
207: || !res.uri.equals(newURI) || res.red != newRed
208: || res.green != newGreen || res.blue != newBlue) {
209: res.paintType = newPaintType;
210: res.uri = newURI;
211: res.red = newRed;
212: res.green = newGreen;
213: res.blue = newBlue;
214: res.hasChanged = true;
215: }
216:
217: return res;
218: }
219:
220: /**
221: * Returns the type of paint this value represents.
222: */
223: public int getPaintType() {
224: return paintType;
225: }
226:
227: /**
228: * Returns the paint server URI.
229: */
230: public String getURI() {
231: return uri;
232: }
233:
234: /**
235: * Returns whether two values of this type can have their distance
236: * computed, as needed by paced animation.
237: */
238: public boolean canPace() {
239: return false;
240: }
241:
242: /**
243: * Returns the absolute distance between this value and the specified other
244: * value.
245: */
246: public float distanceTo(AnimatableValue other) {
247: return 0f;
248: }
249:
250: /**
251: * Returns a zero value of this AnimatableValue's type.
252: */
253: public AnimatableValue getZeroValue() {
254: return AnimatablePaintValue.createColorPaintValue(target, 0f,
255: 0f, 0f);
256: }
257:
258: /**
259: * Returns the CSS text representation of the value.
260: */
261: public String getCssText() {
262: switch (paintType) {
263: case PAINT_NONE:
264: return "none";
265: case PAINT_CURRENT_COLOR:
266: return "currentColor";
267: case PAINT_COLOR:
268: return super .getCssText();
269: case PAINT_URI:
270: return "url(" + uri + ")";
271: case PAINT_URI_NONE:
272: return "url(" + uri + ") none";
273: case PAINT_URI_CURRENT_COLOR:
274: return "url(" + uri + ") currentColor";
275: case PAINT_URI_COLOR:
276: return "url(" + uri + ") " + super .getCssText();
277: default: // PAINT_INHERIT
278: return "inherit";
279: }
280: }
281: }
|