如何将RGB颜色更改为HSV颜色?在C#语言中。我寻找非常快速的方法,无需任何外部库。

有帮助吗?

解决方案

您是否考虑过简单地使用 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 转换回来,但后者是合理的 易于添加.

其他提示

请注意Color.GetSaturation()Color.GetBrightness()回报HSL值,而不是单纯疱疹病毒。结果 下面的代码演示的差。

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

下面的C#代码是你想要的。其转换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);
}

有一个C语言实现的位置:

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

应该是非常简单转换为C#,因为几乎不用调用函数 - 只需计算

谷歌结果

EasyRGB 有许多颜色空间转换。 下面是RGB-> HSV转换的代码

这是VB.net版本,在布拉姆的岗位工作正常,我移植从C代码。

  

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

首先:确保你有一个颜色为位图,像这样的:

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

(e为从事件处理程序至极拾取我的颜色!)

我做什么,当我一个whilke以前有这个问题,我第一次拿到了RGBA(红,绿,蓝和alpha)值。 接下来,我在3个浮筒:浮子色调,饱和度浮动,浮动亮度。然后你只需做:

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值:)

希望这有助于

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top