织梦CMS - 轻松建站从此开始!

罗索

RGB和HSL之间的转换 C++实现 C#实现

jackyhwei 发布于 2011-07-06 22:48 点击:次 
在Windows系统下,HSL分量的范围是[0,240].参考“画笔”程序,可以看到RGB(红|绿|蓝).和HSL(色调|饱和度|亮度)的联系。下面的代码,把COLOR_HSL的分量值变为Windows的HSL分量,取值在[0,240]之间,下面为COLOR_HSL到Windows的HSL的转换.
TAG:

#define min3v(v1, v2, v3)   ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v2)))
#define max3v(v1, v2, v3)   ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))

typedef struct
{
 int  red;              // [0,255]
 int  green;            // [0,255]
 int  blue;             // [0,255]
}COLOR_RGB;

typedef struct
{
 float hue;              // [0,360]
 float saturation;       // [0,100]
 float luminance;        // [0,100]
}COLOR_HSL;

// Converts RGB to HSL
static void RGBtoHSL(const COLOR_RGB *rgb, COLOR_HSL *hsl)
{
 float h=0, s=0, l=0;
 // normalizes red-green-blue values
 float r = rgb->red/255.0f;
 float g = rgb->green/255.0f;
 float b = rgb->blue/255.0f;
 float maxVal = max3v(r, g, b);
 float minVal = min3v(r, g, b);

 // hue
 if(maxVal == minVal)
 {
  h = 0; // undefined
 }
 else if(maxVal==r && g>=b)
 {
  h = 60.0f*(g-b)/(maxVal-minVal);
 }
 else if(maxVal==r && g<b)
 {
  h = 60.0f*(g-b)/(maxVal-minVal) + 360.0f;
 }
 else if(maxVal==g)
 {
  h = 60.0f*(b-r)/(maxVal-minVal) + 120.0f;
 }
 else if(maxVal==b)
 {
  h = 60.0f*(r-g)/(maxVal-minVal) + 240.0f;
 }

 // luminance
 l = (maxVal+minVal)/2.0f;

 // saturation
 if(l == 0 || maxVal == minVal)
 {
  s = 0;
 }
 else if(0<l && l<=0.5f)
 {
  s = (maxVal-minVal)/(maxVal+minVal);
 }
 else if(l>0.5f)
 {
  s = (maxVal-minVal)/(2 - (maxVal+minVal)); //(maxVal-minVal > 0)?
 }

 hsl->hue = (h>360)? 360 : ((h<0)?0:h);
 hsl->saturation = ((s>1)? 1 : ((s<0)?0:s))*100;
 hsl->luminance = ((l>1)? 1 : ((l<0)?0:l))*100;
}

// Converts HSL to RGB
static void HSLtoRGB(const COLOR_HSL *hsl, COLOR_RGB *rgb)
{
 float h = hsl->hue;                  // h must be [0, 360]
 float s = hsl->saturation/100.f; // s must be [0, 1]
 float l = hsl->luminance/100.f;      // l must be [0, 1]
 float R, G, B;
 if(hsl->saturation == 0)
 {
  // achromatic color (gray scale)
  R = G = B = l*255.0f;
 }
 else
 {
  float q = (l<0.5f)?(l * (1.0f+s)):(l+s - (l*s));
  float p = (2.0f * l) - q;
  float Hk = h/360.0f;
  float T[3];
  T[0] = Hk + 0.3333333f; // Tr   0.3333333f=1.0/3.0
  T[1] = Hk;              // Tb
  T[2] = Hk - 0.3333333f; // Tg
  for(int i=0; i<3; i++)
  {
   if(T[i] < 0) T[i] += 1.0f;
   if(T[i] > 1) T[i] -= 1.0f;
   if((T[i]*6) < 1)
   {
    T[i] = p + ((q-p)*6.0f*T[i]);
   }
   else if((T[i]*2.0f) < 1) //(1.0/6.0)<=T[i] && T[i]<0.5
   {
    T[i] = q;
   }
   else if((T[i]*3.0f) < 2) // 0.5<=T[i] && T[i]<(2.0/3.0)
   {
    T[i] = p + (q-p) * ((2.0f/3.0f) - T[i]) * 6.0f;
   }
   else T[i] = p;
  }
  R = T[0]*255.0f;
  G = T[1]*255.0f;
  B = T[2]*255.0f;
 }

 rgb->red = (int)((R>255)? 255 : ((R<0)?0 : R));
 rgb->green = (int)((G>255)? 255 : ((G<0)?0 : G));
 rgb->blue = (int)((B>255)? 255 : ((B<0)?0 : B));
}

