001    /*
002     * Animate.java
003     *
004     *  The Salamander Project - 2D and 3D graphics libraries in Java
005     *  Copyright (C) 2004 Mark McKay
006     *
007     *  This library is free software; you can redistribute it and/or
008     *  modify it under the terms of the GNU Lesser General Public
009     *  License as published by the Free Software Foundation; either
010     *  version 2.1 of the License, or (at your option) any later version.
011     *
012     *  This library is distributed in the hope that it will be useful,
013     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     *  Lesser General Public License for more details.
016     *
017     *  You should have received a copy of the GNU Lesser General Public
018     *  License along with this library; if not, write to the Free Software
019     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020     *
021     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
022     *  projects can be found at http://www.kitfox.com
023     *
024     * Created on August 15, 2004, 2:51 AM
025     */
026    
027    package com.kitfox.svg.animation;
028    
029    import com.kitfox.svg.SVGElement;
030    import com.kitfox.svg.SVGException;
031    import com.kitfox.svg.SVGLoaderHelper;
032    import com.kitfox.svg.animation.parser.AnimTimeParser;
033    import com.kitfox.svg.xml.ColorTable;
034    import com.kitfox.svg.xml.StyleAttribute;
035    import java.awt.Color;
036    import org.xml.sax.Attributes;
037    import org.xml.sax.SAXException;
038    
039    
040    /**
041     * @author Mark McKay
042     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
043     */
044    public class AnimateColor extends AnimateBase implements AnimateColorIface
045    {
046        
047        protected Color fromValue;
048        protected Color toValue;
049        
050        /** Creates a new instance of Animate */
051        public AnimateColor()
052        {
053        }
054        
055        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) throws SAXException
056        {
057                    //Load style string
058            super.loaderStartElement(helper, attrs, parent);
059    
060            String strn = attrs.getValue("from");
061            fromValue = ColorTable.parseColor(strn);
062    
063            strn = attrs.getValue("to");
064            toValue = ColorTable.parseColor(strn);
065        }
066    
067        
068        /**
069         * Evaluates this animation element for the passed interpolation time.  Interp
070         * must be on [0..1].
071         */
072        public Color evalColor(double interp)
073        {
074            int r1 = fromValue.getRed();
075            int g1 = fromValue.getGreen();
076            int b1 = fromValue.getBlue();
077            int r2 = toValue.getRed();
078            int g2 = toValue.getGreen();
079            int b2 = toValue.getBlue();
080            double invInterp = 1.0 - interp;
081            
082            return new Color((int)(r1 * invInterp + r2 * interp), 
083                (int)(g1 * invInterp + g2 * interp), 
084                (int)(b1 * invInterp + b2 * interp));
085        }
086    
087        protected void rebuild(AnimTimeParser animTimeParser) throws SVGException
088        {
089            super.rebuild(animTimeParser);
090    
091            StyleAttribute sty = new StyleAttribute();
092    
093            if (getPres(sty.setName("from")))
094            {
095                String strn = sty.getStringValue();
096                fromValue = ColorTable.parseColor(strn);
097            }
098    
099            if (getPres(sty.setName("to")))
100            {
101                String strn = sty.getStringValue();
102                toValue = ColorTable.parseColor(strn);
103            }
104        }
105    }