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

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

问题描述

有没有内置在.NET框架将HSV的API为RGB ?我没有看到的System.Drawing.Color这种方法,但似乎奇怪,就不会有一个在平台上。

解决方案

我不认为有一个方法,在.NET框架内这样做。
请查看使用C#

这是实施code,

 无效HsvToRgb(双小时,双S,双V,出INT R,OUT INT克,出INT B)
{
  双H =​​ H;
  而(H&小于0){H + = 360; };
  而(H购自Fluka; = 360)〔H  -  = 360; };
  双R,G,B;
  如果(V族; = 0)
    {R = G = B = 0; }
  否则,如果(S< = 0)
  {
    R = G = B = V;
  }
  其他
  {
    双HF = H / 60.0;
    INT I =(int)的Math.Floor(HF);
    双F = HF  - 我;
    双PV = V *(1  -  S);
    双QV = V *(1  -  S * F);
    双电视= V *(1  -  S *(1  -  F));
    开关(ⅰ)
    {

      //红色是主色

      情况下0:
        R = V;
        G =电视;
        B = PV;
        打破;

      //绿色是主色调

      情况1:
        R = QV;
        G =伏;
        B = PV;
        打破;
      案例2:
        R = PV;
        G =伏;
        B =电视;
        打破;

      //蓝色是主色

      案例3:
        R = PV;
        G = QV;
        B = V:
        打破;
      壳体4:
        R =电视;
        G = PV;
        B = V:
        打破;

      //红色是主色

      壳体5:
        R = V;
        G = PV;
        B = QV;
        打破;

      //以防万一,我们过冲我们的数学受了一点,我们把这些在这里。由于它的开关也不会减缓我们的速度都将这些在这里。

      情况6:
        R = V;
        G =电视;
        B = PV;
        打破;
      情况1:
        R = V;
        G = PV;
        B = QV;
        打破;

      //颜色没有定义,我们应该抛出一个错误。

      默认:
        // LFATAL(I值误差象素转换,价值为%d,I);
        R = G = B = V; //只需pretend的黑/白
        打破;
    }
  }
  R =钳((INT)(R * 255.0));
  G =钳((INT)(G * 255.0));
  B =卡箍((INT)(B * 255.0));
}

///<总结>
///夹住值0-255
///< /总结>
INT夹(int i)以
{
  如果(ⅰ℃,)返回0;
  如果(ⅰ> 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;
}

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

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