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

color matrix filter

multipart project 3
part 3 of, I don't know, like 5

Background:
There are these colorful transparent sprites, but I'd rather have something that's less attention-grabbing. I think a color matrix filter can do the job, and keep the fine details in the original sprite.

Task:
- apply a ColorMatrixFilter to a sprite
- render it to a BitmapData for later use
- come up with an appropriate matrix
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kLan
 */

// forked from wh0's weighted average color
package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.net.*;
    import flash.system.*;
    import com.actionscriptbible.Example
    public class FlashTest extends Example {

        private static const PROXY:String = 'http://5ivestar.org/proxy/';

        public function FlashTest() {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, error);
            Security.loadPolicyFile(PROXY + 'crossdomain.xml');
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, check);
            l.load(new URLRequest(PROXY + 'http://www.pokestadium.com/pokemon/sprites/handheld/blackwhite/470.png'), new LoaderContext(true));
        }
        
        private function error(e:UncaughtErrorEvent):void {
            trace(e.error);
        }
        
        private function check(e:Event):void {
            var li:LoaderInfo = e.target as LoaderInfo;
            var d:DisplayObject = li.content;
            
            // render with transformation
            var t:Number = -1. / 3.;
            var cmf:ColorMatrixFilter = new ColorMatrixFilter([
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                t, t, t, 1, 0
            ]);
            d.filters = [cmf];
            var bd:BitmapData = new BitmapData(d.width, d.height, true, 0);
            bd.draw(d);
            d.filters = [];
            
            // put out the original and 3 copies of the result
            addChild(d);
            addChild(new Bitmap(bd)).x = d.width;
            addChild(new Bitmap(bd)).y = d.height;
            with(addChild(new Bitmap(bd))) { x = d.width; y = d.height; }
            
            // test on different backgrounds
            graphics.beginFill(0xffa080);
            graphics.drawRect(d.width, 0, d.width, d.height);
            graphics.endFill();
            graphics.beginFill(0xa0ff80);
            graphics.drawRect(0, d.height, d.width, d.height);
            graphics.endFill();
            graphics.beginFill(0x80c0ff);
            graphics.drawRect(d.width, d.height, d.width, d.height);
            graphics.endFill();
        }
        
    }
}