欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现HSL颜色值转换为RGB的方法

程序员文章站 2022-10-15 16:02:36
本文实例讲述了c#实现hsl颜色值转换为rgb的方法。分享给大家供大家参考。具体实现方法如下: //this method converts the values...

本文实例讲述了c#实现hsl颜色值转换为rgb的方法。分享给大家供大家参考。具体实现方法如下:

//this method converts the values to rgb
public rgbcolor hsltorgb(int hue, int saturation, int lightness)
{
 double num4 = 0.0;
 double num5 = 0.0;
 double num6 = 0.0;
 double num = ((double)hue) % 360.0;
 double num2 = ((double)saturation) / 100.0;
 double num3 = ((double)lightness) / 100.0;
 if (num2 == 0.0)
 {
  num4 = num3;
  num5 = num3;
  num6 = num3;
 }
 else
 {
  double d = num / 60.0;
  int num11 = (int)math.floor(d);
  double num10 = d - num11;
  double num7 = num3 * (1.0 - num2);
  double num8 = num3 * (1.0 - (num2 * num10));
  double num9 = num3 * (1.0 - (num2 * (1.0 - num10)));
  switch (num11)
  {
   case 0:
    num4 = num3;
    num5 = num9;
    num6 = num7;
    break;
   case 1:
    num4 = num8;
    num5 = num3;
    num6 = num7;
    break;
   case 2:
    num4 = num7;
    num5 = num3;
    num6 = num9;
    break;
   case 3:
    num4 = num7;
    num5 = num8;
    num6 = num3;
    break;
   case 4:
    num4 = num9;
    num5 = num7;
    num6 = num3;
    break;
   case 5:
    num4 = num3;
    num5 = num7;
    num6 = num8;
    break;
  }
 }
 return new rgbcolor((int)(num4 * 255.0), (int)(num5 * 255.0), (int)(num6 * 255.0));
}
//the structure that will hold the rgb values
public struct rgbcolor
{
 public rgbcolor(int r, int g, int b)
 {
  red = r;
  green = g;
  blue = b;
 }
 public int red;
 public int green;
 public int blue;
}

希望本文所述对大家的c#程序设计有所帮助。