是否有用于 HSV 到 RGB 的内置 C#/.NET 系统 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1335426/
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-06 15:17:30  来源:igfitidea点击:

Is there a built-in C#/.NET System API for HSV to RGB?

c#.netcolors

提问by jsight

Is there an API built into the .NET framework for converting HSV to RGB? I didn't see a method in System.Drawing.Color for this, but it seems surprising that there wouldn't be one in the platform.

.NET 框架中是否有用于将 HSV 转换为 RGB的 API ?我在 System.Drawing.Color 中没有看到用于此的方法,但平台中没有方法似乎令人惊讶。

采纳答案by Patrik Svensson

I don't think there's a method doing this in the .NET framework.
Check out Converting HSV to RGB colour using C#

我认为 .NET 框架中没有这样做的方法。
查看使用 C# 将 HSV 转换为 RGB 颜色

This is the implementation code,

这是实现代码,

void HsvToRgb(double h, double S, double V, out int r, out int g, out int b)
{    
  double H = h;
  while (H < 0) { H += 360; };
  while (H >= 360) { H -= 360; };
  double R, G, B;
  if (V <= 0)
    { R = G = B = 0; }
  else if (S <= 0)
  {
    R = G = B = V;
  }
  else
  {
    double hf = H / 60.0;
    int i = (int)Math.Floor(hf);
    double f = hf - i;
    double pv = V * (1 - S);
    double qv = V * (1 - S * f);
    double tv = V * (1 - S * (1 - f));
    switch (i)
    {

      // Red is the dominant color

      case 0:
        R = V;
        G = tv;
        B = pv;
        break;

      // Green is the dominant color

      case 1:
        R = qv;
        G = V;
        B = pv;
        break;
      case 2:
        R = pv;
        G = V;
        B = tv;
        break;

      // Blue is the dominant color

      case 3:
        R = pv;
        G = qv;
        B = V;
        break;
      case 4:
        R = tv;
        G = pv;
        B = V;
        break;

      // Red is the dominant color

      case 5:
        R = V;
        G = pv;
        B = qv;
        break;

      // Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here.

      case 6:
        R = V;
        G = tv;
        B = pv;
        break;
      case -1:
        R = V;
        G = pv;
        B = qv;
        break;

      // The color is not defined, we should throw an error.

      default:
        //LFATAL("i Value error in Pixel conversion, Value is %d", i);
        R = G = B = V; // Just pretend its black/white
        break;
    }
  }
  r = Clamp((int)(R * 255.0));
  g = Clamp((int)(G * 255.0));
  b = Clamp((int)(B * 255.0));
}

/// <summary>
/// Clamp a value to 0-255
/// </summary>
int Clamp(int i)
{
  if (i < 0) return 0;
  if (i > 255) return 255;
  return i;
}

回答by Greg

There isn't a built-in method for doing this, but the calculations aren't terribly complex.
Also note that Color's GetHue(), GetSaturation() and GetBrightness() return HSL values, not HSV.

没有这样做的内置方法,但计算并不是非常复杂。
另请注意,Color 的 GetHue()、GetSaturation() 和 GetBrightness() 返回 HSL 值,而不是 HSV。

The following C# code converts between RGB and HSV using the algorithms described on Wikipedia.
I already posted this answer here, but I'll copy the code here for quick reference.

以下 C# 代码使用Wikipedia 上描述的算法在 RGB 和 HSV 之间进行转换。
我已经在这里发布了这个答案,但我会在这里复制代码以供快速参考。

The ranges are 0 - 360 for hue, and 0 - 1 for saturation or value.

色相的范围是 0 - 360,饱和度或值的范围是 0 - 1。

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 Chris Wegener

Look at http://richnewman.wordpress.com/hslcolor-class/which has an excellent c# class to provide all the necessary conversions including to and from windows system colors.

查看http://richnewman.wordpress.com/hslcolor-class/,它有一个出色的 c# 类,可提供所有必要的转换,包括与 Windows 系统颜色之间的转换。

回答by Joe Zack

It's not built in, but there's there's an open-source C# library called ColorMinewhich makes converting between color spaces pretty easy.

它不是内置的,但有一个名为ColorMine的开源 C# 库 ,它使颜色空间之间的转换非常容易。

Rgb to Hsv:

RGB 到 Hsv:

var rgb = new Rgb {R = 123, G = 11, B = 7};
var hsv = rgb.To<Hsv>();

Hsv to Rgb:

Hsv 到 Rgb:

var hsv = new Hsv { H = 360, S = .5, L = .17 }
var rgb = hsv.to<Rgb>();