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: BitmapDataの実験

BitmapData がアルファを持つとおかしい。
@author jc at bk-zen.com
Get Adobe Flash player
by yonatan 15 Jun 2011
/**
 * Copyright yonatan ( http://wonderfl.net/user/yonatan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xzdH
 */

// forked from bkzen's BitmapDataの実験
package  
{
    import com.bit101.components.PushButton;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.ColorTransform;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.system.Capabilities;
    
    /**
     * BitmapData がアルファを持つとおかしい。
     * @author jc at bk-zen.com
     */
    [SWF (backgroundColor = "0x000000", frameRate = "30", width = "465", height = "465")]
    public class Test57 extends Sprite 
    {
        private var bmd: BitmapData;
        private var colorTf: ColorTransform;
        private var color: uint;
        private var txt:TextField;
        private var test:PushButton;
        
        public function Test57() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(event: Event = null): void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            //var color: uint = 0x49001FFF;
            color = 0xFF001FFF;
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            addChild(new Bitmap(bmd = new BitmapData(465, 465, true, color), "auto", true));
            //colorTf = new ColorTransform(0.98, 0.98, 1, 1, -1, -1, -1);
            colorTf = new ColorTransform(1, 1, 1, 1, -1, -1, -1);
            addChild(txt = new TextField());
            txt.width  = stage.stageWidth;
            txt.height = stage.stageHeight;
            txt.defaultTextFormat = new TextFormat("Courier New, Courier, _typewriter", 12, 0xFFFFFF);
            txt.wordWrap = true;
            txt.filters = [new DropShadowFilter(1, 45, 0, 1, 5, 5, 100)];
            test = new PushButton(this, 0, 0, "Test", onClick);
            test.x = stage.stageWidth  - test.width;
            test.y = stage.stageHeight - test.height;
            onClick();
        }
        
        private function onClick(event: MouseEvent = null): void 
        {
            bmd.fillRect(bmd.rect, color);
            var a: int = (color >> 0x18) & 0xFF;
            var r: int = (color >> 0x10) & 0xFF;
            var g: int = (color >> 0x08) & 0xFF;
            var b: int = (color >> 0x00) & 0xFF;
            var na: int = a * colorTf.alphaMultiplier + colorTf.alphaOffset;
            var nr: int = r * colorTf.redMultiplier   + colorTf.redOffset;
            var ng: int = g * colorTf.greenMultiplier + colorTf.greenOffset;
            var nb: int = b * colorTf.blueMultiplier  + colorTf.blueOffset;
            
                 if (na < 0x00) na = 0x00;
            else if (na > 0xFF) na = 0xFF;
                 if (nr < 0x00) nr = 0x00;
            else if (nr > 0xFF) nr = 0xFF;
                 if (ng < 0x00) ng = 0x00;
            else if (ng > 0xFF) ng = 0xFF;
                 if (nb < 0x00) nb = 0x00;
            else if (nb > 0xFF) nb = 0xFF;
            traceReset();
            atrace("Player version: " + Capabilities.version);
            atrace("");
            atrace(colStr(color, 4, true), "塗る色 - fillRect color");
            atrace(colStr(bmd.getPixel32(1, 1), 4, true), "塗られた色32 - getPixel32 result");
            atrace(colStr(bmd.getPixel(1, 1), 3, true), "塗られた色24 - getPixel result");
            atrace("A: " + colStr(a),  "R: " + colStr(r),  "G: " + colStr(g),  "B: " + colStr(b), "古 - Old");
            atrace("A: " + colStr(na), "R: " + colStr(nr), "G: " + colStr(ng), "B: " + colStr(nb), "新 - Expected new");
            atrace();
            atrace("Applied colorTransform:");
            atrace(colorTf, "を適用");
            atrace();
            atrace(colStr(na << 0x18 | nr << 0x10 | ng << 0x08 | nb << 0x00, 4, true), "計算で求めた色 - Expected result");
            bmd.colorTransform(bmd.rect, colorTf);
            atrace(colStr(bmd.getPixel32(1, 1), 4, true), "ColorTransform を適用した色32 - getPixel32 result");
            atrace(colStr(bmd.getPixel(1, 1), 3, true), "ColorTransform を適用した色24 - getPixel result");
            if ((--a) < 0)
            {
                test.removeEventListener(MouseEvent.CLICK, onClick);
            }
            else 
            {
                color = a << 0x18 | r << 0x10 | g << 0x08 | b << 0x00;
                atrace();
                atrace("Test ボタンをクリックで次の色へ");
                atrace("Click the Test button to test the next color");
            }
        }
        
        private function traceReset():void 
        {
            txt.text = "";
        }
        
        private function atrace(...args): void
        {
            txt.appendText(args.join(", ") + "\n");
        }
        
        /**
         * カラーを 16進数の文字列にする。
         * @param    color    : 対象カラー
         * @param    p        : 
         * @param    isHead    : 
         * @return
         */
        private function colStr(color: uint, p: uint = 1, isHead: Boolean = false): String
        {
            p <<= 1;
            var str: String = color.toString(16).toUpperCase();
            var i: uint, n: uint = p - str.length < 0 ? 0 : p - str.length;
            for (i = 0; i < n; i++) 
            {
                str = "0" + str;
            }
            return (isHead ? "0x" : "") + str;
        }
    }
}