package {
    import flash.display.Sprite;
    import flash.events.*;

    [SWF(width="360", height="320", backgroundColor="#ffffff", frameRate="15")]

    public class SepiaCamera extends CameraFramework
    {
        public static const
            VIDEO_WIDTH:int = 320,          ///< ビデオの幅
            VIDEO_HEIGHT:int = 240,         ///< ビデオの高さ
            SCREEN_WIDTH:int = 360,         ///< 画面の幅
            SCREEN_HEIGHT:int = 320,        ///< 画面の高さ
            ADD:int = 20;                   ///< 色相、彩度の変化値

        private var h:int = 220;            ///< 色相
        private var s:int = 220;            ///< 彩度

        /// コンストラクタ
        public function SepiaCamera()
        {
            super( SCREEN_WIDTH, SCREEN_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT );

            /// 表示文字設定
            setText();

            // ボタン追加
            var buttonY:int = 10, buttonW:int = 70;
            var sAddButton:TextButton = new TextButton(buttonW, 20, "彩度+");
            sAddButton.x = 10;
            sAddButton.y = buttonY;
            addChild(sAddButton);
            sAddButton.addEventListener( MouseEvent.CLICK, sAddButtonClick );

            var sDecButton:TextButton = new TextButton(buttonW, 20, "彩度-");
            sDecButton.x = sAddButton.x + sAddButton.width + 10;
            sDecButton.y = buttonY;
            addChild(sDecButton);
            sDecButton.addEventListener( MouseEvent.CLICK, sDecButtonClick );

            var hAddButton:TextButton = new TextButton(buttonW, 20, "色相+");
            hAddButton.x = sDecButton.x + sDecButton.width + 10;
            hAddButton.y = buttonY;
            addChild(hAddButton);
            hAddButton.addEventListener( MouseEvent.CLICK, hAddButtonClick );

            var hDecButton:TextButton = new TextButton(buttonW, 20, "色相-");
            hDecButton.x = hAddButton.x + hAddButton.width + 10;
            hDecButton.y = buttonY;
            addChild(hDecButton);
            hDecButton.addEventListener( MouseEvent.CLICK, hDecButtonClick );

        }

        /// 彩度加算
        private function sAddButtonClick(event:MouseEvent):void {
            s += ADD;
            if( s > 255 ){
                s = 255;
            }
            setText();
        }

        /// 彩度減算
        private function sDecButtonClick(event:MouseEvent):void {
            s -= ADD;
            if( s < 0 ){
                s = 0;
            }
            setText();
        }

        /// 色相加算
        private function hAddButtonClick(event:MouseEvent):void {
            h += ADD;
            if( h > 255 ){
                h = 255;
            }
            setText();
        }

        /// 色相減算
        private function hDecButtonClick(event:MouseEvent):void {
            h -= ADD;
            if( h < 0 ){
                h = 0;
            }
            setText();
        }

        /// 表示文字設定
        private function setText():void {
            label.text = "Sepia Camera: 彩度:" + s + " 色相:" + h;
        }

        /// 描画処理
        public override function draw():void {

            var hsv:HSV = new HSV(0,0,0);

            /// hsvに一回変換して、モノクロ処理。その後RGBに戻して描画
            for( var y:int = 0; y < bdTmp.height; ++y ){
                for( var x:int = 0; x < bdTmp.width; ++x ){
                    var color:uint = bdTmp.getPixel(x , y);
                    //var hsv:HSV = new HSV( (color >> 16) & 0xff, (color >> 8) & 0xff, (color & 0xff) );   //< 一回一回作ってると遅い?
                    hsv.rgb2hsv((color >> 16) & 0xff, (color >> 8) & 0xff, (color & 0xff));
                    hsv.s = s;
                    hsv.h = h;
                    bd.setPixel(x, y, hsv.getRGBColor());
                }
            }
        }

    }
}