代码之家  ›  专栏  ›  技术社区  ›  jsight TaherT

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

  •  27
  • jsight TaherT  · 技术社区  · 15 年前

    .NET框架中是否内置了用于 converting HSV to RGB ?我在system.drawing.color中没有看到这样的方法,但是平台中没有这样的方法似乎让人惊讶。

    4 回复  |  直到 15 年前
        1
  •  19
  •   rae1    10 年前

    我认为在.NET框架中没有这样的方法。
    退房 Converting HSV to RGB colour using C#

    这是实现代码,

    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;
    }
    
        2
  •  49
  •   TaW    5 年前

    没有一种内置的方法可以做到这一点,但计算并不十分复杂。
    还要注意,color的gethue()、getsaturation()和getbrightness()返回的是hsl值,而不是hsv。

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

    色调的范围是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);
    }
    
        3
  •  13
  •   aydjay Farhad Jabiyev    8 年前

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

    RGB到Hsv:

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

    HSV到RGB:

    var hsv = new Hsv { H = 360, S = .5, L = .17 }
    var rgb = hsv.to<Rgb>();
    
        4
  •  -1
  •   Chris Wegener    13 年前

    http://richnewman.wordpress.com/hslcolor-class/ 它有一个优秀的C类来提供所有必要的转换,包括与Windows系统颜色的转换。