001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on August 15, 2004, 2:51 AM
035 */
036
037package com.kitfox.svg.animation;
038
039import com.kitfox.svg.SVGElement;
040import com.kitfox.svg.SVGException;
041import com.kitfox.svg.SVGLoaderHelper;
042import com.kitfox.svg.animation.parser.AnimTimeParser;
043import com.kitfox.svg.xml.ColorTable;
044import com.kitfox.svg.xml.StyleAttribute;
045import java.awt.Color;
046import org.xml.sax.Attributes;
047import org.xml.sax.SAXException;
048
049
050/**
051 * @author Mark McKay
052 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
053 */
054public class AnimateColor extends AnimateBase implements AnimateColorIface
055{
056    public static final String TAG_NAME = "animateColor";
057    
058    
059    private Color fromValue;
060    private Color toValue;
061    
062    /** Creates a new instance of Animate */
063    public AnimateColor()
064    {
065    }
066
067    public String getTagName()
068    {
069        return TAG_NAME;
070    }
071
072    public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) throws SAXException
073    {
074                //Load style string
075        super.loaderStartElement(helper, attrs, parent);
076
077        String strn = attrs.getValue("from");
078        fromValue = ColorTable.parseColor(strn);
079
080        strn = attrs.getValue("to");
081        toValue = ColorTable.parseColor(strn);
082    }
083
084    
085    /**
086     * Evaluates this animation element for the passed interpolation time.  Interp
087     * must be on [0..1].
088     */
089    public Color evalColor(double interp)
090    {
091        int r1 = fromValue.getRed();
092        int g1 = fromValue.getGreen();
093        int b1 = fromValue.getBlue();
094        int r2 = toValue.getRed();
095        int g2 = toValue.getGreen();
096        int b2 = toValue.getBlue();
097        double invInterp = 1.0 - interp;
098        
099        return new Color((int)(r1 * invInterp + r2 * interp), 
100            (int)(g1 * invInterp + g2 * interp), 
101            (int)(b1 * invInterp + b2 * interp));
102    }
103
104    protected void rebuild(AnimTimeParser animTimeParser) throws SVGException
105    {
106        super.rebuild(animTimeParser);
107
108        StyleAttribute sty = new StyleAttribute();
109
110        if (getPres(sty.setName("from")))
111        {
112            String strn = sty.getStringValue();
113            fromValue = ColorTable.parseColor(strn);
114        }
115
116        if (getPres(sty.setName("to")))
117        {
118            String strn = sty.getStringValue();
119            toValue = ColorTable.parseColor(strn);
120        }
121    }
122
123    /**
124     * @return the fromValue
125     */
126    public Color getFromValue()
127    {
128        return fromValue;
129    }
130
131    /**
132     * @param fromValue the fromValue to set
133     */
134    public void setFromValue(Color fromValue)
135    {
136        this.fromValue = fromValue;
137    }
138
139    /**
140     * @return the toValue
141     */
142    public Color getToValue()
143    {
144        return toValue;
145    }
146
147    /**
148     * @param toValue the toValue to set
149     */
150    public void setToValue(Color toValue)
151    {
152        this.toValue = toValue;
153    }
154}