gpt4 book ai didi

c# - HSV 到 RGB 在黄色 C# 处停止

转载 作者:行者123 更新时间:2023-11-30 20:57:07 25 4
gpt4 key购买 nike

我正在为我的游戏框架编写一个 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 不是 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;
}

最佳答案

我根据您的代码编写了自己的 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);
}
}

有了它,我渲染了这个: Screenshot

一些值得注意的事情:

  • new Color(...) 接受从 0 到 1 的输入,而不是 0 到 255。
  • 确保始终明确区分整数和 float
  • 不要在情况​​不尽如人意时抛出异常。尽可能尝试适应。
  • 当您从其他程序借用了代码,并且还有完美的数学引用时...尝试在借用代码之前复制数学站点 - 谁知道从正确的数学中做出了什么样的奇怪的特定情况修改?

关于c# - HSV 到 RGB 在黄色 C# 处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17080535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com