我正在为我的游戏框架编写一个 HSVtoRGB 方法,当通过色调时,会发生这种情况 -> http://youtu.be/ACBwR_0iMWE。
这是代码。
public static Color HSVtoRGB(float hue, float saturation, float value, float alpha)
{
if(hue > 1 || saturation > 1 || value > 1) throw new Exception("values cannot be more than 1!");
if (hue < 0 || saturation < 0|| value < 0) throw new Exception("values cannot be less than 0!");
Color output = new Color();
if (Math.Abs(saturation) < 0.001)
{
output.R = (byte) (value*byte.MaxValue);
output.G = (byte) (value*byte.MaxValue);
output.B = (byte) (value*byte.MaxValue);
}
else
{
hue = hue/60f;
float f = hue - (int)hue;
float p = value*(1f - saturation);
float q = value*(1f - saturation*f);
float t = value*(1f - saturation*(1f - f));
switch ((int)hue)
{
case (0) :
output = new Color(value * 255, t * 255, p * 255, alpha);
break;
case (1):
output = new Color(q * 255, value * 255, p * 255, alpha);
break;
case (2):
output = new Color(p * 255, value * 255, t * 255, alpha);
break;
case (3):
output = new Color(p * 255, q * 255, value * 255, alpha);
break;
case (4):
output = new Color(t * 255, p * 255, value * 255, alpha);
break;
case (5):
output = new Color(value * 255, p * 255, q * 255, alpha);
break;
default :
throw new Exception("RGB color unknown!");
}
}
return output;
}
添加.001f
到色调时,它会导致它从红色变为黄色,但随后会一直保持黄色,直到它回滚到 0。请注意我使用的是Microsoft.Xna.Framework.Color
not System.Drawing.Color
。
作为参考,这里是Flixel Power Tools的 HSVtoRGB 方法,基本上是我想要复制的。
public static function HSVtoRGB(h:Number, s:Number, v:Number, alpha:uint = 255):uint
{
var result:uint;
if (s == 0.0)
{
result = getColor32(alpha, v * 255, v * 255, v * 255);
}
else
{
h = h / 60.0;
var f:Number = h - int(h);
var p:Number = v * (1.0 - s);
var q:Number = v * (1.0 - s * f);
var t:Number = v * (1.0 - s * (1.0 - f));
switch (int(h))
{
case 0:
result = getColor32(alpha, v * 255, t * 255, p * 255);
break;
case 1:
result = getColor32(alpha, q * 255, v * 255, p * 255);
break;
case 2:
result = getColor32(alpha, p * 255, v * 255, t * 255);
break;
case 3:
result = getColor32(alpha, p * 255, q * 255, v * 255);
break;
case 4:
result = getColor32(alpha, t * 255, p * 255, v * 255);
break;
case 5:
result = getColor32(alpha, v * 255, p * 255, q * 255);
break;
default:
FlxG.log("FlxColor Error: HSVtoRGB : Unknown color");
}
}
return result;
}