您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用C# 怎么實現一個顏色梯度漸變功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
為了表示不同的濃度值,對顏色條應用顏色梯度變化,基本方法是對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 brush2 = new LinearGradientBrush(new Point(0, iHigh), new Point(iCall2, iHigh), color[i], color[i]); graphic2.FillRectangle(brush2, 0 + iCall2 * i, 0, iCall2, iHigh); brush2.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 brush2 = new LinearGradientBrush(new Point(0, iHigh), new Point(iCall, iHigh), c3, c4); graphic.FillRectangle(brush2, 0 + iCall * i , 0, iCall, iHigh); brush2.Dispose(); } } else { c4 = color[0]; Brush brush2 = new LinearGradientBrush(new Point(0, iHigh), new Point(iLen, iHigh), c4, c4); graphic.FillRectangle(brush2, 0, 0, iLen, iHigh); brush2.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; }
看完上述內容,你們對使用C# 怎么實現一個顏色梯度漸變功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。