C# 如何将RGB颜色更改为HSV?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:28:47  来源:igfitidea点击:

How to change RGB color to HSV?

c#colorsrgbhsv

提问by Tomasz Smykowski

How to change RGB color to HSV? In C# language. I search for very fast method without any external library.

如何将RGB颜色更改为HSV?在 C# 语言中。我在没有任何外部库的情况下搜索非常快速的方法。

采纳答案by georged

Have you considered simply using System.Drawing namespace? For example:

您是否考虑过简单地使用 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();

Note that it's not exactly what you've asked for (see differences between HSL and HSVand the Color class does not have a conversion back from HSL/HSV but the latter is reasonably easy to add.

请注意,这并不是您所要求的(请参阅HSL 和 HSV 之间的差异,并且Color 类没有从 HSL/HSV 转换回来,但后者相当容易添加.

回答by BlaM

There's a C implementation here:

这里有一个 C 实现:

http://www.cs.rit.edu/~ncs/color/t_convert.html

http://www.cs.rit.edu/~ncs/color/t_convert.html

Should be very straightforward to convert to C#, as almost no functions are called - just calculations.

转换为 C# 应该非常简单,因为几乎没有调用任何函数 - 只是计算。

found via Google

通过谷歌找到

回答by Captain Lepton

This is the VB.net version which works fine for me ported from the C code in BlaM's post.

这是从 BlaM 帖子中的 C 代码移植的 VB.net 版本,对我来说效果很好。

There's a C implementation here:

http://www.cs.rit.edu/~ncs/color/t_convert.html

Should be very straightforward to convert to C#, as almost no functions are called - just > calculations.

这里有一个 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

回答by Greg

Note that Color.GetSaturation()and Color.GetBrightness()return HSL values, not HSV.
The following code demonstrates the difference.

请注意,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

The following C# code is what you want. It converts between RGB and HSV using the algorithms described on Wikipedia. The ranges are 0 - 360 for hue, and 0 - 1 for saturationor value.

以下 C# 代码正是您想要的。它使用Wikipedia 上描述的算法在 RGB 和 HSV 之间进行转换。范围是 0-360 表示hue,0-1 表示saturationvalue

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);
}

回答by David Pointer

The EasyRGBhas many color space conversions. Here is the codefor the RGB->HSV conversion.

EasyRGB有很多色彩空间转换。这是RGB->HSV 转换的代码

回答by Matthias K.

FIRST: make sure you have a color as a bitmap, like this:

第一:确保你有一个颜色作为位图,像这样:

Bitmap bmp = (Bitmap)pictureBox1.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);

(e is from the event handler wich picked my color!)

(e 来自选择我颜色的事件处理程序!)

What I did when I had this problem a whilke ago, I first got the rgba (red, green, blue and alpha) values. Next I created 3 floats: float hue, float saturation, float brightness. Then you simply do:

不久前我遇到这个问题时我做了什么,我首先得到了 rgba(红色、绿色、蓝色和 alpha)值。接下来我创建了 3 个浮点数:浮点色相、浮点饱和度、浮点亮度。然后您只需执行以下操作:

hue = yourcolor.Gethue;
saturation = yourcolor.GetSaturation;
brightness = yourcolor.GetBrightness;

The whole lot looks like this:

整体看起来是这样的:

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();

If you now want to display them in a label, just do:

如果您现在想在标签中显示它们,只需执行以下操作:

yourlabelname.Text = hue.ToString;
yourlabelname.Text = saturation.ToString;
yourlabelname.Text = brightness.ToString;

Here you go, you now have RGB Values into HSV values :)

在这里,您现在将 RGB 值转换为 HSV 值:)

Hope this helps

希望这可以帮助