C# 实现颜色的梯度渐变案例
作者:不听不看不说 时间:2023-11-20 22:01:06
为了表示不同的浓度值,对颜色条应用颜色梯度变化,基本方法是对ARGB分量乘以一个渐变系数。
下面是对十种颜色应用的三个梯度值的过程。
public void DrawRect(gasConcentration[] data)
{
Graphics graphic = pictureBox1.CreateGraphics();
Graphics graphic2 = pictureBox2.CreateGraphics();
int iCall2 = pictureBox2.Width/10;
data = new gasConcentration[40];
int iLen = pictureBox1.Width = 540;
int iHigh = pictureBox1.Height;
//初始化十种颜色
Color[] color = new Color[10] { Color.FromArgb(240, 0, 0), Color.Green, Color.Yellow, Color.Blue, Color.SteelBlue, Color.SeaGreen,
Color.Chartreuse, Color.SaddleBrown, Color.Violet, Color.BurlyWood};
//十个颜色,每个颜色三个深度
for (int i = 0; i < 40; i++)
{
data[i].gasType = i/4 + 1;
data[i].gasConc = i%4;
}
Color c3, c4;
if (data.Length > 0)
{
int iCall = iLen / data.Length;
pictureBox2.Width = iCall * data.Length;
pictureBox1.Width = iCall * data.Length;
iCall2 = iCall * 4;
//画对比框条
for (int i = 0; i < 10; i++)
{
Brush brush1 = new LinearGradientBrush(new Point(0, iHigh), new Point(iCall2, iHigh), color[i], color[i]);
graphic2.FillRectangle(brush1, 0 + iCall2 * i, 0, iCall2, iHigh);
brush1.Dispose();
}
//画颜色条梯度分量
for (int i = 0; i < data.Length; i++)
{
//将颜色分为三个深度
if (data[i].gasConc != 0)
c3 = c4 = Color.FromArgb((byte)(255 * (float)(1 - (data[i].gasConc * 0.01))),
(byte)(color[data[i].gasType-1].R * (float)(1 - (data[i].gasConc * 0.2))),
(byte)(color[data[i].gasType-1].G * (float)(1 - (data[i].gasConc * 0.2))),
(byte)(color[data[i].gasType-1].B * (float)(1 - (data[i].gasConc * 0.2))));
else
c3 = c4 = Color.Black;
Brush brush1 = new LinearGradientBrush(new Point(0, iHigh), new Point(iCall, iHigh), c3, c4);
graphic.FillRectangle(brush1, 0 + iCall * i , 0, iCall, iHigh);
brush1.Dispose();
}
}
else
{
c4 = color[0];
Brush brush1 = new LinearGradientBrush(new Point(0, iHigh), new Point(iLen, iHigh), c4, c4);
graphic.FillRectangle(brush1, 0, 0, iLen, iHigh);
brush1.Dispose();
}
}
public struct gasConcentration
{
int iGasType;//气体名称
int iGasConc;//气体浓度 // 0=no, 1=low, 2=med, 3=high
public int gasType { get { return iGasType; }
set { iGasType = value; } }
public int gasConc { get { return iGasConc; }
set { iGasConc = value; }
}
}
补充:C# 简单的颜色渐变算法
今天要用到一个颜色渐变的算法,网上看了很多,觉得都太繁琐,索性自己写一个。话不多说,直接上代码!
**这是用来获取某一颜色段的分度集合**
/// <summary>
/// 获得某一颜色区间的颜色集合
/// </summary>
/// <param name="sourceColor">起始颜色</param>
/// <param name="destColor">终止颜色</param>
/// <param name="count">分度数</param>
/// <returns>返回颜色集合</returns>
public static List<Color> GetSingleColorList(Color srcColor, Color desColor, int count)
{
List<Color> colorFactorList = new List<Color>();
int redSpan = desColor.R - srcColor.R;
int greenSpan = desColor.G - srcColor.G;
int blueSpan = desColor.B - srcColor.B;
for (int i = 0; i < count; i++)
{
Color color = Color.FromArgb(
srcColor.R + (int)((double)i / count * redSpan),
srcColor.G + (int)((double)i / count * greenSpan),
srcColor.B + (int)((double)i / count * blueSpan)
);
colorFactorList.Add(color);
}
return colorFactorList;
}
**这里就是将红到紫之间的颜色分为5个区间,利用上面的算法拼接5个区间的分度值,就得到全彩颜色集合**
/// <summary>
/// 获取从红到紫的颜色段的颜色集合
/// </summary>
/// <param name="totalCount">分度数</param>
/// <param name="redToPurple">是否从红到紫色渐变</param>
/// <returns>返回颜色集合</returns>
public static List<Color> GetFullColorList(int totalCount, bool redToPurple = true)
{
List<Color> colorList = new List<Color>();
if (totalCount > 0)
{
if (redToPurple)
{
colorList.AddRange(GetSingleColorList(Color.Red, Color.Yellow, totalCount / 5 + (totalCount % 5 > 0 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Yellow, Color.Lime, totalCount / 5 + (totalCount % 5 > 1 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Lime, Color.Cyan, totalCount / 5 + (totalCount % 5 > 2 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Cyan, Color.Blue, totalCount / 5 + (totalCount % 5 > 3 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Blue, Color.Magenta, totalCount / 5 + (totalCount % 5 > 4 ? 1 : 0)));
}
else
{
colorList.AddRange(GetSingleColorList(Color.Magenta, Color.Blue, totalCount / 5 + (totalCount % 5 > 0 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Blue, Color.Cyan, totalCount / 5 + (totalCount % 5 > 1 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Cyan, Color.Lime, totalCount / 5 + (totalCount % 5 > 2 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Lime, Color.Yellow, totalCount / 5 + (totalCount % 5 > 3 ? 1 : 0)));
colorList.AddRange(GetSingleColorList(Color.Yellow, Color.Red, totalCount / 5 + (totalCount % 5 > 4 ? 1 : 0)));
}
}
return colorList;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/fanfan513/article/details/79970522
标签:C#,颜色,梯度渐变
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java synchronized同步方法详解
2022-06-27 02:46:37
![](https://img.aspxhome.com/file/2023/9/83009_0s.png)
关于Spring Data Jpa 自定义方法实现问题
2023-11-28 10:08:32
C#读写Config配置文件案例
2022-10-22 20:11:09
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决
2022-06-14 23:21:51
Java Redis Redisson配置教程详解
2022-10-13 06:32:39
c#创建浮动工具栏功能示例
2022-08-31 01:08:31
![](https://img.aspxhome.com/file/2023/7/82237_0s.png)
使用SpringBoot获取resources文件路径
2022-10-12 15:29:56
![](https://img.aspxhome.com/file/2023/9/66639_0s.png)
Swagger注解-@ApiModel和@ApiModelProperty的用法
2023-02-05 23:57:48
Java C++分别实现滑动窗口的最大值
2023-04-12 02:59:21
![](https://img.aspxhome.com/file/2023/1/124701_0s.png)
C++ 中String 替换指定字符串的实例详解
2021-06-05 19:08:23
详解Spring Cloud中Hystrix的请求合并
2022-07-06 14:53:06
![](https://img.aspxhome.com/file/2023/7/86867_0s.png)
简单理解Java的垃圾回收机制与finalize方法的作用
2023-02-04 01:49:37
![](https://img.aspxhome.com/file/2023/6/128236_0s.jpg)
java组件commons-fileupload文件上传示例
2022-08-16 02:42:56
![](https://img.aspxhome.com/file/2023/2/97792_0s.jpg)
一文带你了解RabbitMQ消息转换器
2023-11-15 23:22:28
![](https://img.aspxhome.com/file/2023/4/64384_0s.jpg)
Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
2022-03-05 22:44:24
android UI绘制加减号按钮
2023-09-11 02:55:28
![](https://img.aspxhome.com/file/2023/1/128981_0s.jpg)
C#利用性能计数器监控网络状态
2022-01-05 00:13:53
![](https://img.aspxhome.com/file/2023/6/119506_0s.png)
Android编程实现自定义进度条颜色的方法
2023-07-24 07:40:58
![](https://img.aspxhome.com/file/2023/8/138038_0s.jpg)
springboot ErrorPageFilter的实际应用详解
2023-11-24 01:02:59
简单了解springboot加载配置文件顺序
2022-06-19 13:36:29
![](https://img.aspxhome.com/file/2023/6/132166_0s.jpg)