Beautifl - Flash Gallery

Thumbnail : Voronoi + Rendering
Voronoi + Rendering
FLASHMAFIA 2014-03-03 MIT License

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;

    [SWF(width = '465', height = '465')]
    public class VoronoLUX extends Sprite
    {
        private const NUM_P : uint = 28;

        private var voronorov : Voronorov;
        private var ctrlp : Vector.<ControlPoint>;
        private var type : uint = 0;
        private var fcnt : uint;
        private var uiLayer : Sprite;

        function VoronoLUX()
        {
            stage.stageFocusRect = mouseEnabled = tabEnabled = tabChildren = false;
            stage.scaleMode = 'noScale';
            stage.align = 'TL';
            stage.quality = 'low';
            stage.frameRate = 32;
            opaqueBackground = 0x0;
            
            ctrlp = new Vector.<ControlPoint>(NUM_P, true);

            voronorov = new Voronorov(256, 256, ctrlp);
            voronorov.width = 465;
            voronorov.height = 465;
            addChild(voronorov);

            addChild(uiLayer = new Sprite());
            uiLayer.blendMode = 'overlay';
            uiLayer.mouseEnabled = false;

            var i : uint = ctrlp.length;
            while (i-- != 0) {
                var cp : ControlPoint = new ControlPoint();
                uiLayer.addChild(addChild(cp));
                ctrlp[i] = cp;
            }

            stage.addEventListener(Event.ENTER_FRAME, oef);
        }

        private function oef(e : Event) : void
        {
            fcnt++;

            if ((fcnt & 31) == 1) {
                type++;
                if (type > 4) type = 0;
            }

            for each (var cp : ControlPoint in ctrlp)
            {
                if (cp.dragged == false)
                {
                    cp.x += cp.vx;
                    cp.y += cp.vy;

                    if ((cp.x > 465) || (cp.x < 0)) cp.vx = -cp.vx;
                    if ((cp.y > 465) || (cp.y < 0)) cp.vy = -cp.vy;
                }
                cp.sx = cp.x * 0.5;
                cp.sy = cp.y * 0.5;
            }

            voronorov.update(type);
        }
    }
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;


class Voronorov extends Bitmap {
    private var w : int;
    private var h : int;
    private var ctrlp : Vector.<ControlPoint>;
    private var colors : Vector.<uint>;

    function Voronorov(w : uint, h : uint, ctrlp : Vector.<ControlPoint>) {
        this.w = w;
        this.h = h;

        super(new BitmapData(w, h, false));

        this.ctrlp = ctrlp;

        colors = new Vector.<uint>(ctrlp.length, true);

        var i : uint = colors.length;
        while (i-- != 0) colors[i] = new HSV(50 * (i / colors.length), 0.125, 1.0).rgb;
    }

    public function update(type : uint) : void {
        switch(type) {
            case 0:
                draw0();
                break;
            case 1:
                draw1();
                break;
            case 2:
                draw2();
                break;
            case 3:
                draw3();
                break;
            case 4:
                draw4();
                break;
        }
    }

    private function draw0() : void {
        trace('draw0: ' + (draw0));
        var num : uint = ctrlp.length;

        for (var x : int = 0; x < w; x++) {
            for (var y : int = 0; y < h; y++) {
                var min : int = 1024 * 1024;
                var min2 : int = 1024 * 1024;
                var min3 : int = 1024 * 1024;
                var min4 : int = 1024 * 1024;

                for (var i : uint = 0; i < num; i++) {
                    var dx : int = x - ctrlp[i].sx;
                    var dy : int = y - ctrlp[i].sy;

                    dx *= dx;
                    dy *= dy;

                    var dist : int = dx + dy;
                    if (min4 > dist) min4 = dist;
                    if (min3 > min4) {
                        min4 = min3;
                        min3 = dist;
                    }
                    if (min2 > min3) {
                        min3 = min2;
                        min2 = dist;
                    }
                    if (min > min2) {
                        min2 = min;
                        min = dist;
                    }
                }

                min = Math.sqrt(min);
                min2 = Math.sqrt(min2);
                min3 = Math.sqrt(min3);
                min4 = Math.sqrt(min4);

                var c : int = 1 * min4 + 1 * min3 + 18 * min2 - 8 * min;
                c = (c >> 2) - 4;
                if (c > 0xF8) c = 0xF8;
                if (c < 0x08) c = 0x08;

                bitmapData.setPixel(x, y, ((c >> 1) - 2) | (((c >> 1) << 8) + 4) | (((c >> 1) << 16)) + 6);
            }
        }
    }

