1 package com.effects.effectClasses
4 import mx.effects.effectClasses.AnimatePropertyInstance;
5 import com.util.ColorUtil;
10 public class AnimateColorInstance extends AnimatePropertyInstance
12 /** The start color values for each of the r, g, and b channels */
13 protected var startValues:Object;;
15 /** The change in color value for each of the r, g, and b channels. */
16 protected var delta:Object;
21 * @param target The Object to animate with this effect.
23 public function AnimateColorInstance( target:Object )
31 override public function play():void
33 // We need to call play first so that the fromValue is
34 // correctly set, but this has the side effect of calling
35 // onTweenUpdate before startValues or delta can be set,
36 // so we need to check for that in onTweenUpdate to avoid
40 // Calculate the delta for each of the color values
41 startValues = ColorUtil.intToRgb( fromValue );
42 var stopValues:Object = ColorUtil.intToRgb( toValue );
44 r: ( startValues.r - stopValues.r ) / duration,
45 g: ( startValues.g - stopValues.g ) / duration,
46 b: ( startValues.b - stopValues.b ) / duration
54 override public function onTweenUpdate( value:Object ):void
56 // Bail out if delta hasn't been set yet
62 // Catch the situation in which the playheadTime is actually more
63 // than duration, which causes incorrect colors to appear at the
64 // end of the animation.
65 var playheadTime:int = this.playheadTime;
66 if ( playheadTime > duration )
68 // Fix the local playhead time to avoid going past the end color
69 playheadTime = duration;
72 // Calculate the new color value based on the elapased time and the change
74 var colorValue:int = ( ( startValues.r - playheadTime * delta.r ) << 16 )
75 + ( (startValues.g - playheadTime * delta.g ) << 8 )
76 + ( startValues.b - playheadTime * delta.b );
78 // Either set the property directly, or set it as a style
81 target[ property ] = colorValue;
85 target.setStyle( property, colorValue );