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

Desaturation Composition; forked from: 外部画像をビットマップ化する

入力欄に画像URLを入力してください。
* とりあえず、画像(BitmapData)の半分だけグレイスケール処理をかけています。
Get Adobe Flash player
by leichtgewicht 20 Apr 2012
// forked from rsakane's 外部画像をビットマップ化する
/*
 * 入力欄に画像URLを入力してください。
 * とりあえず、画像(BitmapData)の半分だけグレイスケール処理をかけています。
 */
package
{
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.system.*;
    import flash.text.*;
    import flash.utils.*;
    import flash.external.*
    import com.bit101.components.*;
 
    public class Main extends Sprite
    {
        private const SIZE:int = 300;
        
        private var loaderA:Loader;
        private var loaderB:Loader;
        
        private var input:InputText;
        private var _red: HSlider;
        private var _green: HSlider;
        private var _blue: HSlider;
        private const _bitmap: Bitmap = new Bitmap();
        private const _width: int = 465;
        private var _originalBD: BitmapData;
        private const _labels: Dictionary = new Dictionary();
        
        public function Main()
        {
            var label:Label = new Label(this, 25, 409, "IMAGE URL");
            input = new InputText(this, 0, 410, "http://www.fxdirectory.info/wp-content/uploads/2011/05/Red-Parrot-which-has-its-own-uniqueness.jpg");
            input.width = 300;
            input.x = (_width- input.width) / 2;

            _red = createSlider( "red", 350 );
            _green = createSlider( "green", 365);
            _blue = createSlider( "blue", 380);
                        
            addChild( _bitmap );
                        
            var button:PushButton = new PushButton(this, 230, 435, "OK", inputHandler);
            button.x = (_width- button.width) / 2;
            
            inputHandler();
        }

        private function createSlider( name: String, y: Number ): HSlider
        {
            const slider: HSlider = new HSlider(this, 200, y+5, updateSlider );
            slider.setSliderParams( 0, 1, 0.5 );
            slider.draw();
            const nameLabel: Label = new Label( this, 50, y, name );
            const label: Label = new Label( this, 50, y);
            const width: Number = 400;
            
            nameLabel.x = ( _width - width ) / 2;
            slider.x = nameLabel.x + 50;
            label.x = slider.x + 100;
            _labels[slider] = label;
            return slider;
        }

        private function updateSlider( event:Event = null ) : void
        {
            const formerGreen: Number = _green.value;
            const formerRed: Number = _red.value;
            const formerBlue: Number = _blue.value;
            if( event.target == _red )
            {
                const greenBlue: Number = (1-formerRed) / (formerGreen+formerBlue);
                if( isNaN( greenBlue ) )
                {
                    _green.value = _blue.value = (1-formerRed) / 2;
                }    
                else
                {
                    _blue.value =  greenBlue * formerBlue;
                    _green.value = greenBlue * formerGreen;
                }
            }

            if( event.target == _green )
            {
                const redBlue: Number = (1-formerGreen) / (formerBlue+formerRed);
                if( isNaN( redBlue ) )
                {
                    _red.value = _blue.value = (1-formerGreen) / 2;
                }
                else
                {
                    _red.value =  redBlue* formerRed;
                    _blue.value = redBlue* formerBlue;
                }
            }
                
            if( event.target == _blue )
            {
                const redGreen: Number = (1-formerBlue) / (formerGreen+formerRed);
                if( isNaN( redGreen ) )
                {
                    _red.value = _green.value = (1-formerBlue) / 2;
                }
                else
                {
                    _red.value =  redGreen * formerRed;
                    _green.value = redGreen * formerGreen;
                }
            }
            _bitmap.bitmapData = grayscale( _originalBD );
        }
        
        private function inputHandler(event:Event = null):void
        {
            loaderA = new Loader();
            loaderA.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
            loaderA.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function():void { } );
            loaderA.load(new URLRequest(input.text), new LoaderContext(true));
        }
 
        private function initHandler(event:Event):void
        {    
            loaderB = new Loader();
            loaderB.contentLoaderInfo.addEventListener(Event.INIT, init);
            loaderB.loadBytes(loaderA.contentLoaderInfo.bytes);
        }
        
        private function init(event:Event):void
        {
            var loader:Loader = event.currentTarget.loader;
            
            var scale:Number = 0;
            if (loader.width < loader.height) scale = SIZE / loader.height;
            else scale = SIZE / loader.width;
            
            var matrix:Matrix = new Matrix();
            matrix.scale(scale, scale);
            _originalBD = new BitmapData(scale * loader.width, scale * loader.height, false);
            _originalBD.draw(loader, matrix);
            
            _bitmap.bitmapData = grayscale( _originalBD );
            _bitmap.x = (465 - _bitmap.width) / 2;
        }
        
        private function grayscale(bd:BitmapData):BitmapData
        {
            var destbd:BitmapData = bd.clone();
         
            var matrix:Array = new Array();
                        
            var redPart: Number = _red.value;
            var greenPart: Number = _green.value;
            var bluePart: Number = _blue.value;
            
            _labels[_red].text = redPart;
            _labels[_green].text = greenPart;
            _labels[_blue].text = bluePart;
                        
            matrix = matrix.concat([redPart, greenPart, bluePart, 0, 0]);
            matrix = matrix.concat([redPart, greenPart, bluePart, 0, 0]);
            matrix = matrix.concat([redPart, greenPart, bluePart, 0, 0]);
            matrix = matrix.concat([0, 0, 0, 1, 0]);
            destbd.applyFilter(destbd, new Rectangle(0, 0, bd.width, bd.height / 2), new Point(), new ColorMatrixFilter(matrix));
         
            return destbd;
        }
    }
}