ColorFader Class

Here’s a class I used for building Mercury’s transition effect between vehicles. Starting from senocular’s ColorFader class, I’ve rewritten it to use the ColorTransform classes instead of Color / setRGB. It’s pretty good and relatively fast.

Mercury Vehicles

I’ll put up some examples shortly of usage, but I’ve got a project that I wish to use as an example that will give you better ideas on how to use it so you’ll have to wait for it.

/**
* @author: Béla Korcsog
* @modifications:
* @date: Jan 2007
* @description: Based initially on senocular’s ColorFader Class. Used to transition between current color and new color. Will use the movieclip’s current color as a starting point.
*  
*/
import flash.geom.ColorTransform;
import flash.geom.Transform;
import com.bkorcsog.BroadCaster;

class com.bkorcsog.ColorFader extends ColorTransform {
 
    // movieclip reference
    private var mc:MovieClip;
 
    // current rgb values
    private var __rgb:Number;
 
    // color holder objects
    private var current_col:Object;
 private var new_col:Object;
 
    // Interval Handler Values
    private var interval_id:Number;
 private var delay_ms:Number = 33;
 
 // interval value holders
    private var steps_left:Number; // number of updates remaining for a fade
 private var steps_total:Number; // total number of updates being used for a fade
 
    // constructor
 function ColorFader(mc){
  super(); //
  this.mc = mc;
  this.mc.cfID = this;
       
        // create mc.trans object if it doesn’t exist
        if (mc.trans == undefined) {
            mc.trans = new Transform(mc);
  }
       
        // if you set a global ‘currentColor’ set the movieclip to that color first thing
  if (_global.currentColor != undefined) {
   this.setTo(_global.currentColor);
  }
 }
   
    /**
   
    */
 public function fadeTo (hexTo, duration, optdelay_ms):Void {
  clearInterval(interval_id)
  
        // get movieclips current transformation values and create the hex value associated with those transformed values
        __rgb = redOffset <<16 | greenOffset << 8 | blueOffset;
  
        // set up current color values
        current_col = {r:redOffset, g:greenOffset, b:blueOffset, hex:rgb};
  
        // set up new color values
        new_col = {r:hexTo>>16, g:(hexTo >> 8)&0xff, b:hexTo&0xff, hex:hexTo};
       
        // start the interval going — if you passed in a custom interval ‘optdelay_ms’ use it otherwise use the default
  var interval = (optdelay_ms != undefined) ? optdelay_ms : delay_ms;

  // set up total steps and step holder value
        steps_left = steps_total = Math.ceil(duration/interval);

        // start the interval drawing the effect on the movieclip
        interval_id = setInterval(this, “fade”, interval);
 }
 private function fade():Void {
  if (steps_left){
   // if there are steps left to render
            steps_left–;
   
            // find the percentage of completion
            var t = 1 – steps_left/steps_total;
   
   // set the percentage of the color value to render
            this.greenOffset = current_col.g+(new_col.g-current_col.g)*t
   this.redOffset = current_col.r+(new_col.r-current_col.r)*t
   this.blueOffset = current_col.b+(new_col.b-current_col.b)*t;
   
  } else {
   // if completed clear interval and broadcast event
           
            clearInterval(interval_id);
   BroadCaster.broadcastEvent(“colorTransitionComplete”);
  }
  
        // transform color of movieclip
        //mc.trans = new Transform(mc);
        mc.trans.colorTransform = this;
 }
 
 public function setTo(hexTo):Void {
        // set the movieclip directly without fade
       
  this.new_col = {r:hexTo>>16, g:(hexTo >> 8)&0xff, b:hexTo&0xff, hex:hexTo};
       
  this.greenOffset = new_col.g;
  this.redOffset = new_col.r;
  this.blueOffset = new_col.b;
       
  // transform color of movieclip
        //mc.trans = new Transform(mc);
        mc.trans.colorTransform = this;
 }
   
    public function stop() {
        clearInterval(interval_id);
    }
}

About Bela Korcsog

Proud father of two children, happy husband to one wife. I've been programming various technologies and leading the development of huge projects for most of the last ten years. I've got some specific likes and dislikes through my experiences in the web site business but generally I'm pretty straightforward about it. Not a huge fan of the latest and greatest shiny toy (it took me four years to show an interest in Flash) I'm more than happy to code in any language that comes along (Actionscript is just so darn fun).
This entry was posted in Flash. Bookmark the permalink.

Comments are closed.