您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。