c# 实现 HSV 调色板
界面相关核心代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
var rnd = new Random();
var rgb = new ColorRGB(rnd.Next(, ), rnd.Next(, ), rnd.Next(, ));
var hsv = ColorHelper.RgbToHsv(rgb);
picDestinationColor.BackColor = Color.FromArgb(, rgb.R, rgb.G, rgb.B);
nudH.Value = hsv.H;
nudS.Value = hsv.S;
nudV.Value = hsv.V;
refreshColorPanel(hsv);
picPosH.Left = (int)(picH.Left + (picH.Width * nudH.Value / ));
picPosS.Left = (int)(picS.Left + (picS.Width * nudS.Value / ));
picPosV.Left = (int)(picV.Left + (picV.Width * nudV.Value / ));
} void refreshColorPanel(ColorHSV hsv)
{
var bm = new Bitmap(picH.Width, picH.Height);
using (Graphics g = Graphics.FromImage(bm))
{
for (var i = ; i < picH.Width; i++)
{
var hsvColor = new ColorHSV( * i / picH.Width, , );
var rgbColor = ColorHelper.HsvToRgb(hsvColor);
var color = Color.FromArgb(,rgbColor.R ,rgbColor.G ,rgbColor.B);
g.DrawLine(new Pen(color, ), i, , i, picH.Height);
}
g.Save();
}
picH.Image = bm; bm = new Bitmap(picS.Width, picS.Height);
using (Graphics g = Graphics.FromImage(bm))
{
for (var i = ; i < picS.Width; i++)
{
var hsvColor = new ColorHSV(hsv.H, * i / picS.Width, hsv.V);
var rgbColor = ColorHelper.HsvToRgb(hsvColor);
var color = Color.FromArgb(, rgbColor.R, rgbColor.G, rgbColor.B);
g.DrawLine(new Pen(color, ), i, , i, picS.Height);
}
g.Save();
}
picS.Image = bm; bm = new Bitmap(picV.Width, picV.Height);
using (Graphics g = Graphics.FromImage(bm))
{
for (var i = ; i < picV.Width; i++)
{
var hsvColor = new ColorHSV(hsv.H, hsv.S, * i / picS.Width);
var rgbColor = ColorHelper.HsvToRgb(hsvColor);
var color = Color.FromArgb(, rgbColor.R, rgbColor.G, rgbColor.B);
g.DrawLine(new Pen(color, ), i, , i, picV.Height);
}
g.Save();
}
picV.Image = bm; var destRgbColor = ColorHelper.HsvToRgb(hsv);
picDestinationColor.BackColor = Color.FromArgb(, destRgbColor.R, destRgbColor.G, destRgbColor.B); ;
} void colorChanged()
{
var hsv = new ColorHSV((int)nudH.Value, (int)nudS.Value, (int)nudV.Value);
refreshColorPanel(hsv);
} private void nudH_ValueChanged(object sender, EventArgs e)
{
colorChanged();
} private void nudS_ValueChanged(object sender, EventArgs e)
{
colorChanged();
} private void nudV_ValueChanged(object sender, EventArgs e)
{
colorChanged();
} private void picH_MouseDown(object sender, MouseEventArgs e)
{
nudH.Value = (int)( * e.X / picH.Width);
nudS.Value = nudS.Maximum;
nudV.Value = nudV.Maximum;
picPosH.Left = picH.Left + e.X;
} private void picS_MouseDown(object sender, MouseEventArgs e)
{
nudS.Value = (int)( * e.X / picS.Width);
picPosS.Left = picS.Left + e.X;
} private void picV_MouseDown(object sender, MouseEventArgs e)
{
nudV.Value = (int)( * e.X / picV.Width);
picPosV.Left = picV.Left + e.X;
}
}
下面是实体类和一个工具类: #region RGB / HSV / HSL 颜色模型类
/// <summary>
/// 类 名:ColorHSL
/// 功 能:H 色相 \ S 饱和度(纯度) \ L 亮度 颜色模型
/// 日 期:2015-02-08
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public class ColorHSL
{
public ColorHSL(int h, int s, int l)
{
this._h = h;
this._s = s;
this._l = l;
} private int _h;
private int _s;
private int _l; /// <summary>
/// 色相
/// </summary>
public int H
{
get { return this._h; }
set
{
this._h = value;
this._h = this._h > ? : this._h;
this._h = this._h < ? : this._h;
}
} /// <summary>
/// 饱和度(纯度)
/// </summary>
public int S
{
get { return this._s; }
set
{
this._s = value;
this._s = this._s > ? : this._s;
this._s = this._s < ? : this._s;
}
} /// <summary>
/// 饱和度
/// </summary>
public int L
{
get { return this._l; }
set
{
this._l = value;
this._l = this._l > ? : this._l;
this._l = this._l < ? : this._l;
}
}
} /// <summary>
/// 类 名:ColorHSV
/// 功 能:H 色相 \ S 饱和度(纯度) \ V 明度 颜色模型
/// 日 期:2015-01-22
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public class ColorHSV
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="h"></param>
/// <param name="s"></param>
/// <param name="v"></param>
public ColorHSV(int h, int s, int v)
{
this._h = h;
this._s = s;
this._v = v;
} private int _h;
private int _s;
private int _v; /// <summary>
/// 色相
/// </summary>
public int H
{
get { return this._h; }
set
{
this._h = value;
this._h = this._h > ? : this._h;
this._h = this._h < ? : this._h;
}
} /// <summary>
/// 饱和度(纯度)
/// </summary>
public int S
{
get { return this._s; }
set
{
this._s = value;
this._s = this._s > ? : this._s;
this._s = this._s < ? : this._s;
}
} /// <summary>
/// 明度
/// </summary>
public int V
{
get { return this._v; }
set
{
this._v = value;
this._v = this._v > ? : this._v;
this._v = this._v < ? : this._v;
}
}
} /// <summary>
/// 类 名:ColorRGB
/// 功 能:R 红色 \ G 绿色 \ B 蓝色 颜色模型
/// 所有颜色模型的基类,RGB是用于输出到屏幕的颜色模式,所以所有模型都将转换成RGB输出
/// 日 期:2015-01-22
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public class ColorRGB
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
public ColorRGB(int r, int g, int b)
{
this._r = r;
this._g = g;
this._b = b;
} private int _r;
private int _g;
private int _b; /// <summary>
/// 红色
/// </summary>
public int R
{
get { return this._r; }
set
{
this._r = value;
this._r = this._r > ? : this._r;
this._r = this._r < ? : this._r;
}
} /// <summary>
/// 绿色
/// </summary>
public int G
{
get { return this._g; }
set
{
this._g = value;
this._g = this._g > ? : this._g;
this._g = this._g < ? : this._g;
}
} /// <summary>
/// 蓝色
/// </summary>
public int B
{
get { return this._b; }
set
{
this._b = value;
this._b = this._b > ? : this._b;
this._b = this._b < ? : this._b;
}
} /// <summary>
/// 获取实际颜色
/// </summary>
/// <returns></returns>
public Color GetColor()
{
return Color.FromArgb(this._r, this._g, this._b);
}
}
#endregion
/// <summary>
/// 类 名:ColorHelper
/// 功 能:提供从RGB到HSV/HSL色彩空间的相互转换
/// 日 期:2015-02-08
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public static class ColorHelper
{
/// <summary>
/// RGB转换HSV
/// </summary>
/// <param name="rgb"></param>
/// <returns></returns>
public static ColorHSV RgbToHsv(ColorRGB rgb)
{
float min, max, tmp, H, S, V;
float R = rgb.R * 1.0f / , G = rgb.G * 1.0f / , B = rgb.B * 1.0f / ;
tmp = Math.Min(R, G);
min = Math.Min(tmp, B);
tmp = Math.Max(R, G);
max = Math.Max(tmp, B);
// H
H = ;
if (max == min)
{
H = ;
}
else if (max == R && G > B)
{
H = * (G - B) * 1.0f / (max - min) + ;
}
else if (max == R && G < B)
{
H = * (G - B) * 1.0f / (max - min) + ;
}
else if (max == G)
{
H = H = * (B - R) * 1.0f / (max - min) + ;
}
else if (max == B)
{
H = H = * (R - G) * 1.0f / (max - min) + ;
}
// S
if (max == )
{
S = ;
}
else
{
S = (max - min) * 1.0f / max;
}
// V
V = max;
return new ColorHSV((int)H, (int)(S * ), (int)(V * ));
} /// <summary>
/// HSV转换RGB
/// </summary>
/// <param name="hsv"></param>
/// <returns></returns>
public static ColorRGB HsvToRgb(ColorHSV hsv)
{
if (hsv.H == ) hsv.H = ; // 360为全黑,原因不明
float R = 0f, G = 0f, B = 0f;
if (hsv.S == )
{
return new ColorRGB(hsv.V, hsv.V, hsv.V);
}
float S = hsv.S * 1.0f / , V = hsv.V * 1.0f / ;
int H1 = (int)(hsv.H * 1.0f / ), H = hsv.H;
float F = H * 1.0f / - H1;
float P = V * (1.0f - S);
float Q = V * (1.0f - F * S);
float T = V * (1.0f - (1.0f - F) * S);
switch (H1)
{
case : R = V; G = T; B = P; break;
case : R = Q; G = V; B = P; break;
case : R = P; G = V; B = T; break;
case : R = P; G = Q; B = V; break;
case : R = T; G = P; B = V; break;
case : R = V; G = P; B = Q; break;
}
R = R * ;
G = G * ;
B = B * ;
while (R > ) R -= ;
while (R < ) R += ;
while (G > ) G -= ;
while (G < ) G += ;
while (B > ) B -= ;
while (B < ) B += ;
return new ColorRGB((int)R, (int)G, (int)B);
} /// <summary>
/// RGB转换HSL
/// </summary>
/// <param name="rgb"></param>
/// <returns></returns>
public static ColorHSL RgbToHsl(ColorRGB rgb)
{
float min, max, tmp, H, S, L;
float R = rgb.R * 1.0f / , G = rgb.G * 1.0f / , B = rgb.B * 1.0f / ;
tmp = Math.Min(R, G);
min = Math.Min(tmp, B);
tmp = Math.Max(R, G);
max = Math.Max(tmp, B);
// H
H = ;
if (max == min)
{
H = ; // 此时H应为未定义,通常写为0
}
else if (max == R && G > B)
{
H = * (G - B) * 1.0f / (max - min) + ;
}
else if (max == R && G < B)
{
H = * (G - B) * 1.0f / (max - min) + ;
}
else if (max == G)
{
H = H = * (B - R) * 1.0f / (max - min) + ;
}
else if (max == B)
{
H = H = * (R - G) * 1.0f / (max - min) + ;
}
// L
L = 0.5f * (max + min);
// S
S = ;
if (L == || max == min)
{
S = ;
}
else if ( < L && L < 0.5)
{
S = (max - min) / (L * );
}
else if (L > 0.5)
{
S = (max - min) / ( - * L);
}
return new ColorHSL((int)H, (int)(S * ), (int)(L * ));
} /// <summary>
/// HSL转换RGB
/// </summary>
/// <param name="hsl"></param>
/// <returns></returns>
public static ColorRGB HslToRgb(ColorHSL hsl)
{
float R = 0f, G = 0f, B = 0f;
float S = hsl.S * 1.0f / , L = hsl.L * 1.0f / ;
float temp1, temp2, temp3;
if (S == 0f) // 灰色
{
R = L;
G = L;
B = L;
}
else
{
if (L < 0.5f)
{
temp2 = L * (1.0f + S);
}
else
{
temp2 = L + S - L * S;
}
temp1 = 2.0f * L - temp2;
float H = hsl.H * 1.0f / ;
// R
temp3 = H + 1.0f / 3.0f;
if (temp3 < ) temp3 += 1.0f;
if (temp3 > ) temp3 -= 1.0f;
R = temp3;
// G
temp3 = H;
if (temp3 < ) temp3 += 1.0f;
if (temp3 > ) temp3 -= 1.0f;
G = temp3;
// B
temp3 = H - 1.0f / 3.0f;
if (temp3 < ) temp3 += 1.0f;
if (temp3 > ) temp3 -= 1.0f;
B = temp3;
}
R = R * ;
G = G * ;
B = B * ;
return new ColorRGB((int)R, (int)G, (int)B);
}
}
c# 实现 HSV 调色板的更多相关文章
- FlowNet2.0 安装指南
 \(安装环境: \color{red}{Ubuntu16.04 + CUDA8.0 + cuDNN5.0}\) 安装 CUDA CUDA 安装准备 CUDA 官方安装文档 首先查看是否电脑具有支持 ...
