Some questionable utils code

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
medved
Newbie
Newbie
Posts: 3
Joined: Fri Apr 12, 2013 12:00 am

Some questionable utils code

Post by medved » Fri Apr 19, 2013 3:04 am

in Utils.cs:

Code: Select all

    public static Color CalcColorBlend(Color start, Color end, int percentage)
    ...
            Rectangle rect = new Rectangle(0, 0, 100, 1);
            LinearGradientBrush brush = new LinearGradientBrush(rect, start, end, LinearGradientMode.Horizontal);
       if (bmp == null)
       {
          bmp = new Bitmap(100, 1);
       }
       Graphics g = Graphics.FromImage(bmp);
       g.FillRectangle(brush, rect);
       result = bmp.GetPixel(percentage, 0);
If I am not mistaken - in order to blend colors it creates a (STATIC!!) bitmap, draws a gradient on it then gets a pixel from the middle of it to get the blended color?

This is the most convoluted way I can think of to blend colors, when all you have to do is the RGB intrapolation to do the same, without calling GDI stuff at all. The reason why I even found this is because each of my charts sits in its own thread and the static bmp made them conflict.

For now I just put in my own code to do the blend. But PLEASE change this in the future releases.

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Some questionable utils code

Post by Sandra » Fri Apr 19, 2013 9:32 am

Hello medved,

Thanks for your information. I have added your request in wish-list with number [TF02016552] to consider its modification to upcoming versions of TeeChartFor.Net.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Some questionable utils code

Post by Christopher » Fri Jul 19, 2013 11:01 am

Hello!

You're right, the calculation is very simple for RGB colours, e.g.

Code: Select all

    public static Color CalcColorBlend2(Color start, Color end, int percentage)
    {
      double perc = percentage / 100.0;
      int a = Utils.Round(start.A + ((end.A - start.A) * perc));
      int r = Utils.Round(start.R + ((end.R - start.R) * perc));
      int g = Utils.Round(start.G + ((end.G - start.G) * perc));
      int b = Utils.Round(start.B + ((end.B - start.B) * perc)); 

      return Color.FromArgb(a, r, g, b);
    }
If we test this against the current CalcColorBlend with code like this:

Code: Select all

    private void DoColor()
    {
      Random rnd = new Random();
      Color one = Color.FromArgb(255, rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
      Color two = Color.FromArgb(255, rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
      int perc = rnd.Next(1, 100);

      Color result = Utils.CalcColorBlend(one, two, perc);
      Color result1 = Utils.CalcColorBlend2(one, two, perc);

      textBox1.Text = one.ToString();
      textBox2.Text = two.ToString();
      textBox5.Text = perc.ToString();
      textBox3.Text = result.ToString();
      textBox4.Text = result1.ToString();
    }
We get identical results. However, changing colors one and two to:

Code: Select all

      Color one = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
      Color two = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
the results become quite different, and there is no obvious way I can see to make the calculation. Do you know a way to make the calculation with transparency values?

Thank you,

Christopher Ireland
http://www.steema.com

Post Reply