有关hsv与rgb的问题
2009-06-27 14:22
351 查看
这是两种最常用的颜色格式。
hsv 是一色度,亮度,饱和对来表示颜色空间的。RGB就是我们最熟悉的红绿蓝来表示颜色。
HSV的范围大多数是如下定义的:
H: 0-360
S,V: 0-1
但也有的H:0-180
S:0-255
V:0-255
这取决于平台级软件的定义。
RGB一般就是取0-255,也有的去0-1.
下面是两者的转换代码:
/// <summary>
And here's how you'd use it:
hsv 是一色度,亮度,饱和对来表示颜色空间的。RGB就是我们最熟悉的红绿蓝来表示颜色。
HSV的范围大多数是如下定义的:
H: 0-360
S,V: 0-1
但也有的H:0-180
S:0-255
V:0-255
这取决于平台级软件的定义。
RGB一般就是取0-255,也有的去0-1.
下面是两者的转换代码:
/// <summary>
/// Convert HSV to RGB /// h is from 0-360 /// s,v values are 0-1 /// r,g,b values are 0-255 /// Based upon http://ilab.usc.edu/wiki/index.php/HSV_And_H2SV_Color_Space#HSV_Transformation_C_.2F_C.2B.2B_Code_2 /// </summary> void HsvToRgb(double h, double S, double V, out int r, out int g, out int b) { // ###################################################################### // T. Nathan Mundhenk // mundhenk@usc.edu // C/C++ Macro HSV to RGB double H = h; while (H < 0) { H += 360; }; while (H >= 360) { H -= 360; }; double R, G, B; if (V <= 0) { R = G = B = 0; } else if (S <= 0) { R = G = B = V; } else { double hf = H / 60.0; int i = (int)Math.Floor(hf); double f = hf - i; double pv = V * (1 - S); double qv = V * (1 - S * f); double tv = V * (1 - S * (1 - f)); switch (i) { // Red is the dominant color case 0: R = V; G = tv; B = pv; break; // Green is the dominant color case 1: R = qv; G = V; B = pv; break; case 2: R = pv; G = V; B = tv; break; // Blue is the dominant color case 3: R = pv; G = qv; B = V; break; case 4: R = tv; G = pv; B = V; break; // Red is the dominant color case 5: R = V; G = pv; B = qv; break; // Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here. case 6: R = V; G = tv; B = pv; break; case -1: R = V; G = pv; B = qv; break; // The color is not defined, we should throw an error. default: //LFATAL("i Value error in Pixel conversion, Value is %d", i); R = G = B = V; // Just pretend its black/white break; } } r = Clamp((int)(R * 255.0)); g = Clamp((int)(G * 255.0)); b = Clamp((int)(B * 255.0)); } /// <summary> /// Clamp a value to 0-255 /// </summary> int Clamp(int i) { if (i < 0) return 0; if (i > 255) return 255; return i; }
And here's how you'd use it:
int r,g,b; HsvToRgb(110, 1, 1, out r, out g, out b);
相关文章推荐
- OpenCV中RGB转HSV和HSV转RGB问题
- OpenCV中RGB和HSV转换的问题
- sws_scale解出来的RGB图像颠倒问题
- 有关IE8样式不兼容css样式问题处理方式--充分利用IE8
- 解决与HTTP 500 – 内部服务器错误错误信息有关的问题
- 有关不同浏览器不同版本的css以及js计算高度的问题
- 【有关问题处理】Icon.png pngcrush caught libpng error:Read
- 有关dup2(fd,1),复制文件句柄的问题
- 有关web中的权限问题
- Photoshop显示RGB值问题
- 有关iReport调用打印机的问题
- 有关linux下redis overcommit_memory的问题
- matlab实现RGB与HSV(HSB)、HSL和HSI的色彩空间互转
- 有关混淆的问题解决
- 学习向 Solaris 操作系统迁移 C 和 C++ 应用程序的有关问题与技巧
- 关于Java调用批处理命令向mysql导入数据的中文乱码有关问题
- 使用动态规划解决有关数字组合的问题
- 火狐上input密码框自动填充值的有关问题
- 【OpenCV】有关内存释放的一些问题
- 有关JNLP中传SESSIONID为参数的问题