星空网 > 软件开发 > ASP.net

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

【C#】RGB,CMYK,HSB各种颜色表示的转换

 

一、表示颜色的方式有很多种,如RGB,CMYK,HSB,Hex等等

  1、RGB:这种表示颜色由三原色构成,通过红,绿,蓝三种颜色分量的不同,组合成不同的颜色,例如,100%红+100%绿混合可以得到黄色,红绿蓝三种颜色叠加可以得到白色,基本上屏幕显示色彩都采用这种方式

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

  2、CMYK:也称作印刷色彩模式,是一种依靠反光的色彩模式,主要用于印刷,和RGB类似,CMY是3种印刷油墨名称的首字母:青色Cyan、品红色Magenta、黄色Yellow。而K取的是black最后一个字母,之所以不取首字母,是为了避免与蓝色(Blue)混淆。从理论上来说,只需要CMY三种油墨就足够了,它们三个加在一起就应该得到黑色。但是由于目前制造工艺还不能造出高纯度的油墨,CMY相加的结果实际是一种暗红色。

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

 

  3、HSB:通过色相(hues),饱和度(saturation),亮度(brightness)来表示颜色

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

 

 

二、下面说说关于各种颜色之间的转换

  1、RGB与CMYK之间的转换

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)
    public static void RGB2CMYK(int red, int green, int blue, out double c, out double m, out double y, out double k)    {      c = (double)(255 - red) / 255;      m = (double)(255 - green) / 255;      y = (double)(255 - blue) / 255;      k = (double)Math.Min(c, Math.Min(m, y));      if (k == 1.0)      {        c = m = y = 0;      }      else      {        c = (c - k) / (1 - k);        m = (m - k) / (1 - k);        y = (y - k) / (1 - k);      }    }    public static void CMYK2RGB(double c, double m, double y, double k, out int r, out int g, out int b)    {      r = Convert.ToInt32((1.0 - c) * (1.0 - k) * 255.0);      g = Convert.ToInt32((1.0 - m) * (1.0 - k) * 255.0);      b = Convert.ToInt32((1.0 - y) * (1.0 - k) * 255.0);    }
【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

  2、RGB与HSB之间的转换

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)
    public static void RGB2HSB(int red, int green, int blue, out double hue, out double sat, out double bri)    {      double r = ((double)red / 255.0);      double g = ((double)green / 255.0);      double b = ((double)blue / 255.0);      double max = Math.Max(r, Math.Max(g, b));      double min = Math.Min(r, Math.Min(g, b));      hue = 0.0;      if (max == r && g >= b)      {        if (max - min == 0) hue = 0.0;        else hue = 60 * (g - b) / (max - min);      }      else if (max == r && g < b)      {        hue = 60 * (g - b) / (max - min) + 360;      }      else if (max == g)      {        hue = 60 * (b - r) / (max - min) + 120;      }      else if (max == b)      {        hue = 60 * (r - g) / (max - min) + 240;      }      sat = (max == 0) ? 0.0 : (1.0 - ((double)min / (double)max));      bri = max;    }    public static void HSB2RGB(double hue, double sat, double bri, out int red, out int green ,out int blue)    {      double r = 0;      double g = 0;      double b = 0;      if (sat == 0)      {        r = g = b = bri;      }      else      {        // the color wheel consists of 6 sectors. Figure out which sector you're in.        double sectorPos = hue / 60.0;        int sectorNumber = (int)(Math.Floor(sectorPos));        // get the fractional part of the sector        double fractionalSector = sectorPos - sectorNumber;        // calculate values for the three axes of the color.         double p = bri * (1.0 - sat);        double q = bri * (1.0 - (sat * fractionalSector));        double t = bri * (1.0 - (sat * (1 - fractionalSector)));        // assign the fractional colors to r, g, and b based on the sector the angle is in.        switch (sectorNumber)        {          case 0:            r = bri;            g = t;            b = p;            break;          case 1:            r = q;            g = bri;            b = p;            break;          case 2:            r = p;            g = bri;            b = t;            break;          case 3:            r = p;            g = q;            b = bri;            break;          case 4:            r = t;            g = p;            b = bri;            break;          case 5:            r = bri;            g = p;            b = q;            break;        }      }      red = Convert.ToInt32(r * 255);      green = Convert.ToInt32(g * 255);      blue = Convert.ToInt32(b * 255); ;    }
【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

  3、RGB与十六进制Hex表示的转换

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)
    public static string RGB2Hex(int r, int g, int b)    {      return String.Format("#{0:x2}{1:x2}{2:x2}", (int)r, (int)g, (int)b);    }    public static Color Hex2Color(string hexColor)    {      string r, g, b;      if (hexColor != String.Empty)      {        hexColor = hexColor.Trim();        if (hexColor[0] == '#') hexColor = hexColor.Substring(1, hexColor.Length - 1);        r = hexColor.Substring(0, 2);        g = hexColor.Substring(2, 2);        b = hexColor.Substring(4, 2);        r = Convert.ToString(16 * GetIntFromHex(r.Substring(0, 1)) + GetIntFromHex(r.Substring(1, 1)));        g = Convert.ToString(16 * GetIntFromHex(g.Substring(0, 1)) + GetIntFromHex(g.Substring(1, 1)));        b = Convert.ToString(16 * GetIntFromHex(b.Substring(0, 1)) + GetIntFromHex(b.Substring(1, 1)));        return Color.FromArgb(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b));      }      return Color.Empty;    }    private static int GetIntFromHex(string strHex)    {      switch (strHex)      {        case ("A"):          {            return 10;          }        case ("B"):          {            return 11;          }        case ("C"):          {            return 12;          }        case ("D"):          {            return 13;          }        case ("E"):          {            return 14;          }        case ("F"):          {            return 15;          }        default:          {            return int.Parse(strHex);          }      }    }
【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

 

转换算法摘自:http://www.codeproject.com/Articles/19045/Manipulating-colors-in.NET-Part-1






原标题:【C#】RGB,CMYK,HSB各种颜色表示的转换(转)

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

香港公司委托内地律师需要公证吗? :https://www.kjdsnews.com/a/1837393.html
香港公司委托内地律师需要公证吗? :https://www.xlkjsw.com/news/60977.html
美国公司开户攻略:全方位了解注册需注意事项 :https://www.kjdsnews.com/a/1837394.html
美国公司开户攻略:全方位了解注册需注意事项 :https://www.xlkjsw.com/news/60978.html
郑州办理汇丰银行开户须知 :https://www.kjdsnews.com/a/1837395.html
郑州办理汇丰银行开户须知 :https://www.xlkjsw.com/news/60979.html
2024.04.18亚马逊选品推荐(仅供参考):女装蛋糕连衣裙:https://www.kjdsnews.com/a/1842234.html
欧洲市场疯了,订单排到7、8月!:https://www.kjdsnews.com/a/1842235.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流