HSV実験(コード整理中)
Wikipediaを参考にHSVとRGBの実験をしてみる。
* 参考 : http://ja.wikipedia.org/wiki/HSV色空間
* まずは適当に説明どおりに実装。コードが汚いので整理中
/**
* Copyright esukei ( http://wonderfl.net/user/esukei )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ldcM
*/
// forked from esukei's HSV実験
/**
* 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:TestCircle;
private var shapeS:TestCircle;
private var shapeV:TestCircle;
private var shapeA:TestCircle;
public function HSVandRGB() {
//色相が変わる
shapeH = new TestCircle(0,0,80);
//彩度が変わる
shapeS = new TestCircle(0,0,80);
//明度が変わる
shapeV = new TestCircle(0,0,80);
//全部変えてみる
shapeA = new TestCircle(0,0,80);
stage.addChild(shapeH);
stage.addChild(shapeS);
stage.addChild(shapeV);
stage.addChild(shapeA);
shapeH.x = stage.stageWidth / 2 -100;
shapeH.y = stage.stageHeight / 2 - 100;
shapeS.x = stage.stageWidth / 2 - 100;
shapeS.y = stage.stageHeight / 2 + 100;
shapeV.x = stage.stageWidth / 2 + 100;
shapeV.y = stage.stageHeight / 2 - 100;
shapeA.x = stage.stageWidth / 2 + 100;
shapeA.y = stage.stageHeight / 2 + 100;
var tmpH:Number = 0.0;
var tmpS:Number = 1.0;
var tmpV:Number = 1.0;
stage.addEventListener(Event.ENTER_FRAME,function(event:Event):void{
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);
shapeH.rgbColor = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
shapeH.draw();
tmpRGB = HSVtoRGB(tmpH, tmpS, 1.0);
shapeS.rgbColor = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
shapeS.draw();
tmpRGB = HSVtoRGB(tmpH, 1.0, tmpV);
shapeV.rgbColor = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
shapeV.draw();
tmpRGB = HSVtoRGB(tmpH, tmpS, tmpV);
shapeA.rgbColor = tmpRGB.r << 16 | tmpRGB.g << 8 | tmpRGB.b;
shapeA.draw();
});
}
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;
}
}
}
}
//色変える図形
import flash.display.Shape;
class TestCircle extends Shape
{
private var _x:Number;
private var _y:Number;
private var _radius:Number;
private var _rgbColor:uint;
//コンストラクタ
function TestCircle(x:Number = 0, y:Number = 0, radius:Number = 0, rgbColor:uint = 0x000000)
{
_x = x;
_y = y;
_radius = radius;
_rgbColor = rgbColor;
draw();
}
public function set rgbColor(color:uint):void
{
_rgbColor = color;
}
public function draw(){
graphics.clear();
graphics.beginFill(_rgbColor, 1.0);
graphics.drawCircle(_x,_y,_radius);
graphics.endFill();
}
}