C# 實現顏色的梯度漸變案例
阿新 • • 發佈:2021-01-16
為了表示不同的濃度值,對顏色條應用顏色梯度變化,基本方法是對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),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,color[i],color[i]); graphic2.FillRectangle(brush1,0 + iCall2 * i,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,new Point(iCall,c3,c4); graphic.FillRectangle(brush1,0 + iCall * i,iCall,iHigh); brush1.Dispose(); } } else { c4 = color[0]; Brush brush1 = new LinearGradientBrush(new Point(0,new Point(iLen,c4,c4); graphic.FillRectangle(brush1,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,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,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,totalCount / 5 + (totalCount % 5 > 0 ? 1 : 0))); colorList.AddRange(GetSingleColorList(Color.Blue,totalCount / 5 + (totalCount % 5 > 1 ? 1 : 0))); colorList.AddRange(GetSingleColorList(Color.Cyan,totalCount / 5 + (totalCount % 5 > 2 ? 1 : 0))); colorList.AddRange(GetSingleColorList(Color.Lime,totalCount / 5 + (totalCount % 5 > 3 ? 1 : 0))); colorList.AddRange(GetSingleColorList(Color.Yellow,Color.Red,totalCount / 5 + (totalCount % 5 > 4 ? 1 : 0))); } } return colorList; }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。