国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

【C#】RGB,CMYK,HSB各種顏色表示的轉(zhuǎn)換(轉(zhuǎn))

2019-11-14 13:47:55
字體:
供稿:網(wǎng)友

【C#】RGB,CMYK,HSB各種顏色表示的轉(zhuǎn)換

 

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

  1、RGB:這種表示顏色由三原色構(gòu)成,通過紅,綠,藍三種顏色分量的不同,組合成不同的顏色,例如,100%紅+100%綠混合可以得到黃色,紅綠藍三種顏色疊加可以得到白色,基本上屏幕顯示色彩都采用這種方式

  2、CMYK:也稱作印刷色彩模式,是一種依靠反光的色彩模式,主要用于印刷,和RGB類似,CMY是3種印刷油墨名稱的首字母:青色Cyan、品紅色Magenta、黃色Yellow。而K取的是black最后一個字母,之所以不取首字母,是為了避免與藍色(Blue)混淆。從理論上來說,只需要CMY三種油墨就足夠了,它們?nèi)齻€加在一起就應(yīng)該得到黑色。但是由于目前制造工藝還不能造出高純度的油墨,CMY相加的結(jié)果實際是一種暗紅色。

 

  3、HSB:通過色相(hues),飽和度(saturation),亮度(brightness)來表示顏色

 

 

二、下面說說關(guān)于各種顏色之間的轉(zhuǎn)換

  1、RGB與CMYK之間的轉(zhuǎn)換

復(fù)制代碼
        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);        }
復(fù)制代碼

  2、RGB與HSB之間的轉(zhuǎn)換

復(fù)制代碼
        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); ;        }
復(fù)制代碼

  3、RGB與十六進制Hex表示的轉(zhuǎn)換

復(fù)制代碼
        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);                    }            }        }
復(fù)制代碼

 

轉(zhuǎn)換算法摘自:http://www.codeproject.com/Articles/19045/Manipulating-colors-in-NET-Part-1


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永宁县| 昂仁县| 南平市| 鸡西市| 天津市| 湘阴县| 宁化县| 驻马店市| 定远县| 宣威市| 阳谷县| 白山市| 石林| 芒康县| 贵南县| 贺兰县| 绩溪县| 教育| 寿光市| 昭苏县| 枣阳市| 怀安县| 那坡县| 靖江市| 武威市| 芦溪县| 泽普县| 称多县| 伊吾县| 昌吉市| 宜州市| 永泰县| 商南县| 繁昌县| 虎林市| 县级市| 宣恩县| 永顺县| 禹州市| 兴国县| 东宁县|