C# RGB和HSB相互转换
背景
最近做的项目中有这样一个场景,设置任意一种颜色,得到这种颜色偏深和偏浅的两种颜色。也就是说取该颜色同色系的深浅两种颜色。首先想到的是调节透明度,但效果不理想。后来尝试调节颜色亮度,发现这才是正解。但是.NET中不能直接改变Color的亮度,需要将Color转换成HSB模式,然后改变B的值调节亮度。调节亮度后需要再转换成我们熟悉的RGB模式才能使用。下面给出颜色转换方法。
代码
/// <summary>
/// 颜色转换帮助类
/// </summary>
public class ColorConversionHelper
{
/// <summary>
/// RGB转HSB
/// </summary>
/// <param name="red">红色值</param>
/// <param name="green">绿色值</param>
/// <param name="blue">蓝色值</param>
/// <returns>返回:HSB值集合</returns>
public static List<float> RGBtoHSB(int red, int green, int blue)
{
List<float> hsbList = new List<float>();
System.Drawing.Color dColor = System.Drawing.Color.FromArgb(red, green, blue);
hsbList.Add(dColor.GetHue());
hsbList.Add(dColor.GetSaturation());
hsbList.Add(dColor.GetBrightness());
return hsbList;
} /// <summary>
/// HSB转RGB
/// </summary>
/// <param name="hue">色调</param>
/// <param name="saturation">饱和度</param>
/// <param name="brightness">亮度</param>
/// <returns>返回:Color</returns>
public static Color HSBtoRGB(float hue, float saturation, float brightness)
{
int r = , g = , b = ;
if (saturation == )
{
r = g = b = (int)(brightness * 255.0f + 0.5f);
}
else
{
float h = (hue - (float)Math.Floor(hue)) * 6.0f;
float f = h - (float)Math.Floor(h);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - saturation * f);
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int)h)
{
case :
r = (int)(brightness * 255.0f + 0.5f);
g = (int)(t * 255.0f + 0.5f);
b = (int)(p * 255.0f + 0.5f);
break;
case :
r = (int)(q * 255.0f + 0.5f);
g = (int)(brightness * 255.0f + 0.5f);
b = (int)(p * 255.0f + 0.5f);
break;
case :
r = (int)(p * 255.0f + 0.5f);
g = (int)(brightness * 255.0f + 0.5f);
b = (int)(t * 255.0f + 0.5f);
break;
case :
r = (int)(p * 255.0f + 0.5f);
g = (int)(q * 255.0f + 0.5f);
b = (int)(brightness * 255.0f + 0.5f);
break;
case :
r = (int)(t * 255.0f + 0.5f);
g = (int)(p * 255.0f + 0.5f);
b = (int)(brightness * 255.0f + 0.5f);
break;
case :
r = (int)(brightness * 255.0f + 0.5f);
g = (int)(p * 255.0f + 0.5f);
b = (int)(q * 255.0f + 0.5f);
break;
}
}
return Color.FromArgb(Convert.ToByte(), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b));
}
}
扩展阅读
http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
http://my.oschina.net/soitravel/blog/79862
C# RGB和HSB相互转换的更多相关文章
- RGB和HSB的转换推算
RGB三原色是基于人肉眼对光线的生理作用.人眼内有三种椎状体“对这三种光线频率所能感受的带宽最大,也能独立刺激这三种颜色的受光体”,因此RGB称为三原色.比如,黄色波长的光对人眼的刺激效果,和红色与绿 ...
- RGB与HSB之间的转换公式
先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...
- RGB,CMYK,HSB各种颜色表示的转换 C#语言
Introduction Why an article on "colors"? It's the same question I asked myself before writ ...
- c# 颜色RGB到HSB互相转换
/// <summary> /// 色相,饱和度,亮度转换成rgb值 /// </summary> /// <returns></returns> pu ...
- RGB与HSB之间转换
先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...
- e585. Converting Between RGB and HSB Colors
This example demonstrates how to convert between a color value in RGB (three integer values in the r ...
- 【C#】RGB,CMYK,HSB各种颜色表示的转换(转)
[C#]RGB,CMYK,HSB各种颜色表示的转换 一.表示颜色的方式有很多种,如RGB,CMYK,HSB,Hex等等 1.RGB:这种表示颜色由三原色构成,通过红,绿,蓝三种颜色分量的不同,组合 ...
- 基于pcl 和 liblas 库 las与pcd格式(rgb点)相互转换(win10 VS2013 X64环境 )
#include <liblas/liblas.hpp> #include <iomanip> #include <iostream> #include <s ...
- RGB 与 HSB/HSV 的关系
能理解 RGB 模式中确定数值的各种颜色,但怎么理解「明度」.「饱和度」.「色相」等概念? 从第一张图可以简单得出以下结论: 明度--这个最简单,rgb中,三色光的值,其加起来的和越大,明度就越大. ...
随机推荐
- HTTP协议(转)
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- Spring4学习笔记1-HelloWorld与IOC和DI概述
1.安装环境 1.1安装eclipse,jdk 1.1安装Spring tool suite(非必要项) 2.spring HelloWorld 2.1 需要的jar包(spring官网下载:http ...
- Test your application
Creating automatic test suites for your application is a good way to make it robust. It allows you t ...
- 用css 制作三角
html代码: <div class="div"></div> css代码: .div{ border-top:40px solid #ff0077; bo ...
- Dense.js - 响应式的视网膜(Rtina)图像支持
Dense 是一款 jQuery 插件,它提供一个简单的方法为设备提供精密像素比的图像,为你的网站带来视网膜支持,清除模糊,图像更清晰.通过简单地包括 jQuery 插件的页面上,就能实现响应式的视网 ...
- strurts2入门
MVC :struts2是一个经典开源的mvc框架.主要负责控制器(Controller) 处理请求特点: 核心控制器: +Actions; 与ServletAPI解耦合: 非单例,线程安全. ...
- 开发者账号续期后,itunes停止付款了
开发者账号过期后,没有及时续期,等再续期后,itunes停止付款到公司银行账户了.过了一个多月了还是没有收到itunes的付款.然后开始联系苹果客服和技术支持,他们都说只能通过itunes的“联系我们 ...
- ios 颜色转图片
- (UIImage *)imageWithColor:(UIColor*) color{ CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); ...
- 深入.net(数据类型)
C#究竟为我们提供了哪些“数据类型”供我们使用?这些类型有什么样的“特征”? 数据类型的分类: --- 数据类型是存放数据的容器.那么我们就以它们“存放数据的方式”分类! 1.值类型:变量中直接存放着 ...
- Provisioning Profile文件在哪找?
~/Library/MobileDevice/Provisioning Profiles