// 在Windows系统下,HSL分量的范围是[0,240].参考“画笔”程序,可以看到RGB(红|绿|蓝)
// 和HSL(色调|饱和度|亮度)的联系。
// 下面的代码,把COLOR_HSL的分量值变为Windows的HSL分量,取值在[0,240]之间,需要:
// 下面为COLOR_HSL到Windows的HSL的转换:
// win_H = 240 * hsl.hue / 360.f;
// win_S = 240 * hsl.saturation / 100.f;
// win_L = 240 * hsl.luminance / 100.f;

上面内容转载自http://c.chinaitlab.com/cc/example/200903/780279.html,原文里有些错误,已修正~

下面是C#实现

      public struct ColorRGB
      {
            public byte R;
            public byte G;
            public byte B;
            public ColorRGB(Color value)
            {
                  this.R = value.R;
                  this.G = value.G;
                  this.B = value.B;
            }
            public static implicit operator Color(ColorRGB rgb)
            {
                  Color c = Color.FromArgb(rgb.R,rgb.G,rgb.B);
                  return c;
            }
            public static explicit operator ColorRGB(Color c)
            {
                  return new ColorRGB(c);
            }
      }
 
      // Given H,S,L in range of 0-1
      // Returns a Color (RGB struct) in range of 0-255
      public static ColorRGB HSL2RGB(double h, double sl, double l)
      {
            double v;
            double r,g,b;
 
            r = l;   // default to gray
            g = l;
            b = l;
            v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
            if (v > 0)
            {
                  double m;
                  double sv;
                  int sextant;
                  double fract, vsf, mid1, mid2;
 
                  m = l + l - v;
                  sv = (v - m ) / v;
                  h *= 6.0;
                  sextant = (int)h;
                  fract = h - sextant;
                  vsf = v * sv * fract;
                  mid1 = m + vsf;
                  mid2 = v - vsf;
                  switch (sextant)
                  {
                        case 0:
                              r = v;
                              g = mid1;
                              b = m;
                              break;
                        case 1:
                              r = mid2;
                              g = v;
                              b = m;
                              break;
                        case 2:
                              r = m;
                              g = v;
                              b = mid1;
                              break;
                        case 3:
                              r = m;
                              g = mid2;
                              b = v;
                              break;
                        case 4:
                              r = mid1;
                              g = m;
                              b = v;
                              break;
                        case 5:
                              r = v;
                              g = m;
                              b = mid2;
                              break;
                  }
            }
            ColorRGB rgb;
            rgb.R = Convert.ToByte(r * 255.0f);
            rgb.G = Convert.ToByte(g * 255.0f);
            rgb.B = Convert.ToByte(b * 255.0f);
            return rgb;
      }
 
      // Given a Color (RGB Struct) in range of 0-255
      // Return H,S,L in range of 0-1
      public static void      RGB2HSL (ColorRGB rgb, out double h, out double s, out double l)
      {
            double r = rgb.R/255.0;
            double g = rgb.G/255.0;
            double b = rgb.B/255.0;
            double v;
            double m;
            double vm;
            double r2, g2, b2;
 
            h = 0; // default to black
            s = 0;
            l = 0;
            v = Math.Max(r,g);
            v = Math.Max(v,b);
            m = Math.Min(r,g);
            m = Math.Min(m,b);
            l = (m + v) / 2.0;
            if (l <= 0.0)
            {
                  return;
            }
            vm = v - m;
            s = vm;
            if (s > 0.0)
            {
                  s /= (l <= 0.5) ? (v + m ) : (2.0 - v - m) ;
            }
            else
            {
                  return;
            }
            r2 = (v - r) / vm;
            g2 = (v - g) / vm;
            b2 = (v - b) / vm;
            if (r == v)
            {
                  h = (g == m ? 5.0 + b2 : 1.0 - g2);
            }
            else if (g == v)
            {
                  h = (b == m ? 1.0 + r2 : 3.0 - b2);
            }
            else
            {
                  h = (r == m ? 3.0 + g2 : 5.0 - r2);
            }
            h /= 6.0;
      }

(ily961)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201107/14674.html]
本文出处:CSDN博客 作者:ily961
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容