decolorエフェクト

by shohei909 forked from NowLoading ライブラリ (diff: 154)
徐々に色あせていく写真。アハ体験風。

image:
http://www.flickr.com/photos/horiavarlan/4267398696/#/
♥2 | Line 232 | Modified 2010-11-03 14:47:18 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zCSw
 */

// forked from shohei909's NowLoading ライブラリ
// forked from shohei909's NowLoading
 
 
package {
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.system.LoaderContext;
    import flash.net.URLRequest;
    import flash.display.Sprite;
    import caurina.transitions.Tweener;
    import com.bit101.components.FPSMeter;
    
    [SWF(frameRate = "60")]
    public class FlashTest extends Sprite {
        private var loader:Loader = new Loader();
        private var nowLoading:NowLoading = new NowLoading(onLoaded);
        
        private var source:BitmapData = new BitmapData(465,465,true,0xFF000000);
        private var decolored:BitmapData;
        private var bitmapData:BitmapData;
        public var rate:Number = 0;
         
        public function FlashTest(){
            
            //ロードを開始する
            loader.load( 
                new URLRequest("http://farm3.static.flickr.com/2548/4267398696_7736f96a75_z.jpg"),
                new LoaderContext(true)
            );
            
            //ローダーを登録する
            nowLoading.addLoader(loader);
            
            addChild( nowLoading );
        }
        
        //ロードが完了したときに呼び出す
        private function onLoaded():void{
            //ロード画面を消す
            Tweener.addTween( nowLoading,{alpha:0,time:3,onComplete:function():void{removeChild(nowLoading)} });
            
            init();
        }
        
        //初期化
        private function init():void{
            var rect:Rectangle = RabBit.autoSizeDraw( source, loader );
            decolored =  source.clone();
            RabBit.decolor( decolored, decolored, rect, 0xFFC88A4B, 1 );
            var b:BitmapData = bitmapData = source.clone();
            addChild( new Bitmap( b ) );
            new FPSMeter( this )
            
            Tweener.addTween( 
                this,
                {
                    rate: 255,
                    time: 100,
                    onUpdate: function _():void{
                        var p:Point = new Point(b.rect.x, b.rect.y)
                        b.copyPixels( source, b.rect, p ); 
                        b.merge( decolored, b.rect, p, rate, rate, rate, rate  );
                    }
                }
            )
            
        }
    }
}

import flash.geom.Matrix;
import flash.geom.Rectangle;
class RabBit{
    static public const ALING_CENTER:int = 0;
    static public const ALING_LEFT:int = 1;
    static public const ALING_RIGHT:int = 2;
    static public const ALING_TOP:int = 3;
    static public const ALING_BOTTOM:int = 6;
    
    static public function autoSizeDraw( bitmapData:BitmapData, sourceImage:*, rect:Rectangle=null, aling:int = 0 ):Rectangle {
        if(! rect ){ rect = bitmapData.rect; }
        var rw:int = rect.width;
        var rh:int = rect.height;
        var sw:int = sourceImage.width;
        var sh:int = sourceImage.height;
        
        var rate:Number = rw / sw; 
        var rateH:Number = rh / sh; 
        rate = rate < rateH ? rate : rateH ;
        
        var nw:int = sw * rate;
        var nh:int = sh * rate;
        
        var x:int = rect.x,y:int = rect.y;
        switch( aling % 3 ){
            case ALING_LEFT: 
                x += 0;
                break;
            case ALING_RIGHT: 
                x += ( rw - nw ) >>0;
                break;
            default:
                x += ( rw - nw ) >> 1;
        }
        switch( ((aling/3)>>0 % 3) * 3 ){
            case ALING_TOP: 
                y += 0;
                break;
            case ALING_BOTTOM: 
                y += ( rh - nh ) >> 0;
                break;
            default:
                y += ( rh - nh ) >> 1;
        }
            
        var mtr:Matrix = new Matrix( rate, 0, 0, rate, x, y );
        bitmapData.draw( sourceImage, mtr );
        
        return new Rectangle(x,y,nw,nh);
    }
    
    
    static public function decolor( output:BitmapData, source:BitmapData, rect:Rectangle = null, color:uint = 0, rate:Number = 1 ):void{
        if(! rect ){ rect = source.rect; }
        var a:uint = (color >>> 24) % 0x100;
        var r:uint = (color >>> 16) % 0x100;
        var g:uint = (color >>> 8) % 0x100;
        var b:uint = (color >>> 0) % 0x100;
        
        var vec:Vector.<uint> = source.getVector( rect );
        var l:uint = vec.length
        var a2:uint = ( a << 24 );
        if( rate >= 1 ){
           for( var i:int = 0; i < l; i++){
                var c:uint = vec[i];
                var r2:uint = (c >>> 16) % 0x100;
                var g2:uint = (c >>> 8) % 0x100;
                var b2:uint = (c >>> 0) % 0x100;
                var level:Number = (r2 + g2 + b2) / 0x300;
                vec[i] = a2 + ( (level * r) << 16 ) + ( (level * g) << 8 ) + ( (level * b) << 0 )
            }
        }else if( rate <= 0 ){
            
        }else{
           var rev:Number = 1 - rate; 
           for( i = 0; i < l; i++){
                c = vec[i];
                a2 = (c >>> 24) % 0x100;
                r2 = (c >>> 16) % 0x100;
                g2 = (c >>> 8) % 0x100;
                b2 = (c >>> 0) % 0x100;
                level = (r2 + g2 + b2) / 0x300;
                vec[i] = ( ( rate * a + rev * a2) << 24 )
                            + ( ( level * rate * r + rev * r2) << 16 )
                            + ( ( level * rate * g + rev * g2) << 8 ) 
                            + ( ( level * rate * b + rev * b2) << 0 )
            }
        }
        output.setVector( rect, vec );
    }


}



