]> git.mjollnir.org Git - moodle.git/blob
171c78e285b21c801873b85b897b147ef396e41f
[moodle.git] /
1 package flare.animate.interpolate
2 {       
3         /**
4          * Interpolator for color (<code>uint</code>) values.
5          */ 
6         public class ColorInterpolator extends Interpolator
7         {
8                 private var _start:uint;
9                 private var _end:uint;
10                 
11                 /**
12                  * Creates a new ColorInterpolator.
13                  * @param target the object whose property is being interpolated
14                  * @param property the property to interpolate
15                  * @param start the starting color value to interpolate from
16                  * @param end the target color value to interpolate to
17                  */
18                 public function ColorInterpolator(target:Object, property:String,
19                                                   start:Object, end:Object)
20                 {
21                         super(target, property, start, end);
22                 }
23                 
24                 /**
25                  * Initializes this interpolator.
26                  * @param start the starting value of the interpolation
27                  * @param end the target value of the interpolation
28                  */
29                 protected override function init(start:Object, end:Object) : void
30                 {
31                         _start = uint(start);
32                         _end = uint(end);
33                 }
34                 
35                 /**
36                  * Calculate and set an interpolated property value.
37                  * @param f the interpolation fraction (typically between 0 and 1)
38                  */
39                 public override function interpolate(f:Number) : void
40                 {
41                         // we'll do all the work here to avoid the overhead of
42                         //  extra method calls (rather than call Colors.interpolate)
43                         var a1:uint, a2:uint, r1:uint, r2:uint, 
44                             g1:uint, g2:uint, b1:uint, b2:uint;
45                         
46                         // get color components
47                         a1 = (_start >> 24) & 0xFF; a2 = (_end >> 24) & 0xFF;
48                         r1 = (_start >> 16) & 0xFF; r2 = (_end >> 16) & 0xFF;
49                         g1 = (_start >>  8) & 0xFF; g2 = (_end >>  8) & 0xFF;
50                         b1 =  _start & 0xff;        b2 =  _end & 0xFF;
51                         
52                         // interpolate the color components
53                         a1 += f*(a2-a1); r1 += f*(r2-r1);
54                         g1 += f*(g2-g1); b1 += f*(b2-b1);
55                         
56                         // recombine into final color
57                         a1 = ((a1 & 0xFF) << 24) | ((r1 & 0xFF) << 16) |
58                                  ((g1 & 0xFF) <<  8) |  (b1 & 0xFF);
59                         
60                         // update the property value
61                         _prop.setValue(_target, a1);
62                 }
63                 
64         } // end of class ColorInterpolator
65 }