001    /*
002     * TrackManager.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 September 21, 2004, 11:34 PM
025     */
026    
027    package com.kitfox.svg.animation;
028    
029    import com.kitfox.svg.xml.StyleAttribute;
030    import java.awt.*;
031    import java.util.*;
032    
033    import com.kitfox.svg.*;
034    import com.kitfox.svg.xml.*;
035    
036    /**
037     * A track holds the animation events for a single parameter of a single SVG
038     * element.  It also contains the default value for the element, should the
039     * user want to see the 'unanimated' value.
040     *
041     * @author Mark McKay
042     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
043     */
044    public class TrackColor extends TrackBase
045    {
046    
047        public TrackColor(AnimationElement ele) throws SVGElementException
048        {
049            super(ele.getParent(), ele);
050        }
051    
052        public boolean getValue(StyleAttribute attrib, double curTime)
053        {
054            Color col = getValue(curTime);
055            if (col == null) return false;
056    
057            attrib.setStringValue("#" + Integer.toHexString(col.getRGB()));
058            return true;
059        }
060    
061        public Color getValue(double curTime)
062        {
063            Color retVal = null;
064            AnimationTimeEval state = new AnimationTimeEval();
065    
066            for (Iterator it = animEvents.iterator(); it.hasNext();)
067            {
068                AnimateBase ele = (AnimateBase)it.next();
069                AnimateColorIface eleColor = (AnimateColorIface)ele;
070                ele.evalParametric(state, curTime);
071    
072                //Reject value if it is in the invalid state
073                if (Double.isNaN(state.interp)) continue;
074    
075                if (retVal == null)
076                {
077                    retVal = eleColor.evalColor(state.interp);
078                    continue;
079                }
080                
081                Color curCol = eleColor.evalColor(state.interp);
082                switch (ele.getAdditiveType())
083                {
084                    case AnimationElement.AD_REPLACE:
085                        retVal = curCol;
086                        break;
087                    case AnimationElement.AD_SUM:
088                        retVal = new Color(curCol.getRed() + retVal.getRed(), curCol.getGreen() + retVal.getGreen(), curCol.getBlue() + retVal.getBlue());
089                        break;
090                }
091            }
092    
093            return retVal;
094        }
095    }