initial commit
[CPE_learningsite] / CPEFlex / EngagementPod / src / com / effects / effectClasses / AnimateColorInstance.as
1 package com.effects.effectClasses
2 {
3         
4         import mx.effects.effectClasses.AnimatePropertyInstance;
5         import com.util.ColorUtil;
6         
7         /**
8          * 
9          */
10         public class AnimateColorInstance extends AnimatePropertyInstance
11         {
12                 /** The start color values for each of the r, g, and b channels */
13                 protected var startValues:Object;;
14                 
15                 /** The change in color value for each of the r, g, and b channels. */
16                 protected var delta:Object;
17                 
18                 /**
19                  * Constructor
20                  *
21                  * @param target The Object to animate with this effect.
22                  */
23                 public function AnimateColorInstance( target:Object )
24                 {
25                         super( target );
26                 }
27                 
28                 /**
29                  * @private
30                  */
31                 override public function play():void
32                 {
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
37                         // run time errors.
38                         super.play();
39                         
40                         // Calculate the delta for each of the color values
41                         startValues = ColorUtil.intToRgb( fromValue );
42                         var stopValues:Object = ColorUtil.intToRgb( toValue );
43                         delta = {
44                                 r: ( startValues.r - stopValues.r ) / duration,
45                                         g: ( startValues.g - stopValues.g ) / duration,
46                                         b: ( startValues.b - stopValues.b ) / duration
47                         };
48                         
49                 }
50                 
51                 /**
52                  * @private
53                  */
54                 override public function onTweenUpdate( value:Object ):void
55                 {
56                         // Bail out if delta hasn't been set yet
57                         if ( delta == null )
58                         {
59                                 return;
60                         }
61                         
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 )
67                         {
68                                 // Fix the local playhead time to avoid going past the end color
69                                 playheadTime = duration;
70                         }
71                         
72                         // Calculate the new color value based on the elapased time and the change
73                         // in color values
74                         var colorValue:int = ( ( startValues.r - playheadTime * delta.r ) << 16 )
75                                 + ( (startValues.g - playheadTime * delta.g ) << 8 )
76                                 + ( startValues.b - playheadTime * delta.b );
77                         
78                         // Either set the property directly, or set it as a style
79                         if ( !isStyle )
80                         {
81                                 target[ property ] = colorValue;
82                         }
83                         else
84                         {
85                                 target.setStyle( property, colorValue );
86                         }
87                 }
88                 
89         } // end class
90 } // end package