    private function draw1() : void {
        trace('draw1: ' + (draw1));
        var num : uint = ctrlp.length;

        for (var x : int = 0; x < w; x++) {
            for (var y : int = 0; y < h; y++) {
                var min : int = 1024 * 1024;
                var min2 : int = 1024 * 1024;
                var min3 : int = 1024 * 1024;
                var min4 : int = 1024 * 1024;

                for (var i : uint = 0; i < num; i++) {
                    var dx : int = x - ctrlp[i].sx;
                    var dy : int = y - ctrlp[i].sy;

                    if (dx < 0) dx *= -1;
                    if (dy < 0) dy *= -1;

                    var dist : int = dx + dy;
                    if (min4 > dist) min4 = dist;
                    if (min3 > min4) {
                        min4 = min3;
                        min3 = dist;
                    }
                    if (min2 > min3) {
                        min3 = min2;
                        min2 = dist;
                    }
                    if (min > min2) {
                        min2 = min;
                        min = dist;
                    }
                }

                var c : int = 1 * min4 + 1 * min3 + 18 * min2 - 8 * min;
                c = (c >> 2) - 32;
                if (c > 0xF8) c = 0xF8;
                if (c < 0x08) c = 0x08;

                bitmapData.setPixel(x, y, (c - 2) | ((c << 8) + 4) | ((c << 16) + 6));
            }
        }
    }

    private function draw2() : void {
        trace('draw2: ' + (draw2));
        var num : uint = ctrlp.length;

        for (var x : int = 0; x < w; x++) {
            for (var y : int = 0; y < h; y++) {
                var min : int = 1024 * 1024;
                var min2 : int = 1024 * 1024;
                var min3 : int = 1024 * 1024;
                var min4 : int = 1024 * 1024;

                for (var i : uint = 0; i < num; i++) {
                    var dx : int = x - ctrlp[i].sx;
                    var dy : int = y - ctrlp[i].sy;

                    dx *= dx;
                    dy *= dy;

                    var dist : int = dx + dy;
                    if (min4 > dist) min4 = dist;
                    if (min3 > min4) {
                        min4 = min3;
                        min3 = dist;
                    }
                    if (min2 > min3) {
                        min3 = min2;
                        min2 = dist;
                    }
                    if (min > min2) {
                        min2 = min;
                        min = dist;
                    }
                }

                min = Math.sqrt(min);
                min2 = Math.sqrt(min2);
                min3 = Math.sqrt(min3);
                min4 = Math.sqrt(min4);

                var c : int = -2 * min4 - 2 * min3 + 8 * min2 + 5 * min;
                c = 255 - ((c >> 1) + 64);
                if (c > 0xF8) c = 0xF8;
                if (c < 0x08) c = 0x08;

                bitmapData.setPixel(x, y, (c >> 1) | (((c >> 1) << 8) + 4) | (((c >> 1) << 16) + 6));
            }
        }
    }

    private function draw3() : void {
        trace('draw3: ' + (draw3));
        var num : uint = ctrlp.length;

        for (var x : int = 0; x < w; x++) {
            for (var y : int = 0; y < h; y++) {
                var min : int = 1024 * 1024;
                var min2 : int = 1024 * 1024;
                var min3 : int = 1024 * 1024;
                var min4 : int = 1024 * 1024;

                for (var i : uint = 0; i < num; i++) {
                    var dx : int = x - ctrlp[i].sx;
                    var dy : int = y - ctrlp[i].sy;

                    dx *= dx;
                    dy *= dy;

                    var dist : int = dx + dy;
                    if (min4 > dist) min4 = dist;
                    if (min3 > min4) {
                        min4 = min3;
                        min3 = dist;
                    }
                    if (min2 > min3) {
                        min3 = min2;
                        min2 = dist;
                    }
                    if (min > min2) {
                        min2 = min;
                        min = dist;
                    }
                }

                min = Math.sqrt(min);
                min2 = Math.sqrt(min2);
                min3 = Math.sqrt(min3);
                min4 = Math.sqrt(min4);

                var c : int = 1 * min3 * min2 + (min >> 1);
                c = (c >> 1) - 0x40;
                if (c > 0xF8) c = 0xF8;
                if (c < 0x08) c = 0x08;
                c ^= 0xFF;

                bitmapData.setPixel(x, y, (c - 2) | ((c << 8) + 4) | ((c << 16) + 6));
            }
        }
    }

