HSV実験

by esukei
Wikipediaを参考にHSVとRGBの実験をしてみる。
* 参考 : http://ja.wikipedia.org/wiki/HSV色空間
* まずは適当に説明どおりに実装。コードが死ぬほど汚いので後できれいに直す。
♥0 | Line 126 | Modified 2009-03-09 11:51:20 | MIT License
play

ActionScript3 source code

/**
 * Copyright esukei ( http://wonderfl.net/user/esukei )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jc0s
 */

/**
 * Wikipediaを参考にHSVとRGBの実験をしてみる。
 * 参考 : http://ja.wikipedia.org/wiki/HSV色空間
 * まずは適当に説明どおりに実装。コードが死ぬほど汚いので後できれいに直す。
 */
package {
    
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class HSVandRGB extends Sprite {
        
        private var shapeH:Shape;
        private var shapeS:Shape;
        private var shapeV:Shape;
        
        public function HSVandRGB() {
            
            //色相が変わる
            shapeH = new Shape();
            //彩度が変わる
            shapeS = new Shape();
            //明度が変わる
            shapeV = new Shape();
            stage.addChild(shapeH);
            stage.addChild(shapeS);
            stage.addChild(shapeV);
            
            shapeH.x = stage.stageWidth / 2;
            shapeH.y = stage.stageHeight / 2 - 100;
            
            shapeS.x = stage.stageWidth / 2 -100;
            shapeS.y = stage.stageHeight / 2 + 50;
            
            shapeV.x = stage.stageWidth / 2 + 100;
            shapeV.y = stage.stageHeight / 2 + 50;
            
            var tmpH:Number = 0.0;
            var tmpS:Number = 1.0;
            var tmpV:Number = 1.0;
            
            stage.addEventListener(Event.ENTER_FRAME,function(event:Event):void{
                shapeH.graphics.clear();
                shapeS.graphics.clear();
                shapeV.graphics.clear();
                
                tmpH = ( (tmpH+1) % 360.0 + 360.0) % 360.0;
                tmpS -= 0.002;
                tmpS = (tmpS < 0.0) ? tmpS = 1.0 : tmpS;
                tmpV -= 0.002; 
                tmpV = (tmpV < 0.0) ? tmpV = 1.0 : tmpV;
                
                var tmpRGB:Object = HSVtoRGB(tmpH, 1.0, 1.0);
                var setRGB:Number = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
                
                shapeH.graphics.beginFill(setRGB, 1.0);
                shapeH.graphics.drawCircle(0,0,80);
                shapeH.graphics.endFill();
                
                tmpRGB = HSVtoRGB(tmpH, tmpS, 1.0);
                setRGB = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
                
                shapeS.graphics.beginFill(setRGB, 1.0);
                shapeS.graphics.drawCircle(0,0,80);
                shapeS.graphics.endFill();
                
                tmpRGB = HSVtoRGB(tmpH, 1.0, tmpV);
                setRGB = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
                
                shapeV.graphics.beginFill(setRGB, 1.0);
                shapeV.graphics.drawCircle(0,0,80);
                shapeV.graphics.endFill();
                
            });
            
        }
        
        public function HSVtoRGB(h:Number, s:Number, v:Number):Object{
            var rgbObject:Object = new Object();
            //0.0~360.0に丸める
            h = (h % 360.0 + 360.0) % 360.0;
            //0.0~1.0に丸める
            s = Math.min( Math.max(s, 0.0) , 1.0);
            v = Math.min( Math.max(v, 0.0) , 1.0);
            
            //sが0.0の時はすべての値がvに等しい
            if(s == 0.0)
            {
                rgbObject.r = v;
                rgbObject.g = v;
                rgbObject.b = v;
                return rgbObject;
            }
            else
            {
                var hi:uint = (h / 60) % 6;
                var f:Number = h / 60 - hi;
                var p:Number = v * (1.0 - s);
                var q:Number = v * (1.0 - f * s);
                var t:Number = v * (1.0 - (1.0 - f) * s);
                switch(hi)
                {
                    case 0:
                    {
                        rgbObject.r = v * 255;
                        rgbObject.g = t * 255;
                        rgbObject.b = p * 255;
                        break;
                    }
                    case 1:
                    {
                        rgbObject.r = q * 255;
                        rgbObject.g = v * 255;
                        rgbObject.b = p * 255;
                        break;
                    }
                    case 2:
                    {
                        rgbObject.r = p * 255;
                        rgbObject.g = v * 255;
                        rgbObject.b = t * 255;
                        break;
                    }
                    case 3:
                    {
                        rgbObject.r = p * 255;
                        rgbObject.g = q * 255;
                        rgbObject.b = v * 255;
                        break;
                    }
                    case 4:
                    {
                        rgbObject.r = t * 255;
                        rgbObject.g = p * 255;
                        rgbObject.b = v * 255;
                        break;
                    }
                    case 5:
                    {
                        rgbObject.r = v * 255;
                        rgbObject.g = p * 255;
                        rgbObject.b = q * 255;
                        break;
                    }
                    default:
                    {
                        rgbObject.r = 0;
                        rgbObject.g = 0;
                        rgbObject.b = 0;
                        break;
                    }
                }
                return rgbObject;
            }
            
        }
    }
}

Forked