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

webカムからの画像の色補正

webカムからの画像の色補正をする時用
* 
* ColorTransformに送る値を作る。
* 値は次の関係を持っているので、これにより画像を変色させる。
* 新しい red 値 = (古い red 値 * redMultiplier) + redOffset
* 
* スライダーは上から、次の値を設定している。
* redMultiplier
* greenMultiplier
* blueMultiplier
* alphaMultiplier
* redOffset
* greenOffset
* blueOffset
* alphaOffset
* 
* 参考
* http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/geom/ColorTransform.html#ColorTransform()
*
/**
 * Copyright umhr ( http://wonderfl.net/user/umhr )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aOjq
 */

/*
 * webカムからの画像の色補正をする時用
 * 
 * ColorTransformに送る値を作る。
 * 値は次の関係を持っているので、これにより画像を変色させる。
 * 新しい red 値 = (古い red 値 * redMultiplier) + redOffset
 * 
 * スライダーは上から、次の値を設定している。
 * redMultiplier
 * greenMultiplier
 * blueMultiplier
 * alphaMultiplier
 * redOffset
 * greenOffset
 * blueOffset
 * alphaOffset
 * 
 * 参考
 * http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/geom/ColorTransform.html#ColorTransform()
 * */
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.media.Camera;
    import flash.media.Video;
    
    import com.bit101.components.HSlider;
    import com.bit101.components.ColorChooser;
    import com.bit101.components.InputText;
    import com.bit101.components.PushButton;
    
    [SWF(width = "465", height = "465", frameRate = "15", backgroundColor = "#FFFFFF")]
    
    public class Main extends Sprite {
        
        private var _bitmap:Bitmap;
        private var _video:Video;
        
        private var _multiplier:Array;
        private var _offset:Array;
        private var _inputTexts:Array;
        private var _colorChooser:ColorChooser;
        private var _colorChooserClicked:ColorChooser;
        private var _ichimatsu:Sprite;
        
        public function Main() {
            stage.scaleMode = "noScale";
            stage.align = "TL";
            
            //カメラの初期化
            var camera:Camera = Camera.getCamera();
            //カメラの存在を確認
            if (camera) {
                camera.setMode(640, 480, 15);
                _video = new Video(640, 480);
                _video.attachCamera(camera);
            } else {
                trace("カメラが見つかりませんでした。");
                new InputText(this, 0, 0, "Camera not found.");
                
                return;
            }
            
            _bitmap = new Bitmap(new BitmapData(640, 480));
            this.addChild(_bitmap);
            
            _inputTexts = [];
            _multiplier = [];
            for (var i:int = 0; i < 4; i++) {
                var inputText:InputText = new InputText(this, 0, i * 16, "1", onInput);
                inputText.width = 30;
                _inputTexts.push(inputText);
                var slider:HSlider = new HSlider(this, 32, i * 16 + 3, onSlider);
                slider.setSliderParams(0, 4, 1);
                slider.tick = 0.01;
                slider.width = 200;
                slider.alpha = 0.5;
                slider.tabChildren = false;
                _multiplier[i] = slider;
            }
            
            _offset = [];
            for (i = 0; i < 4; i++) {
                var inputText2:InputText = new InputText(this, 0, i * 16 + 16 * 4 + 2, "0", onInput);
                inputText2.width = 30;
                _inputTexts.push(inputText2);
                var slider2:HSlider = new HSlider(this, 32, 16 * i + 16 * 4 + 6, onSlider);
                slider2.setSliderParams(-255, 255, 0);
                slider2.tick = 1;
                slider2.width = 200;
                slider2.alpha = 0.5;
                slider2.tabChildren = false;
                _offset[i] = slider2;
            }
            
            _colorChooser = new ColorChooser(this, 95, 135);
            _colorChooserClicked = new ColorChooser(this, 165, 135);
            var pushButton:PushButton = new PushButton(this, 0, 132, "Reset", atReset);
            pushButton.alpha = 0.5;
            pushButton.width = 60;
            
            _ichimatsu = drawIchimatsu();
            
            this.addEventListener(Event.ENTER_FRAME, atEnter);
            this.stage.addEventListener(MouseEvent.CLICK, atClick);
        }
        
        private function onSlider(event:Event):void {
            for (var i:int = 0; i < 4; i++) {
                _inputTexts[i].text = _multiplier[i].value;
                _inputTexts[i + 4].text = _offset[i].value;
            }
        }
        
        private function onInput(event:Event):void {
            for (var i:int = 0; i < 4; i++) {
                _multiplier[i].value = _inputTexts[i].text;
                _offset[i].value = _inputTexts[i + 4].text;
            }
        }
        
        private function atEnter(event:Event):void {
            var colorTransform:ColorTransform = new ColorTransform();
            colorTransform.redMultiplier = _multiplier[0].value;
            colorTransform.greenMultiplier = _multiplier[1].value;
            colorTransform.blueMultiplier = _multiplier[2].value;
            colorTransform.alphaMultiplier = _multiplier[3].value;
            colorTransform.redOffset = _offset[0].value;
            colorTransform.greenOffset = _offset[1].value;
            colorTransform.blueOffset = _offset[2].value;
            colorTransform.alphaOffset = _offset[3].value;
            
            _bitmap.bitmapData.draw(_ichimatsu);
            _bitmap.bitmapData.draw(_video, null, colorTransform);
            
            _colorChooser.value = _bitmap.bitmapData.getPixel(_bitmap.mouseX,_bitmap.mouseY);
            
        }
        
        private function atClick(event:MouseEvent):void {
            if (event.target == this.stage) {
                _colorChooserClicked.value = _colorChooser.value;
            }
        }
        
        private function atReset(event:Event):void {
            for (var i:int = 0; i < 4; i++) {
                _inputTexts[i].text = 1;
                _inputTexts[i + 4].text = 0;
            }
            onInput(null);
        }
        
        private function drawIchimatsu():Sprite {
            var bitmapData:BitmapData = new BitmapData(2, 2, false);
            bitmapData.setPixel(0, 0, 0xCCCCCC);
            bitmapData.setPixel(1, 1, 0xCCCCCC);
            var sp:Sprite = new Sprite();
            sp.graphics.beginBitmapFill(bitmapData, new Matrix(8, 0, 0, 8));
            sp.graphics.drawRect(0, 0, 640, 480);
            sp.graphics.endFill();
            return sp;
        }
    }
}