是否有用于 HSV 到 RGB 的内置 C#/.NET 系统 API? [英] Is there a built-in C#/.NET System API for HSV to RGB?

查看:36
本文介绍了是否有用于 HSV 到 RGB 的内置 C#/.NET 系统 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这是实现代码,

void HsvToRgb(double h, double S, double V, out int r, out int g, out int b){双 H = h;而 (H <0) { H += 360;};而 (H >= 360) { H -= 360;};双R、G、B;如果 (V <= 0){ R = G = B = 0;}否则如果 (S <= 0){R = G = B = V;}别的{双高频 = H/60.0;int i = (int)Math.Floor(hf);双 f = hf - i;双 pv = V * (1 - S);双 qv = V * (1 - S * f);双电视 = V * (1 - S * (1 - f));开关 (i){//红色是主色案例0:R = V;G = 电视;B = 光伏;休息;//绿色是主色情况1:R = qv;G = V;B = 光伏;休息;案例2:R = 光伏;G = V;B = 电视;休息;//蓝色是主色案例3:R = 光伏;G = qv;B = V;休息;案例4:R = 电视;G = 光伏;B = V;休息;//红色是主色案例5:R = V;G = 光伏;B = qv;休息;//以防万一我们的数学计算有点过头,我们把这些放在这里.因为它是一个开关,所以把这些放在这里根本不会减慢我们的速度.案例6:R = V;G = 电视;B = 光伏;休息;情况1:R = V;G = 光伏;B = qv;休息;//颜色没有定义,我们应该抛出一个错误.默认://LFATAL("i 像素转换中的值错误,值为 %d", i);R = G = B = V;//假装它是黑/白休息;}}r = Clamp((int)(R * 255.0));g = Clamp((int)(G * 255.0));b = Clamp((int)(B * 255.0));}///<总结>///将值限制为 0-255///</总结>int Clamp(int i){如果 (i <0) 返回 0;如果 (i > 255) 返回 255;返回我;}

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.

解决方案

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

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

这篇关于是否有用于 HSV 到 RGB 的内置 C#/.NET 系统 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