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

wallpaper generator

multipart project 3
part 5, finale

Background:
description withheld

Task:
- combine all previous parts

images used without permission.
Get Adobe Flash player
by wh0 13 Mar 2011
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/5abW
 */

package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    
    import com.adobe.images.PNGEncoder;
    import com.bit101.components.*;
    
    [SWF(backgroundColor=0xf0f0f0)]
    public class FlashTest extends Sprite {
        
        private static const S:Number = 10. / 0x4000;
        private static const PROXY:String = 'http://p.jsapp.us/proxy/';
        private static const YES:LoaderContext = new LoaderContext(true);
        private static const W:Number = Capabilities.screenResolutionX;
        private static const H:Number = Capabilities.screenResolutionY;
        private static const W2:Number = W / 2;
        private static const H2:Number = H / 2;
        private static const D:Number = W * W + H * H;
        private static const R:Number = Math.sqrt(W * W + H * H / 4);
        private static const T:Number = -1. / 3.;
        private static const CMF:ColorMatrixFilter = new ColorMatrixFilter([
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            T, T, T, 1, 0
        ]);
        private static const DSF:DropShadowFilter = new DropShadowFilter(10, 90, 0x000000, 0.75, 50, 50, 1, BitmapFilterQuality.HIGH);
        
        private var big:Loader = new Loader();
        private var small:Loader = new Loader();
        private var smallData:BitmapData = new BitmapData(96, 96, true, 0x10ffffff);
        private var numInput:InputText;
        
        public function FlashTest() {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function(e:UncaughtErrorEvent):void { Wonderfl.log(e.error) });
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            var m:Shape = new Shape();
            m.graphics.beginFill(0x000000, 0);
            m.graphics.drawRect(0, 0, W, H);
            m.graphics.endFill();
            addChild(m);
            mask = m;
            
            for (var dx:Number = 0; dx < W2 + 48; dx += 150) {
                drawSmall(W2 + dx, H2);
                if (dx == 0) continue;
                drawSmall(W2 - dx, H2);
            }
            for (var dy:Number = 130; dy < H2 + 48; dy += 260) {
                for (dx = 75; dx < W2 + 48; dx += 150) {
                    drawSmall(W2 + dx, H2 + dy);
                    drawSmall(W2 + dx, H2 - dy);
                    drawSmall(W2 - dx, H2 + dy);
                    drawSmall(W2 - dx, H2 - dy);
                }
                for (dx = 0; dx < W2 + 48; dx += 150) {
                    drawSmall(W2 + dx, H2 + dy + 130);
                    drawSmall(W2 + dx, H2 - dy - 130);
                    if (dx == 0) continue;
                    drawSmall(W2 - dx, H2 + dy + 130);
                    drawSmall(W2 - dx, H2 - dy - 130);
                }
            }
            
            big.filters = [DSF];
            addChild(big);
            big.contentLoaderInfo.addEventListener(Event.COMPLETE, bigComplete);
            small.contentLoaderInfo.addEventListener(Event.COMPLETE, smallComplete);
            
            numInput = new InputText(stage, 4, 4, '470');
            numInput.width = 32;
            numInput.restrict = '0-9';
            numInput.maxChars = 3;
            new PushButton(stage, 40, 4, 'load', load).width = 32;
            new PushButton(stage, 76, 4, 'rand', rand).width = 32;
            new PushButton(stage, 112, 4, 'save', save).width = 32;
            new PushButton(stage, stage.stageWidth - 48, stage.stageHeight - 24, '+', zoomIn).width = 20;
            new PushButton(stage, stage.stageWidth - 24, stage.stageHeight - 24, '-', zoomOut).width = 20;
            zoomIn();
            load();
        }
        
        private function zoomIn(e:MouseEvent=null):void {
            scaleX = scaleY = 1;
            x = (stage.stageWidth - scaleX * W) / 2;
            y = (stage.stageHeight - scaleY * H) / 2;
        }
        
        private function zoomOut(e:MouseEvent=null):void {
            scaleX = scaleY = 0.25;
            x = (stage.stageWidth - scaleX * W) / 2;
            y = (stage.stageHeight - scaleY * H) / 2;
        }
        
        private function drawSmall(x:Number, y:Number):void {
            var b:Bitmap = new Bitmap(smallData, PixelSnapping.ALWAYS);
            b.x = x - 48;
            b.y = y - 48;
            var r:Number = (x - W2) * (x - W2) + (y - H2) * (y - H2) - 40000;
            b.alpha = Math.sqrt(r / D);
            addChild(b);
        }
        
        private function load(e:MouseEvent=null):void {
            graphics.clear();
            smallData.fillRect(smallData.rect, 0x00000000);
            var num:int = parseInt(numInput.text, 10);
            big.load(new URLRequest(PROXY + 'http://i240.photobucket.com/albums/ff85/Pokestadium/sugimori/' + num + '.png'), YES);
            small.load(new URLRequest(PROXY + 'http://www.pokestadium.com/pokemon/sprites/handheld/blackwhite/' + num + '.png'), YES);
        }
        
        private function rand(e:MouseEvent=null):void {
            numInput.text = (Math.floor(Math.random() * 493) + 1).toString();
            load();
        }
        
        private function bigComplete(e:Event):void {
            var bd:BitmapData = (big.content as Bitmap).bitmapData;
            big.x = W2 - bd.width / 2;
            big.y = H2 - bd.height / 2;
            var rect:Rectangle = new Rectangle(0, 0, bd.width, 1);
            var r:Number = 0;
            var g:Number = 0;
            var b:Number = 0;
            var d:Number = 0;
            for (var y:int = 0; y < bd.height; y++) {
                rect.top = y;
                rect.height = 1;
                var data:ByteArray = bd.getPixels(rect);
                data.position = 0;
                for (var x:int = 0; x < bd.width; x++) {
                    var a:int = data.readByte() & 0xff;
                    d += a;
                    r += a * (data.readByte() & 0xff);
                    g += a * (data.readByte() & 0xff);
                    b += a * (data.readByte() & 0xff);
                }
            }
            r /= d;
            g /= d;
            b /= d;
            /*
            var min:Number = Math.min(r, g, b) * 0.8;
            var max:Number = Math.max(r, g, b) * 0.8 + 256 * 0.2;
            var scale:Number = 256. / (max - min);
            r = (r - min) * scale;
            g = (g - min) * scale;
            b = (b - min) * scale;
            */
            drawBack(r, g, b);
        }
        
        private function drawBack(r:Number, g:Number, b:Number):void {
            var min:Number = Math.min(r, g, b);
            var max:Number = Math.max(r, g, b);
            var delta:Number = min * 0.8;
            var dr:Number = r - delta;
            var dg:Number = g - delta;
            var db:Number = b - delta;
            var colors:Array = [
                (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff),
                (dr & 0xff) << 16 | (dg & 0xff) << 8 | (db & 0xff)
            ];
            var alphas:Array = [1, 1];
            var ratios:Array = [0, 255];
            var matrix:Matrix = new Matrix(0, -2 * R * S, R * S, 0, W / 2, H / 2);
            graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB, H / R);
            graphics.drawRect(0, 0, W, H);
            graphics.endFill();
        }
        
        private function smallComplete(e:Event):void {
            small.filters = [CMF];
            smallData.draw(small);
            small.filters = [];
        }
        
        private function save(e:MouseEvent):void {
            var bd:BitmapData = new BitmapData(W, H, false);
            bd.draw(this);
            var ba:ByteArray = PNGEncoder.encode(bd);
            bd.dispose();
            bd = null;
            var file:FileReference = new FileReference();
            file.save(ba, 'sample.png');
        }
        
    }
}