我正在为我的游戏框架编写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
而不是{{1 }}
作为参考,这里是Flixel Power Tools的HSVtoRGB方法,基本上是我想要复制的方法。
System.Drawing.Color
答案 0 :(得分:2)
在方法开始时,Hue被限制为0.0f ... 1.0f 然后它被60f除以现在它在0.0f ... 1 / 60.0f
的范围内然后你打开(int)色调,它总是为零。
我不认为你的交换机的其他分支会执行。
答案 1 :(得分:2)
我根据你的代码写了我自己的HSV-> RGB转换器......
(以及指向http://www.easyrgb.com/index.php?X=MATH&H=21#text21的链接)
代码是:
public static Color HSVtoRGB(float hue, float saturation, float value, float alpha)
{
while (hue > 1f) { hue -= 1f; }
while (hue < 0f) { hue += 1f; }
while (saturation > 1f) { saturation -= 1f; }
while (saturation < 0f) { saturation += 1f; }
while (value > 1f) { value -= 1f; }
while (value < 0f) { value += 1f; }
if (hue > 0.999f) { hue = 0.999f; }
if (hue < 0.001f) { hue = 0.001f; }
if (saturation > 0.999f) { saturation = 0.999f; }
if (saturation < 0.001f) { return new Color(value * 255f, value * 255f, value * 255f); }
if (value > 0.999f) { value = 0.999f; }
if (value < 0.001f) { value = 0.001f; }
float h6 = hue * 6f;
if (h6 == 6f) { h6 = 0f; }
int ihue = (int)(h6);
float p = value * (1f - saturation);
float q = value * (1f - (saturation * (h6 - (float)ihue)));
float t = value * (1f - (saturation * (1f - (h6 - (float)ihue))));
switch (ihue)
{
case 0:
return new Color(value, t, p, alpha);
case 1:
return new Color(q, value, p, alpha);
case 2:
return new Color(p, value, t, alpha);
case 3:
return new Color(p, q, value, alpha);
case 4:
return new Color(t, p, value, alpha);
default:
return new Color(value, p, q, alpha);
}
}
有了它,我渲染了这个:
值得注意的一些事情: