HSV/RGB 色彩空间转换
我在这个论坛中找到了这段代码,但我对该代码有疑问。
在此代码片段中“int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;”为什么完整的答案是模6? (%6)
为什么“value = value * 255” value 乘以 255?
我参考了这篇研究论文(p-15、p-16),讨论了相同的算法,但我发现了这些差异。
http://www.poynton.com/PDFs/coloureq.pdf< /p>
public static Color ColorFromHSV(双色相、双饱和度、双值) { int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; double f = 色调 / 60 - Math.Floor(色调 / 60); 值=值*255; int v = Convert.ToInt32(值); int p = Convert.ToInt32(值 * (1 - 饱和度)); int q = Convert.ToInt32(value * (1 - f * 饱和度)); int t = Convert.ToInt32(值 * (1 - (1 - f) * 饱和度)); 如果(嗨==0) 返回 Color.FromArgb(255, v, t, p); 否则如果 (hi == 1) 返回 Color.FromArgb(255, q, v, p); 否则如果 (hi == 2) 返回 Color.FromArgb(255, p, v, t); 否则如果 (hi == 3) 返回 Color.FromArgb(255, p, q, v); 否则如果 (hi == 4) 返回 Color.FromArgb(255, t, p, v); 别的 返回 Color.FromArgb(255, v, p, q); } public void ConvertToHSV(颜色颜色,输出双色相,输出双饱和度,输出双值) { int max = Math.Max(颜色.R, Math.Max(颜色.G, 颜色.B)); int min = Math.Min(color.R, Math.Min(color.G, color.B)); 色调 = color.GetHue(); 饱和度 = (最大值 == 0) ? 0 : 1d - (1d * 最小/最大); 值=最大值/255d; }
I found this code in this forum but Im having doubts with the code.
in this code snippet "int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;" why the complete answer is modulus by 6? (%6)
why is " value = value * 255 " value is multiplied by 255?
im reffering to this research paper (p-15 , p-16) and same algorithmic is discussed but I found these differences.
http://www.poynton.com/PDFs/coloureq.pdf
public static Color ColorFromHSV(double hue, double saturation, double value) { int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; double f = hue / 60 - Math.Floor(hue / 60); value = value * 255; int v = Convert.ToInt32(value); int p = Convert.ToInt32(value * (1 - saturation)); int q = Convert.ToInt32(value * (1 - f * saturation)); int t = Convert.ToInt32(value * (1 - (1 - f) * saturation)); if (hi == 0) return Color.FromArgb(255, v, t, p); else if (hi == 1) return Color.FromArgb(255, q, v, p); else if (hi == 2) return Color.FromArgb(255, p, v, t); else if (hi == 3) return Color.FromArgb(255, p, q, v); else if (hi == 4) return Color.FromArgb(255, t, p, v); else return Color.FromArgb(255, v, p, q); } public void convertToHSV(Color color, out double hue, out double saturation, out double value) { int max = Math.Max(color.R, Math.Max(color.G, color.B)); int min = Math.Min(color.R, Math.Min(color.G, color.B)); hue = color.GetHue(); saturation = (max == 0) ? 0 : 1d - (1d * min / max); value = max / 255d; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
如果其他代码段中涉及到不能确保除以 360 模的颜色转换,则色调可能会表示为大于 360 或小于 0。
如果您 100% 确定所有其他函数将返回 [0,360] 范围内的 Hue,则不需要模 6。
在 HSV 中,值通常标准化为 [0,1] 连续区间,而在 RGB 中则标准化为离散 [0,255] 区间。因此两者:
<前><代码>值 = 值 * 255;
和
Regarding
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
Hue might be represented as larger than 360 or smaller than 0, if there are color transformations involved in other pieces of code that do not make sure to divide mod 360.
If you are 100% sure that all other functions are going to return Hue within [0,360] then the modulo 6 is not needed.
In HSV, Value is typically normalized to the [0,1] continuous interval, whereas in RGB in the discrete [0,255] interval. Hence both:
and