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

forked from: ブレンドモード

Get Adobe Flash player
by aobyrne 22 Mar 2012
    Embed
// forked from rsakane's ブレンドモード
package
{
    import flash.display.*
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.geom.Matrix;
    import flash.net.URLRequest;
    import flash.system.*
    import com.bit101.components.*;
    import flash.utils.Timer;
    
    public class BlendModeGutsExplorer extends Sprite
    {
        private const WIDTH:int = 250;
        private const HEIGHT:int = 250;
        
        private var window:Window;
        private var bitmap:Bitmap;
        private var bd:BitmapData;
        private var color:int = 0xFF9900;
        private var code:Text;
        private var py:int = 0;
        private var px:int = 0;
        private var w:int = WIDTH;
        private var h:int = HEIGHT;
        private var _currentFunction:String;
        private var functions:Array;
        private var isAutomatic:Boolean=true;
        
        public function BlendModeGutsExplorer()
        {    
            window = new Window(this, 0, 0, "Blend Mode");
            window.width = window.height = 465;
            
            //Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
            //var loader:Loader = new Loader();
            //loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
            //loader.load(new URLRequest("http://farm3.static.flickr.com/2342/2216487374_496cb5af46.jpg"), new LoaderContext(true)); // 読み込みたい画像URL
            initHandler(null);
        }
        
        private function initHandler(event:Event):void
        {
            //var loader:Loader = event.currentTarget.loader;
 
            //var matrix:Matrix = new Matrix();
            //matrix.scale(WIDTH / loader.width, HEIGHT / loader.height);
 
            bd = new BitmapData(WIDTH, HEIGHT);
            //bd.draw(loader, matrix);
            bd.perlinNoise(100, 100, 1,int(Math.random()*100), false, true, 1,true);
            var destbd:BitmapData = bd.clone();
            bitmap = new Bitmap(destbd);
            bitmap.x = (465 - bitmap.width) / 2 - 10;
            bitmap.y = 0;
            window.content.addChild(bitmap);
            
            code = new Text(window.content, 230, 320, "");
            code.width = 230;
            code.height = 120;
            
            var label:Label = new Label(window.content, 5, 260, "Color");
            var chooser:ColorChooser = new ColorChooser(window.content, 5, 280, 0xFF9900, chooserHandler);
            chooser.value = this.color = Math.random() * 0xffffff;
            chooser.usePopup = true;
            
            var radioButtonWidow:Window = new Window(window.content, 360, 215, "Size");
            var full:RadioButton = new RadioButton(radioButtonWidow.content, 5, 5, "Full", true, radioButtonHandler);
            var center:RadioButton = new RadioButton(radioButtonWidow.content, 5, 20, "CenterA", false, radioButtonHandler);
            var center2:RadioButton = new RadioButton(radioButtonWidow.content, 5, 35, "CenterB", false, radioButtonHandler);
            var top:RadioButton = new RadioButton(radioButtonWidow.content, 5, 50, "Top", false, radioButtonHandler);
            var bottom:RadioButton = new RadioButton(radioButtonWidow.content, 5, 65, "Bottom", false, radioButtonHandler);
            
            var addButton:PushButton = new PushButton(window.content, 5, 320, "Add", add); addButton.toggle = true;
            var multipleButton:PushButton = new PushButton(window.content, 5, 340, "Multiple", multiple);multipleButton.toggle = true;
            var differenceButton:PushButton = new PushButton(window.content, 5, 360, "Difference", difference);differenceButton.toggle = true;
            var screenButton:PushButton = new PushButton(window.content, 5, 380, "Screen", screen);screenButton.toggle = true;
            var exclusionButton:PushButton = new PushButton(window.content, 5, 400, "Exclusion", exclusion);exclusionButton.toggle = true;
            var overlayButton:PushButton = new PushButton(window.content, 5, 420, "Overlay", overlay);overlayButton.toggle = true;
            var dodgeButton:PushButton = new PushButton(window.content, 118, 320, "Dodge", dodge);dodgeButton.toggle = true;
            var burnButton:PushButton = new PushButton(window.content, 118, 340, "Burn", burn);burnButton.toggle = true;
            var darkenButton:PushButton = new PushButton(window.content, 118, 360, "Darken", darken);darkenButton.toggle = true;
            var lightenButton:PushButton = new PushButton(window.content, 118, 380, "Lighten", lighten);lightenButton.toggle = true;
            var softLightButton:PushButton = new PushButton(window.content, 118, 400, "Softlight", softlight);softLightButton.toggle = true;
            var hardLightButton:PushButton = new PushButton(window.content, 118, 420, "Hardlight", hardlight); hardLightButton.toggle = true;
            
            new CheckBox(window.content, 10, 10, 'automatic', onAutomatic).selected = true;
            setRandomBlendMode();
            this[currentFunction](null);
            setToggle(true);
            var timer:Timer = new Timer(3000);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
        }
        
        private function onTimer(e:TimerEvent):void 
        {
            if (isAutomatic) 
            {
                setRandomBlendMode();
                setToggle(true);
                this[currentFunction](null);
            }
        }
        
        private function onAutomatic(e:Event):void 
        {
            var checkBox:CheckBox = e.target as CheckBox;
            isAutomatic = checkBox.selected;
        }
        
        private function setToggle(isForcing:Boolean=false):void 
        {
            var pushButton:PushButton;
            var n:int = window.content.numChildren;
            for (var i:int = 0; i < n; i++) 
            {
                var childAt:DisplayObject = window.content.getChildAt(i);
                if (childAt is PushButton) 
                {
                    pushButton = childAt as PushButton;
                    if (pushButton.label.toLowerCase()!=currentFunction) 
                    {
                        pushButton.selected = false;
                    }
                    else if (isForcing) 
                    {
                        pushButton.selected = true;
                    }
                }
            }
        }
        
        private function setRandomBlendMode():void 
        {
            var pushButton:PushButton;
            var n:int = window.content.numChildren;
            functions = [];
            for (var i:int = 0; i < n; i++) 
            {
                var childAt:DisplayObject = window.content.getChildAt(i);
                if (childAt is PushButton) 
                {
                    pushButton = childAt as PushButton;
                    functions[functions.length] = pushButton.label.toLowerCase();
                }
            }
            currentFunction = functions[int(functions.length * Math.random())];
        }
        
        private function radioButtonHandler(event:Event):void
        {
            var button:RadioButton = event.currentTarget as RadioButton;
            if (button.label == "Full") px = 0, py = 0, w = WIDTH, h = HEIGHT;
            else if (button.label == "CenterA") px = WIDTH / 4, py = HEIGHT / 4, w = px * 3, h = py * 3;
            else if (button.label == "CenterB") px = WIDTH / 3, py = HEIGHT / 3, w = px * 2, h = py * 2;
            else if (button.label == "Top") px = 0, py = 0, w = WIDTH, h = HEIGHT / 2;
            else if (button.label == "Bottom") px = 0, py = HEIGHT / 2, w = WIDTH, h = HEIGHT;
            this[currentFunction](null);
        }
        
        private function chooserHandler(event:Event):void
        {
            var chooser:ColorChooser = event.currentTarget as ColorChooser;
            this.color = chooser.value;
            this[currentFunction](null);
        }
        
        private function exclusion(event:Event = null):void
        {
            currentFunction =  'exclusion';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    colorA.r = colorA.r + colorB.r - 2 * colorA.r * colorB.r / 255;
                    colorA.g = colorA.g + colorB.g - 2 * colorA.g * colorB.g / 255;
                    colorA.b = colorA.b + colorB.b - 2 * colorA.b * colorB.b / 255;
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = a + b - 2 * a * b / 255";
        }
        
        private function hardlight(event:Event = null):void
        {
            currentFunction = 'hardlight';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorB.r < 128) colorA.r = colorA.r * colorB.r * 2 / 255;
                    else colorA.r = 2 * (colorA.r + colorB.r - colorA.r * colorB.r / 255) - 255;
                    if (colorB.g < 128) colorA.g = colorA.g * colorB.g * 2 / 255;
                    else colorA.g = 2 * (colorA.g + colorB.g - colorA.g * colorB.g / 255) - 255;
                    if (colorB.b < 128) colorA.b = colorA.b * colorB.b * 2 / 255;
                    else colorA.b = 2 * (colorA.b + colorB.b - colorA.b * colorB.b / 255) - 255;
         
                    if (colorA.r > 255) colorA.r = 255;
                    if (colorA.g > 255) colorA.g = 255;
                    if (colorA.b > 255) colorA.b = 255;
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "if (b < 128)\n   value = a * b * 2 / 255\n" + 
                        "else\n   value = 2 * (a + b - a * b / 255) - 255\n\n" +
                        "if (value > 255) value = 255";
        }
        
        private function softlight(event:Event = null):void
        {
            currentFunction = 'softlight';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorB.r < 128) colorA.r = Math.pow((colorA.r / 255), (255 - colorB.r) / 128) * 255;
                    else colorA.r = Math.pow((colorA.r / 255), 128 / colorB.r) * 255;
                    if (colorB.g < 128) colorA.g = Math.pow((colorA.g / 255), (255 - colorB.g) / 128) * 255;
                    else colorA.g = Math.pow((colorA.g / 255), 128 / colorB.g) * 255;
                    if (colorB.b < 128) colorA.b = Math.pow((colorA.b / 255), (255 - colorB.b) / 128) * 255;
                    else colorA.b = Math.pow((colorA.b / 255), 128 / colorB.b) * 255;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "if (b < 128)\n   value = ((a / 255)^((255 - b) / 128)) * 255\n" +
                        "else\n   value = ((a / 255)^(128 / b)) * 255\n\n" +
                        "(^ = exponentiation)";
        }
        
        private function lighten(event:Event = null):void
        {
            currentFunction = 'lighten' ;
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorA.r <= colorB.r) colorA.r = colorB.r;
                    if (colorA.g <= colorB.g) colorA.g = colorB.g;
                    if (colorA.b <= colorB.b) colorA.b = colorB.b;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = Math.max(a, b)";
        }
        
        private function darken(event:Event = null):void
        {
            currentFunction = 'darken';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorA.r > colorB.r) colorA.r = colorB.r;
                    if (colorA.g > colorB.g) colorA.g = colorB.g;
                    if (colorA.b > colorB.b) colorA.b = colorB.b;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = Math.min(a, b)";
        }
        
        private function overlay(event:Event = null):void
        {
            currentFunction = 'overlay';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorA.r < 128) colorA.r = colorA.r * colorB.r * 2 / 255;
                    else colorA.r = 2 * (colorA.r + colorB.r - colorA.r * colorB.r / 255) - 255;
                    if (colorA.g < 128) colorA.g = colorA.g * colorB.g * 2 / 255;
                    else colorA.g = 2 * (colorA.g + colorB.g - colorA.g * colorB.g / 255) - 255;
                    if (colorA.b < 128) colorA.b = colorA.b * colorB.b * 2 / 255;
                    else colorA.b = 2 * (colorA.b + colorB.b - colorA.b * colorB.b / 255) - 255;
         
                    if (colorA.r > 255) colorA.r = 255;
                    if (colorA.g > 255) colorA.g = 255;
                    if (colorA.b > 255) colorA.b = 255;
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "if (a < 128)\n   value = a * b * 2 / 255\n" +
                        "else\n   value = 2 * (a + b - a * b / 255) - 255\n\n" +
                        "if (value > 255) value = 255";
        }
        
        private function burn(event:Event = null):void
        {
            currentFunction = 'burn';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorB.r == 0) colorA.r = 0;
                    colorA.r = 255 - ((255 - colorA.r) * 255 / colorB.r);
                    if (colorB.g == 0) colorA.g = 0;
                    colorA.g = 255 - ((255 - colorA.g) * 255 / colorB.g);
                    if (colorB.b == 0) colorA.b = 0;
                    colorA.b = 255 - ((255 - colorA.b) * 255 / colorB.b);
         
                    if (colorA.r < 0) colorA.r = 0;
                    if (colorA.g < 0) colorA.g = 0;
                    if (colorA.b < 0) colorA.b = 0;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "if (b == 0)\n   value = 0\n" +
                        "else\n   value = 255 - ((255 - a) * 255 / b)\n\n" +
                        "if (value < 0) value = 0";
        }
        
        private function dodge(event:Event = null):void
        {
            currentFunction = 'dodge';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    if (colorB.r == 255) colorA.r = 255;
                    colorA.r = colorA.r * 255 / (255 - colorB.r);
                    if (colorB.g == 255) colorA.g = 255;
                    colorA.g = colorA.g * 255 / (255 - colorB.g);
                    if (colorB.b == 255) colorA.b = 255;
                    colorA.b = colorA.b * 255 / (255 - colorB.b);
         
                    if (colorA.r > 255) colorA.r = 255;
                    if (colorA.g > 255) colorA.g = 255;
                    if (colorA.b > 255) colorA.b = 255;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "if (b == 255)\n   value = 255\n" +
                        "else\n   value = a * 255 / (255 - b)\n\n" +
                        "if (value > 255) value = 255";
        }
        
        private function screen(event:Event = null):void
        {
            currentFunction = 'screen';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    colorA.r = 255 - ((255 - colorA.r) * (255 - colorB.r)) / 255;
                    colorA.g = 255 - ((255 - colorA.g) * (255 - colorB.g)) / 255;
                    colorA.b = 255 - ((255 - colorA.b) * (255 - colorB.b)) / 255;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = 255 - ((255 - a) * (255 - b)) / 255";
        }
        
        private function difference(event:Event = null):void
        {
            currentFunction = 'difference';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    colorA.r = colorA.r - colorB.r;
                    colorA.g = colorA.g - colorB.g;
                    colorA.b = colorA.b - colorB.b;
         
                    if (colorA.r < 0) colorA.r = 0;
                    if (colorA.g < 0) colorA.g = 0;
                    if (colorA.b < 0) colorA.b = 0;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = a - b\n\n" +
                        "if (value < 0) value = 0";
        }
        
        private function multiple(event:Event = null):void
        {
            currentFunction = 'multiple';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    colorA.r = colorA.r * colorB.r / 255;
                    colorA.g = colorA.g * colorB.g / 255;
                    colorA.b = colorA.b * colorB.b / 255;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = a * b / 255";
        }
        
        private function add(event:Event = null):void
        {
            currentFunction = 'add';
            bitmap.bitmapData = bd.clone();
            var colorA:RGB = new RGB();
            var colorB:RGB = new RGB();
            colorB.value = color;
         
            for (var y:int = py; y < h; y++)
            {
                for (var x:int = px; x < w; x++)
                {
                    colorA.value = bd.getPixel(x, y);
                    colorA.r += colorB.r;
                    colorA.g += colorB.g;
                    colorA.b += colorB.b;
         
                    if (colorA.r > 255) colorA.r = 255;
                    if (colorA.g > 255) colorA.g = 255;
                    if (colorA.b > 255) colorA.b = 255;
         
                    bitmap.bitmapData.setPixel(x, y, colorA.value);
                }
            }
            
            code.text = "value = a + b\n\n" +
                        "if (value > 255) value = 255";
        }
        
        public function get currentFunction():String 
        {
            return _currentFunction;
        }
        
        public function set currentFunction(value:String):void 
        {
            _currentFunction = value;
            if (this[_currentFunction]) setToggle();
        }
    }
}

class RGB
{
    public var r:int = 0, g:int = 0, b:int = 0;
 
    public function set value(pixel:int):void
    {
        this.r = pixel >> 16 & 0xFF;
        this.g = pixel >>  8 & 0xFF;
        this.b = pixel & 0xFF;
    }
 
    public function get value():int
    {        
        return this.r << 16 | this.g << 8 | this.b;
    }
}