C# 實(shí)現(xiàn)顏色的梯度漸變案例
為了表示不同的濃度值,對顏色條應(yīng)用顏色梯度變化,基本方法是對ARGB分量乘以一個漸變系數(shù)。
下面是對十種顏色應(yīng)用的三個梯度值的過程。
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; }
}
}

補(bǔ)充:C# 簡單的顏色漸變算法
今天要用到一個顏色漸變的算法,網(wǎng)上看了很多,覺得都太繁瑣,索性自己寫一個。話不多說,直接上代碼!
**這是用來獲取某一顏色段的分度集合**
/// <summary>
/// 獲得某一顏色區(qū)間的顏色集合
/// </summary>
/// <param name="sourceColor">起始顏色</param>
/// <param name="destColor">終止顏色</param>
/// <param name="count">分度數(shù)</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個區(qū)間,利用上面的算法拼接5個區(qū)間的分度值,就得到全彩顏色集合**
/// <summary>
/// 獲取從紅到紫的顏色段的顏色集合
/// </summary>
/// <param name="totalCount">分度數(shù)</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;
}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解
- C# Color.FromArgb()及系統(tǒng)顏色對照表一覽
- C# 如何設(shè)置label(標(biāo)簽)控件的背景顏色為透明
- C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能示例
- C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動及窗體顏色不斷變換效果
- C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
- C#更改tabControl選項(xiàng)卡顏色的方法
- C#及WPF獲取本機(jī)所有字體和顏色的方法
- C#實(shí)現(xiàn)更改MDI窗體背景顏色的方法
- c# 顏色選擇控件的實(shí)現(xiàn)代碼
相關(guān)文章
在C#里面給PPT文檔添加注釋的實(shí)現(xiàn)代碼
平常開會或者做總結(jié)報(bào)告的時候我們通常都會用到PowerPoint演示文稿,我們可以在單個幻燈片或者全部幻燈片里面添加注釋,這樣觀眾可以從注釋內(nèi)容里面獲取更多的相關(guān)信息,需要的朋友可以參考下2017-01-01
c# 使用Entity Framework操作Access數(shù)據(jù)庫的示例
本篇文章主要介紹了c# 使用Entity Framework操作Access數(shù)據(jù)庫的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
C#實(shí)現(xiàn)獲取設(shè)置IP地址小工具
c# 開發(fā),方便更改IP地址。由于公司和家里的ip設(shè)置不一樣,公司要求手動設(shè)置,在家可以自動獲取IP,切都是無線網(wǎng)絡(luò),為了方便操作,故做了這個小工具!2015-06-06
C#將Sql數(shù)據(jù)保存到Excel文件中的方法
這篇文章主要介紹了C#將Sql數(shù)據(jù)保存到Excel文件中的方法,文中的ExportExcel可起到將sql數(shù)據(jù)導(dǎo)出為Excel的作用,需要的朋友可以參考下2014-08-08
unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能
workerman?是一款開源高性能?PHP?應(yīng)用容器,他除了用于互聯(lián)網(wǎng)、即時通訊、APP?開發(fā)、硬件通訊、智能家居、物聯(lián)網(wǎng)等領(lǐng)域的開發(fā)外,這篇文章主要介紹了unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲,需要的朋友可以參考下2022-10-10
Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03

