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

Binary ChessBoards

click stage
Get Adobe Flash player
by Aquioux 13 Nov 2012
/**
 * Copyright Aquioux ( http://wonderfl.net/user/Aquioux )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1b6O
 */

package {
//    import aquioux.display.colorUtil.CycleRGB;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#999999")]
    /**
     * 
     * @see "Alt.Fractals: A Visual Guide to Fractal Geometry and Design" P.187
     *       http://amzn.to/nb2zaC
     * @see  http://wonderfl.net/c/zmlI
     * @author Aquioux(Yoshida, Akio)
     */
    public class Main extends Sprite {
        // ビューア
        private var bmdWidth_:int;    // 幅
        private var bmdHeight_:int;    // 高
        private var bmd_:BitmapData;    // ビューア

        // 底と指数
        private const BASE:int = 2;        // 底
        private const EXPONENT:int = 7;    // 指数

        // 描画のための Rectangle
        private var rect_:Rectangle;

        // 塗り分けの色
        private var colorList_:Vector.<uint>;    // 色リスト
        private var currentColorIdx_:int;        // カレント色インデックス
        
        // 切り替えスイッチ
        private var sw_:Boolean;
        

        // コンストラクタ
        public function Main() {
            setup();
            stage.addEventListener(MouseEvent.CLICK, update);
            update(null);
        }
        
        private function setup():void {
            // 階乗計算
            var size:int = 0;
            for (var i:int = 0; i < EXPONENT; i++) size += Math.pow(BASE, i);

            // ビューアサイズ決定
            bmdWidth_ = bmdHeight_ = size * 2;
            
            // ビューア生成
            bmd_ = new BitmapData(bmdWidth_, bmdHeight_, false, 0x0);
            var bm:Bitmap = new Bitmap(bmd_);
            bm.scaleX = stage.stageWidth  / bmdWidth_;
            bm.scaleY = stage.stageHeight / bmdHeight_;
            addChild(bm);
            
            // 描画用 Rectangle 生成
            rect_ = new Rectangle();
            
            // 色リスト生成
            colorList_ = new Vector.<uint>(BASE, true);
        }
        
        private function update(e:MouseEvent):void {
            // 切り替えスイッチ
            sw_ = !sw_;
            
            // 描画色設定
            var rate:Number = BASE;
            if (Math.random() < 0.5) rate *= 2;
            var dist:int = 360 / rate;
            var start:int = (Math.random() * 12 >> 0) * 15;
            for (var i:int = 0; i < BASE; i++) colorList_[i] = CycleRGB.getColor(dist * i + start);

            // 各変数初期化
            var xLen:int = bmdWidth_;
            var yLen:int = bmdHeight_;
            var accumulation:int = 0;    // edge の累積
            currentColorIdx_ = 0;
            
            // 描画ループ
            for (i = 0; i < EXPONENT; i++) {
                if (sw_) currentColorIdx_++;
                var edge:int = Math.pow(BASE, i);
                for (var y:int = accumulation; y < yLen; y += edge) {
                    currentColorIdx_++;
                    for (var x:int = accumulation; x < xLen; x += edge) {
                        rect_.x = x;
                        rect_.y = y;
                        rect_.width = rect_.height = edge;
                        currentColorIdx_ %= BASE;
                        bmd_.fillRect(rect_, colorList_[currentColorIdx_]);
                        currentColorIdx_++;
                    }
                }
                accumulation += edge;
                xLen -= accumulation;
                yLen -= accumulation;
            }
        }
    }
}

//package aquioux.display.colorUtil {
    /**
     * コサインカーブで色相環的な RGB を計算
     * @author Aquioux(YOSHIDA, Akio)
     */
    /*public*/ class CycleRGB {
        /**
         * 32bit カラーのためのアルファ値(0~255)
         */
        static public function get alpha():uint { return _alpha; }
        static public function set alpha(value:uint):void {
            _alpha = (value > 0xFF) ? 0xFF : value;
        }
        private static var _alpha:uint = 0xFF;
    
        private static const PI:Number = Math.PI;        // 円周率
        private static const DEGREE120:Number  = PI * 2 / 3;    // 120度(弧度法形式)
        
        /**
         * 角度に応じた RGB を得る
         * @param    angle    HSV のように角度(度数法)を指定
         * @return    色(0xNNNNNN)
         */
        public static function getColor(angle:Number):uint {
            var radian:Number = angle * PI / 180;
            var r:uint = (Math.cos(radian)             + 1) * 0xFF >> 1;
            var g:uint = (Math.cos(radian + DEGREE120) + 1) * 0xFF >> 1;
            var b:uint = (Math.cos(radian - DEGREE120) + 1) * 0xFF >> 1;
            return r << 16 | g << 8 | b;
        }
        
        /**
         * 角度に応じた RGB を得る(32bit カラー)
         * @param    angle    HSV のように角度(度数法)を指定
         * @return    色(0xNNNNNNNN)
         */
        public static function getColor32(angle:Number):uint {
            return _alpha << 24 | getColor(angle);
        }
    }
//}