//NowLoadingライブラリ===============================================================
import flash.display.Loader;
import flash.media.Sound;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.net.URLLoader;
class NowLoading extends Sprite {
    static public const COMPLETE:String = "complete";
    private var loaders:Vector.<Object> = new Vector.<Object>;
    private var bytesTotal:uint=0,bytesLoaded:uint=0;
    private var sprite:ProgressSprite;
    
    private var _loaderNum:uint=0, _completedNum:uint=0, _openNum:uint=0;
    private var text:Bitmap;
    private var onLoaded:Function;
    
    private var LETTER:Object = {
        "1":[[0,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"2":[[1,1,1],[0,0,1],[0,1,1],[1,0,0],[1,1,1]],"3":[[1,1,1],[0,0,1],[1,1,1],[0,0,1],[1,1,1]],"4":[[1,0,1],[1,0,1],[1,0,1],[1,1,1],[0,0,1]],"5":[[1,1,1],[1,0,0],[1,1,1],[0,0,1],[1,1,1]],
        "6":[[1,1,1],[1,0,0],[1,1,1],[1,0,1],[1,1,1]],"7":[[1,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"8":[[1,1,1],[1,0,1],[1,1,1],[1,0,1],[1,1,1]],"9":[[1,1,1],[1,0,1],[1,1,1],[0,0,1],[0,0,1]],"0":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],
        ".":[[0],[0],[0],[0],[1]]," ":[[0],[0],[0],[0],[0]],"n":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,0,1]],"w":[[0,0,0,0,0],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,1],[1,1,1,1,1]],"o":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1]],
        "a":[[0,0,0],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"l":[[1],[1],[1],[1],[1]],"i":[[1],[0],[1],[1],[1]],"d":[[0,0,1],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"g":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1],[0,0,1],[1,1,1]],
        "C":[[1,1,1],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"O":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],"M":[[1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1]],"P":[[1,1,1],[1,0,1],[1,1,1],[1,0,0],[1,0,0]],
        "T":[[1,1,1],[0,1,0],[0,1,0],[0,1,0],[0,1,0]],"L":[[1,0,0],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"E":[[1,1,1],[1,0,0],[1,1,1],[1,0,0],[1,1,1]]
    }
    
    public function NowLoading( onLoaded:Function = null, progressSprite:ProgressSprite = null ){
        this.onLoaded = onLoaded;
        if ( progressSprite == null ) {
            sprite = new ProgressSprite();
        }else{
            sprite = progressSprite;
        }
        
        text = new Bitmap( new BitmapData(30 * 4, 8, true, 0x00000000 ) ); 
        
        addChild(sprite).blendMode = "invert"; 
        addChild(text).blendMode = "invert"; 
        
        addEventListener( Event.ADDED_TO_STAGE, init );
    }
    
    private function init(e:Event):void{
        removeEventListener( Event.ADDED_TO_STAGE, init );
        with (text) { 
            scaleX = scaleY = 1; 
            x = stage.stageWidth - text.width;
            y = stage.stageHeight - text.height;
        }
    }
    
    public function addLoader(loader:Loader):Loader{ setListener(loader.contentLoaderInfo);_loaderNum++;return loader;}
    public function addURLLoader(loader:URLLoader):URLLoader{setListener(loader); _loaderNum++; return loader;}
    public function addSound(sound:Sound):Sound{setListener(sound); _loaderNum++; return sound;}
    
    private function setListener(loader:*):void{
        loader.addEventListener("open", onOpen);
        loader.addEventListener("complete", onComplete);
        loader.addEventListener("progress", update);
    }
    
    private function update(e:Event=null):void{
        bytesLoaded=0; bytesTotal=0;
        for each(var loadObj:Object in loaders){
            bytesLoaded += loadObj.bytesLoaded;
            bytesTotal += loadObj.bytesTotal;
        };
        sprite.progress( bytesLoaded/bytesTotal * _openNum / _loaderNum );
        if (bytesTotal != 0) {
            setText( "now loading... " + (bytesLoaded / bytesTotal * _openNum / _loaderNum * 100).toFixed(1) );
        }
    }
    
    private function onOpen(e:Event):void { 
        e.currentTarget.removeEventListener("open", onOpen); 
        _openNum++;
        loaders.push(e.currentTarget);
        bytesTotal += e.currentTarget.bytesTotal;
    }
    
    private function onComplete(e:Event):void {  
        e.currentTarget.removeEventListener("complete", onComplete);
        e.currentTarget.removeEventListener("progress", update);
        _completedNum++; 
        if (_loaderNum == _completedNum) { 
            setText( "COMPLETE" );
            if ( onLoaded != null ) { onLoaded() } 
        } 
    }
    
    
    private function setText(str:String):void{
        var b:BitmapData = text.bitmapData; var l:int = str.length; var position:int = b.width;
        b.lock();b.fillRect(b.rect,0x000000);
        for(var i:int=0;i<l;i++){
            var letterData:Array = LETTER[str.substr(l-i-1,1)];position-=letterData[0].length+1;
            for(var n:int=0;n<letterData.length;n++){ for(var m:int=0;m<letterData[n].length;m++){ 
                if(letterData[n][m]==1){b.setPixel32(m+position,n+1,0xFF000000);} 
            } }
        }
        b.unlock();
    }
}
class ProgressSprite extends Sprite{
    public function progress(rate:Number):void {
        if( stage != null ){
            graphics.clear();
            graphics.beginFill( 0x000000, 1 )
            graphics.drawRect( 0, stage.stageHeight - 11, rate * stage.stageWidth, 11);
        }
    }
}
//NowLoadingライブラリここまで==============================================================

Forked