C# color gradient gradient

To represent different density values, a color gradient is applied to the color bar, the basic method is to multiply the ARGB components by a gradient coefficient.

Below is the process of applying three gradient values ​​to ten colors.

 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;
            //Initialize ten colors
            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};
                    
            //Ten colors, each with three depths
            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;
                // draw a comparison frame
                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();
                }
                //Draw the color bar gradient component
                for (int i = 0; i < data.Length; i++)
                {                    
                    // Divide the color into three depths
                    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;//Gas name
            int iGasConc;//Gas concentration// 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; }
            }
        }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326080496&siteId=291194637