为了表示不同的浓度值,对颜色条应用颜色梯度变化,基本方法是对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# 简单的颜色渐变算法
今天要用到一个颜色渐变的算法,网上看了很多,觉得都太繁琐,索性自己写一个。话不多说,直接上代码!
**这是用来获取某一颜色段的分度集合** ////// 获得某一颜色区间的颜色集合 /// /// 起始颜色 /// 终止颜色 /// 分度数 ///返回颜色集合 public static listgetsinglecolorlist(color srccolor, color descolor, int count) { list colorfactorlist = new list (); 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个区间的分度值,就得到全彩颜色集合** ////// 获取从红到紫的颜色段的颜色集合 /// /// 分度数 /// 是否从红到紫色渐变 ///返回颜色集合 public static listgetfullcolorlist(int totalcount, bool redtopurple = true) { list colorlist = new list (); 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; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
嗫?暁雲?