    private function draw4() : void {
        trace('draw4: ' + (draw4));
        var num : uint = ctrlp.length;

        for (var x : int = 0; x < w; x++) {
            for (var y : int = 0; y < h; y++) {
                var min : int = 1024 * 1024;
                var min2 : int = 1024 * 1024;
                var min3 : int = 1024 * 1024;
                var min4 : int = 1024 * 1024;

                for (var i : uint = 0; i < num; i++) {
                    var dx : int = x - ctrlp[i].sx;
                    var dy : int = y - ctrlp[i].sy;

                    dx *= dx;
                    dy *= dy;

                    var dist : int = dx + dy;

                    if (min4 > dist) min4 = dist;

                    if (min3 > min4) {
                        min4 = min3;
                        min3 = dist;
                    }

                    if (min2 > min3) {
                        min3 = min2;
                        min2 = dist;
                        var no : uint = i;
                    }

                    if (min > min2) {
                        min2 = min;
                        min = dist;
                    }
                }

                min = Math.sqrt(min);
                min2 = Math.sqrt(min2);
                min3 = Math.sqrt(min3);
                min4 = Math.sqrt(min4);

                var c : int = 0xFF - (-1 * min4 - 60 * min3 * (min2 >> 8) + 10 * min2 + 1 * min);
                if (c > 0xF0) c = 0xF8;
                if (c < 0x08) c = 0x04;

                c = ((((colors[no] & 0xFF) * c) >> 8) - 2) | ((((((colors[no] & 0xFF00) >> 8) * c) >> 8) << 8) + 4) | ((((((colors[no] & 0xFF0000) >> 16) * c) >> 8) << 16) + 6);

                bitmapData.setPixel(x, y, c);
            }
        }
    }
}

internal class ControlPoint extends Sprite {
    public var sx : Number;
    public var sy : Number;
    public var vx : Number;
    public var vy : Number;
    public var dragged : Boolean;

    function ControlPoint() {
        this.x = 465 * Math.random();
        this.y = 465 * Math.random();

        vx = 1.0 + 3.0 * Math.random();
        if (Math.random() > 0.5) vx = -vx;

        vy = 1.0 + 3.0 * Math.random();
        if (Math.random() > 0.5) vy = -vy;

        buttonMode = true;
        addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        onMouseUp(null);
    }

    private function draw(c : uint, opacity : Number) : void {
        graphics.clear();

        graphics.lineStyle(4, 0x0, 1.0, false, 'none', 'none');
        graphics.moveTo(-6, -6);
        graphics.lineTo(6, 6);
        graphics.moveTo(6, -6);
        graphics.lineTo(-6, 6);
    }

    private function onMouseUp(event : MouseEvent) : void {
        dragged = false;
        draw(0xFFFFFF, 0.33);
        stopDrag();
    }

    private function onMouseDown(event : MouseEvent) : void {
        dragged = true;
        draw(0xFFFFFF, 1.0);
        startDrag(true);
    }
}

internal class V2D {
    public var x : Number = 0.0;
    public var y : Number = 0.0;
}

internal class HSV {
    private var r : uint;
    private var g : uint;
    private var b : uint;
    private var h : Number;
    private var s : Number;
    private var v : Number;

    function HSV(h : Number = 0.0, s : Number = 1.0, v : Number = 1.0) {
        this.h = h;
        this.s = s;
        this.v = v;
    }

    public function get rgb() : uint {
        if (s > 0) {
            var h : Number = (h < 0) ? ((h % 360) + 360) : ((h % 360) / 60);

            if (h < 1) {
                r = (255 * v) >> 0;
                g = (255 * v * (1 - s * (1 - h))) >> 0;
                b = (255 * v * (1 - s)) >> 0;
            } else if (h < 2) {
                r = (255 * v * (1 - s * (h - 1))) >> 0;
                g = (255 * v) >> 0;
                b = (255 * v * (1 - s)) >> 0;
            } else if (h < 3) {
                r = (255 * v * (1 - s)) >> 0;
                g = (255 * v) >> 0;
                b = (255 * v * (1 - s * (3 - h))) >> 0;
            } else if (h < 4) {
                r = (255 * v * (1 - s)) >> 0;
                g = (255 * v * (1 - s * (h - 3))) >> 0;
                b = (255 * v) >> 0;
            } else if (h < 5) {
                r = (255 * v * (1 - s * (5 - h))) >> 0;
                g = (255 * v * (1 - s)) >> 0;
                b = (255 * v) >> 0;
            } else {
                r = (255 * v) >> 0;
                g = (255 * v * (1 - s)) >> 0;
                b = (255 * v * (1 - s * (h - 5))) >> 0;
            }
        } else {
            r = g = b = (255 * v) >> 0;
        }

        return r << 16 | g << 8 | b ;
    }
}