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    @Override
068    public String getTagName()
069    {
070        return TAG_NAME;
071    }
072
073    @Override
074    public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) throws SAXException
075    {
076                //Load style string
077        super.loaderStartElement(helper, attrs, parent);
078
079        String strn = attrs.getValue("from");
080        fromValue = ColorTable.parseColor(strn);
081
082        strn = attrs.getValue("to");
083        toValue = ColorTable.parseColor(strn);
084    }
085
086    
087    /**
088     * Evaluates this animation element for the passed interpolation time.  Interp
089     * must be on [0..1].
090     */
091    public Color evalColor(double interp)
092    {
093        int r1 = fromValue.getRed();
094        int g1 = fromValue.getGreen();
095        int b1 = fromValue.getBlue();
096        int r2 = toValue.getRed();
097        int g2 = toValue.getGreen();
098        int b2 = toValue.getBlue();
099        double invInterp = 1.0 - interp;
100        
101        return new Color((int)(r1 * invInterp + r2 * interp), 
102            (int)(g1 * invInterp + g2 * interp), 
103            (int)(b1 * invInterp + b2 * interp));
104    }
105
106    @Override
107    protected void rebuild(AnimTimeParser animTimeParser) throws SVGException
108    {
109        super.rebuild(animTimeParser);
110
111        StyleAttribute sty = new StyleAttribute();
112
113        if (getPres(sty.setName("from")))
114        {
115            String strn = sty.getStringValue();
116            fromValue = ColorTable.parseColor(strn);
117        }
118
119        if (getPres(sty.setName("to")))
120        {
121            String strn = sty.getStringValue();
122            toValue = ColorTable.parseColor(strn);
123        }
124    }
125
126    /**
127     * @return the fromValue
128     */
129    public Color getFromValue()
130    {
131        return fromValue;
132    }
133
134    /**
135     * @param fromValue the fromValue to set
136     */
137    public void setFromValue(Color fromValue)
138    {
139        this.fromValue = fromValue;
140    }
141
142    /**
143     * @return the toValue
144     */
145    public Color getToValue()
146    {
147        return toValue;
148    }
149
150    /**
151     * @param toValue the toValue to set
152     */
153    public void setToValue(Color toValue)
154    {
155        this.toValue = toValue;
156    }
157}