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

 

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

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

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

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

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

  1、RGB与CMYK之间的转换

        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);
}

  2、RGB与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); ;
}

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

        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);
}
}
}

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

【C#】RGB,CMYK,HSB各种颜色表示的转换(转)的更多相关文章

  1. RGB,CMYK,HSB各种颜色表示的转换 C#语言

    Introduction Why an article on "colors"? It's the same question I asked myself before writ ...

  2. 色彩空间RGB/CMYK/HSL/HSB/HSV/Lab/YUV基础理论及转换方法:RGB与YUV

    之前做个设计,现在从事IT,脑子里面关于RGB,RGBA,CMY,CMYK,YUV,但是具体理论还是不扎实.若干年前之前写过<水煮RGB与CMYK色彩模型—色彩与光学相关物理理论浅叙>&l ...

  3. c# 颜色RGB到HSB互相转换

    /// <summary> /// 色相,饱和度,亮度转换成rgb值 /// </summary> /// <returns></returns> pu ...

  4. 颜色空间模型(HSV\LAB\RGB\CMYK)

    通过Photoshop的拾色器,我们知道表征颜色的模型的不止一种,本文将系统并且详细讨论这四种模型(HSV.LAB.RGB和CMYK)之间的联系以及应用.本文部分章节整合了多位优秀博主的博客(链接见本 ...

  5. C# RGB和HSB相互转换

    背景 最近做的项目中有这样一个场景,设置任意一种颜色,得到这种颜色偏深和偏浅的两种颜色.也就是说取该颜色同色系的深浅两种颜色.首先想到的是调节透明度,但效果不理想.后来尝试调节颜色亮度,发现这才是正解 ...

  6. RGB和HSB的转换推算

    RGB三原色是基于人肉眼对光线的生理作用.人眼内有三种椎状体“对这三种光线频率所能感受的带宽最大,也能独立刺激这三种颜色的受光体”,因此RGB称为三原色.比如,黄色波长的光对人眼的刺激效果,和红色与绿 ...

  7. RGB与HSB之间的转换公式

    先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...

  8. RGB与HSB之间转换

    先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...

  9. 常用icon以及color颜色RGB值和对应颜色效果图

    Android谷歌官方扁平化设计常用icon集合   Android谷歌官方扁平化设计color颜色RGB值和对应颜色效果图.

随机推荐

  1. ABAQUS用户子程序一览表

    说明 ABAQUS用户子程序一览表 ABAQUSStandard subroutines Refence 说明 本系列文章本人基本没有原创贡献,都是在学习过程中找到的相关书籍和教程相关内容的汇总和梳理 ...

  2. hihoCoder-1098-kruskal

    如果起始点和终止点的父节点相同,就说明它们就已经在同一个连通分量里面,说明,起始点和终止点在此之前就已经被连入同一个分量之中,如果此时还将起始点和终止点连入此分量,就会形成回路,想象一个三角形,你大概 ...

  3. 文件操作-dd

    Linux dd命令 用于读取.转换并输出数据. dd可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件.设备或标准输出. 参数说明: if=文件名: 输入文件名,缺省为标准输入.即 ...

  4. Python赋值运算及流程控制

    1. 内置函数 1> len:统计元素长度 str1 = 'wonderful' print(len(str1)) result: li = [,,] print(len(li)) result ...

  5. Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...

  6. mbist summary

    1. 关于mbist,网上也有介绍,觉得不错: 推荐的mbistt的博客:奋斗的猪 2.使用的工具是mbistarchitect,不是tessent. 3.工具使用的相关文档:从EETOP和工具自带的 ...

  7. CVS update常用技巧

    常用的命令有 cvs update 全部更新 cvs update path/to/file 来更新某一个文件 cvs update -dP 意为删除空目录创建新目录 cvs -f -n update ...

  8. Lex与Yacc学习(八)之变量和有类型的标记(扩展计算器)

    变量和有类型的标记 下一步扩展计算器来处理具有单个字母名字的变量,因为只有26个字母 (目前只关心小写字母),所以我们能在26个条目的数组(称它为vbltable)中存储变量. 为了使得计算器更加有用 ...

  9. python基础学习笔记——time模块

    time模块 time翻译过来就是时间,有我们其实在之前编程的时候有用到过. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取 ...

  10. 【Kubernetes】声明式API与Kubernetes编程范式

    什么是声明式API呢? 答案是,kubectl apply命令. 举个栗子 在本地编写一个Deployment的YAML文件: apiVersion: apps/v1 kind: Deployment ...