Friday, September 5, 2014

Linear Gradients in.NET 2D

Hi,
Here's a simple demo using Linear gradients (Color Manipulation) in .NET
Code:
private int _rColor = 100;
     
public int GenerateRGB(int cc)
{
 int rt = 0;
 if (cc <= 255)
 {
  rt = Information.RGB(255, cc, 0);
 }
 else if (cc < 511)
 {
  rt = Information.RGB(255 - (cc - 255), 255, 0);
 }
 else if (cc < 766)
 {
  rt = Information.RGB(0, 255, cc - 511);
 }
 else if (cc < 1022)
 {
  rt = Information.RGB(0, 255 - (cc - 767), 255);
 }
 else if (cc < 1277)
 {
  rt = Information.RGB(0, 0, 255 - (cc - 1022));
 }
 else
 {
  rt = Information.RGB(0, 0, 0);
 }
 return rt;
}

private void timer1_Tick(object sender, EventArgs e)
{
 this.Refresh();
 _rColor += 1;
 if (_rColor > 1500)
 {
  _rColor = 0;
 }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
 Color color = new Color();
 Random m_Rnd = new Random(); 
 color = Color.FromArgb(255, m_Rnd.Next(0, 255), m_Rnd.Next(0, 255), m_Rnd.Next(0, 255));

 using (LinearGradientBrush linGrBrush = new LinearGradientBrush(new Point(0, 10), new Point(200, 10), Color.FromArgb(GenerateRGB(_rColor)), color))
 {
  using (Pen mypen = new Pen(linGrBrush, 2))
  {
   e.Graphics.DrawLine(mypen, 0, 0, this.Width, this.Height);
  }
 }
}
Form Load:
After One Second, the color changes:

0 comments:

Post a Comment