In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

colour transitions

Here is a simple colour transition example I coded when I should have been doing an illustration... oh well, I have about 10 hrs until the illustration is due so no worries.

It just steps the colours... play with the steps and the colours to see if I messed up.
Get Adobe Flash player
by NME 12 May 2015
//By Anthony R Pace a.k.a NME
//contact me at ap13mo@student.ocadu.ca
//All rights reserved
package {
    import flash.utils.setInterval;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var startColour:uint;
            var endColour:uint;
            var steps:uint = 10;
            var g:Graphics;
            var colourVec:Vector.<uint>;
            var shapeW:uint = stage.stageWidth/steps;
            var shapeH:uint = stage.stageHeight/steps;
            //for later manipulation, should I choose it 
            //making the swatch shapes we will be drawing/filling
            var shapeVec:Vector.<Shape> =  new Vector.<Shape>(steps,true);
            for(var i:uint = 0; i!=steps;i++){
                shapeVec[i] = new Shape();
            }
            var intervalDuration:uint = 1000;
            var intervalID:Number = setInterval(
                    //anonymous function that gets called based on the interval
                    function():void{
                        startColour = Math.random()*0xffffff;
                        endColour =  Math.random()*0xffffff;
                        colourVec = transitionColours(steps,startColour,endColour);
                        
                        for(var i:uint = 0; i!=steps;i++){
                            g = shapeVec[i].graphics;
                            g.clear();
                            g.beginFill(colourVec[i],1);
                            g.drawRect(i*shapeW,i*shapeH,shapeW,shapeH);
                            stage.addChild(shapeVec[i]); 
                        }
                },//this is where we set the interval parameter in ms
                intervalDuration);
        }
        public function transitionColours(maxSteps:uint,startClr:uint,endClr:uint):Vector.<uint>{
            //I could save space by making it so only the step colours are returned
            //but for now, I will just return them in the vecotor
            var c1:Vector.<uint> = seperateRGB(startClr);
            var c2:Vector.<uint> = seperateRGB(endClr);
            var transitionVec:Vector.<uint>;        
            if(maxSteps <2){
                maxSteps = 2;
            }
            transitionVec = new Vector.<uint>(maxSteps,true);
            //the equation is pretty simple... c1 + t*(c2-c1)/(n-1)           
            var msn1:uint = maxSteps-1;//max steps negative 1
            //since we will be reusing these numbers, lets store it
            //to prevent additional overhead
            var rFac:Number = (c2[0]-c1[0])/msn1;
            var gFac:Number = (c2[1]-c1[1])/msn1;
            var bFac:Number = (c2[2]-c1[2])/msn1;
            //make the Vector array to be returned 
            transitionVec[0]=startClr;         
            for (var i:uint = 1;i!=msn1;i++){
                //takes the R,G,B and stores it as 0xrrggbb..
                transitionVec[i]= (uint(c1[0]+i*rFac)<< 16 ) | ( uint(c1[1]+i*gFac) << 8 ) | uint(c1[2]+i*bFac);
            }
            transitionVec[i] = endClr;
            return transitionVec;
        }
        public function seperateRGB(RGB:uint):Vector.<uint>{
            var v:Vector.<uint> = new Vector.<uint>(3,true);
            v[0] = RGB>>16; //get only the red value out of 0xrrggbb by right shifting >> 16 places in binary, or bits
            v[1] = (RGB>>8)&0xff;//get only the blue value by right shiting >>  8 bits, and doing an exclusive AND to indicate the bits you want
            v[2] = RGB&0xff;
            return v;
        }
    }
}