- [UWP]分享一个基于HSV色轮的调色板应用
1. 前言 上一篇文章介绍了HSV色轮,这次分享一个基于HSV色轮的调色板应用,应用地址:ColorfulBox - Microsoft Store 2. 功能 ColorfulBox是Adobe 色 ...
- UnityShader:HSV(色相,饱和度,亮度)转换
http://blog.csdn.net/costfine/article/details/46930473 发现其实美术调整颜色的时候大部分都是调整的HSV,因为可以方便的分别调整色相(hue).饱 ...
- Android 仿PhotoShop调色板应用(四) 不同区域颜色选择的颜色生成响应
版权声明:本文为博主原创文章,未经博主允许不得转载. Android 仿PhotoShop调色板应用(四) 不同区域颜色选择的颜色生成响应 上一篇讲过了主体界面的绘制,这里讲解调色板应用中的另外一 ...
- Android 仿PhotoShop调色板应用(三) 主体界面绘制
版权声明:本文为博主原创文章,未经博主允许不得转载. Android 仿PhotoShop调色板应用(三) 主体界面绘制 关于PhotoShop调色板应用的实现我总结了两个最核心的部分: 1 ...
- Android 仿PhotoShop调色板应用(一)概述
版权声明:本文为博主原创文章,未经博主允许不得转载. 在前面的系列我已经将Android中颜色渲染的原理及使用做了一个整体上概述. 现在开始根据一个比较复杂的实现进行具体的分析,这就是PhotoSho ...
- 第47课 Qt中的调色板
1. QPalette类 (1)QPalette类提供了绘制QWidget组件的不同状态所使用的颜色. (2)QPalette对象包含了3个状态的颜色描述 ①激活颜色组(Active):组件获得焦点使 ...
- HSV与RGB颜色空间的转换
一.本质上,H的取值范围:0~360 S的取值范围:0~1 V的取值范围:0~255 但是,当图像为32F型的时候,各 ...
- RGB与HSV颜色空间
RGB颜色空间 1.三基色原理:大多数的颜色可以通过红.绿.蓝三色按照不同的比例合成产生,同样绝大多数单色光也可以分解成红绿蓝三种色光 红绿蓝三基色按照不同的比例相加合成混色称为相加混色.其中一 ...
随机推荐
- java 的重写(覆盖) 和重载的区别
方法的的重写(覆盖) 在类继承中,子类可以修改从父类继承来的行为,也就是说子类能创建一个与父类方法有不同功能的方法,但具有相同的:名称.返回类型.参数列表.如果在子类中定义一个方法,其方法名称.返回值 ...
- 团队项目需求心得——慢阻肺疾病管理APP
一.项目介绍: 先上图: 这是我们小组开发项目的系统构成图. 本项目研发面向家庭/社区,对稳定期中慢阻肺患者病情和环境全方位监测.简单病情趋势分析和患者行为干预,并且提供患者与家庭医生和专家沟通渠道的 ...
- C++ MySQL编程
MySQL编程需要包含<mysql.h>头文件.该文件一般在MySQL安装目录下的include文件夹下. 包含头文件还不够,还需要包含“libmysql.lib”库,一般在lib文件夹下 ...
- mysql5.5.25 中文问题 my.ini没有及配置问题 修改默认编码方式
1.当你向mysql输入中文后,用select查询结果中文变成了问号,那你就往下看. 2.打开cmd,进入mysql,输入show variables like"%char%";可 ...
- Java学习——类与对象
在学习面向对象之前首先我们要理解一下几点: 什么是面向对象 对象的概念 类 类与对象的关系/区别 什么是对象的属性 什么是对象的方法 什么是面向对象.对象.类 讲到面向对象就不得提到面向过程,早期的计 ...
- [JsonSchema] 关于接口测试 Json 格式比对核心算法实现 (Java 版)
引言 为什么要自己重新造轮子,而不是采用第三方的JsonSchema方法进行实现存在以下痛点:1.我之前在网上找了很久,没有找到java版直接进行jsonschema生成的方法或直接比较的方法2.ht ...
- Arm11-mini6410入坑
Mini6410 学习Stm32的时候原子哥的资料真全啊,而且原子哥在他论坛也解答问题.然而现在学习ARM买了一套友善之臂的开发板,官方的资料只能刚好入门而已,其实入门都算不上.看了一下,网上的资料很 ...
- Java学习笔记(9)
lambda表达式: 只定义了单个方法的接口,称为单一抽象方法(Single Abstract Method, SAM)接口:由于此接口通过调用,完成某一功能,所以也称为功能接口,此类接口的实例称为功 ...
- Tomcat配置SSL后使用HTTP后跳转到HTTPS
Tomcat配置好SSL后将HTTP请求自动转到HTTPS需要在TOMCAT/conf/web.xml的未尾加入以下配置: <login-config> <!-- Authoriza ...
- shell截取指定字符串之间的内容
#!/bin/bash#截取字符串 #path=ss/usr/share/src/root/home/admin path=ss/usr/share/src/root/home/admin/src/a ...