我该如何在C#中将RGB颜色转换为HSV颜色空间?
我搜索了一种不使用任何外部库的快速方法。
我该如何在C#中将RGB颜色转换为HSV颜色空间?
我搜索了一种不使用任何外部库的快速方法。
Color.GetSaturation()
和Color.GetBrightness()
返回的是HSL值,而不是HSV值。Color original = Color.FromArgb(50, 120, 200);
// original = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
double hue;
double saturation;
double value;
ColorToHSV(original, out hue, out saturation, out value);
// hue = 212.0
// saturation = 0.75
// value = 0.78431372549019607
Color copy = ColorFromHSV(hue, saturation, value);
// copy = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
// Compare that to the HSL values that the .NET framework provides:
original.GetHue(); // 212.0
original.GetSaturation(); // 0.6
original.GetBrightness(); // 0.490196079
public static void ColorToHSV(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;
}
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);
}
ToHSV(FromHSV(32,1, 1))
返回 32
,但 ToHSV(FromHSV(33,1, 1))
返回 32.9411773
。ToHSV(FromHSV(34,1, 1))
返回 33.8823547
。ToHSV(FromHSV(35,1, 1))
返回 35.0588264
。ToHSV(FromHSV(36,1, 1))
再次返回 36
。看起来倍数为 4
的工作正常,而其他的则会被多个 1/17
的倍数移位。现在这个神奇的数字从哪里来,我也不知道。 - dotNETdouble
类型,其精度比8位RGB通道所能容纳的要高得多。因此,无法实现100%的往返映射。不过,我想知道这些函数是否可以改进,以便在RGB子集上安全地执行。 - dotNET你考虑过直接使用System.Drawing命名空间吗?例如:
System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
float hue = color.GetHue();
float saturation = color.GetSaturation();
float lightness = color.GetBrightness();
请注意,这并不完全是你所要求的(参见HSL和HSV之间的差异),而且Color类没有从HSL / HSV的转换方式,但后者相当容易添加。
System.Drawing.Color
的实现后,我发现它并不基于NTSC对红、绿、蓝三色的权重,而是将它们视为相同的,导致结果不够优化。 - Itai Bar-HaimSystem.Drawing
实现的 HSB 实际上是个误称:他们实现的是 HSL。.NET API 表明 Color.GetBrightness()
返回亮度,而不是值(即,它不是 HSV 中的“V”,而是 HSL 中的“L”)。 - Informagic这里有一个C语言的实现:
http://www.cs.rit.edu/~ncs/color/t_convert.html
将其转换为C#应该非常简单,因为几乎没有调用函数,只需要进行计算。
通过Google找到的。
这里有一个C实现:
http://www.cs.rit.edu/~ncs/color/t_convert.html
应该很容易转换成C#,因为几乎没有调用函数,只有计算。
Public Sub HSVtoRGB(ByRef r As Double, ByRef g As Double, ByRef b As Double, ByVal h As Double, ByVal s As Double, ByVal v As Double)
Dim i As Integer
Dim f, p, q, t As Double
If (s = 0) Then
' achromatic (grey)
r = v
g = v
b = v
Exit Sub
End If
h /= 60 'sector 0 to 5
i = Math.Floor(h)
f = h - i 'factorial part of h
p = v * (1 - s)
q = v * (1 - s * f)
t = v * (1 - s * (1 - f))
Select Case (i)
Case 0
r = v
g = t
b = p
Exit Select
Case 1
r = q
g = v
b = p
Exit Select
Case 2
r = p
g = v
b = t
Exit Select
Case 3
r = p
g = q
b = v
Exit Select
Case 4
r = t
g = p
b = v
Exit Select
Case Else 'case 5:
r = v
g = p
b = q
Exit Select
End Select
End Sub
我也有同样的需求,所以我来分享目前我找到的最好和最简单的解决方案。
以下是Greg的答案(在这里找到)的修改版;但是代码更加谦逊易懂。
对于那些正在学习的人,我添加了一些值得检查的参考资料,以便更好地理解。
参考资料
代码
/// <summary> Convert RGB Color to HSV. </summary>
/// <param name="color"></param>
/// <returns> A double[] Containing HSV Color Values. </returns>
public double[] rgbToHSV(Color color)
{
double[] output = new double[3];
double hue, saturation, 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;
output[0] = hue;
output[1] = saturation;
output[2] = value;
return output;
}
ColorFromHSV
,它是相反的方向。 - ToolmakerSteve首先,请确保您有一个位图颜色,就像这样:
Bitmap bmp = (Bitmap)pictureBox1.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);
(e是从事件处理程序中选择我的颜色!)
当我遇到这个问题时,我首先获取了rgba(红,绿,蓝和alpha)值。接下来我创建了3个浮点数:float hue,float saturation,float brightness。然后你只需要执行:
hue = yourcolor.Gethue;
saturation = yourcolor.GetSaturation;
brightness = yourcolor.GetBrightness;
Bitmap bmp = (Bitmap)pictureBox1.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);
float hue;
float saturation;
float brightness;
hue = paintcolor.GetHue();
saturation = paintcolor.GetSaturation();
brightness = paintcolor.GetBrightness();
yourlabelname.Text = hue.ToString;
yourlabelname.Text = saturation.ToString;
yourlabelname.Text = brightness.ToString;
好了,现在你已经将RGB值转换为HSV值了 :)
希望这能帮到你