Fading Between Colors

I have a program in which I need to fade between two colors (to create a mouse over effect). Drawing is performed by a ColorBlend object and a LinearGradientBrush with its InterpolationColors property. The control I am writing should fade from one set of colors (in an array) to another, with a float parameter from 0.0f (use the first color) to 1.0f (use the second color). I have written code to do this by using a temporary bitmap object and drawing to it, however I have found this method to be quite slow...

I'm looking for a faster method to replace the one below that will take the same parameters and function in the same way to fade between two arrays of colors. Below is the code for my current method:


        internal static Color[] GetFadeColors(Color[] Original, Color[] New, float Progress)
        {
            if (Original.Length != New.Length)
            {
                throw new Exception("Parameters Incorrect: Original & New Color Arrays Must Have Same Length");
            }

            Color[] ret = new Color[Original.Length];

            Bitmap tmp = new Bitmap(1, Original.Length);
            Graphics g = Graphics.FromImage(tmp);

            int alpha = (int)(Progress * 255);

            for (int i = 0; i < Original.Length; i++)
            {
                Pen p = new Pen(Original[i]);
                g.DrawLine(p, 0, i, 1, i);
                p.Dispose();

                Color newcolor = Color.FromArgb(alpha, New[i]);
                p = new Pen(newcolor);
                g.DrawLine(p, 0, i, 1, i);
                p.Dispose();

                ret[i] = tmp.GetPixel(0, i);
            }

            g.Dispose();
            tmp.Dispose();

            return ret;
        }
LVL 5
paulb1989Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bob LearnedConnect With a Mentor Commented:
Obvious question:  Have you experimented with the increment value to see what the "best" value is?

Bob
"Mr. Obvious"
0
 
paulb1989Author Commented:
If it matters, the color arrays will typically have 4 or 5 colors in them, but it could increase in the future so I don't want the method to be reliant on the amount of items in the arrays.
0
 
Bob LearnedCommented:
I was wondering if HLS (Hue-Luminance-Saturation) functions might help.  Here is a VB.NET class that we can duplicate in C#, if it applies to your situation:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21633738.html

Bob
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
paulb1989Author Commented:
But I'm not just lightening and darkening colors, the two colors can be completely different
0
 
Bob LearnedCommented:
It not just lightening and darkening, but affecting color values, but I see your point, which is why I asked before continuing.  

There is also color transformations in GDI+, if that might prove to be useful:

GDI+ Graphics Transformation
http://www.awprofessional.com/articles/article.asp?p=102607&seqNum=8&rl=1

It looks like you are drawing a progress bar that has a color transform from one end of the bar to the other.

Bob
0
 
paulb1989Author Commented:
I'm not drawing a progress bar, I'm writing controls that immitate the office 2007 ribbon style. This code is used to fade in the colors that are used when the mouse is over a control (button, tab, etc) so that rather than just skipping from its normal look to its mouse over look, the control fades the mouse over look in, slowly (well, currently too slowly) changing from the normal colors to the colors used when the mouse is over the object.

As for the GDI+ link, I don't see anything about blending two colors to fade between them as I am doing, just ways to modify one color in a specific way. My code needs to take two colors and combine them to give back a color that is part way between the original color and the new color, when the colors are completely different (eg, typically the colors are moving from shades of blue to orange, or dark grays to orange).
0
 
Bob LearnedCommented:
>>the control fades the mouse over look in, slowly (well, currently too slowly)
Very good points.  I wasn't getting the implication of the color blend (it's been a long day).  

What is driving the slowly part?  Is there a timer or some other kind of similar method?

Bob
0
 
paulb1989Author Commented:
I have a timer set to fire every 50ms which increments the progress value by 0.1f each time, so in theory it should take 500ms to go from the original colors through to the new colors, but it actually takes around a second (may not sound like much but its noticeably slow).
0
 
paulb1989Author Commented:
OK I just tried incrementing by 0.2f and it does look a lot better, so unless anyone posts a more efficient method of fading the colors in the next few hours or so (I'm sure there must be a more efficient way than drawing to a bitmap), the points are yours. Otherwise they'll be split.
0
 
Bob LearnedCommented:
There are probably faster ways of getting information from the bitmap other than using GetPixel call, like using Scan0 + Stride.

Here is an example of what I mean:

Converting an RGB image to 1 bit-per-pixel monochrome.
http://www.bobpowell.net/onebit.htm

         Bitmap img = (Bitmap)Image.FromFile(dlg.FileName);
        //Ensure that it's a 32 bit per pixel file
        if(img.PixelFormat!=PixelFormat.Format32bppPArgb)
        {
          Bitmap temp=new Bitmap(img.Width,img.Height,PixelFormat.Format32bppPArgb);
          Graphics g=Graphics.FromImage(temp);
          g.DrawImage(img, new Rectangle(0,0,img.Width,img.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel);
          img.Dispose();
          g.Dispose();
          img=temp;
        }

        this.pictureBox1.Image=img;
        //lock the bits of the original bitmap
        BitmapData bmdo=img.LockBits(new Rectangle(0,0,img.Width,img.Height),ImageLockMode.ReadOnly,img.PixelFormat);
 
        //and the new 1bpp bitmap
        bm=new Bitmap(this.pictureBox1.Image.Width,this.pictureBox1.Image.Height,PixelFormat.Format1bppIndexed);
        BitmapData bmdn=bm.LockBits(new Rectangle(0,0,bm.Width,bm.Height),ImageLockMode.ReadWrite,PixelFormat.Format1bppIndexed);
 
        //for diagnostics
        DateTime dt=DateTime.Now;
       
        //scan through the pixels Y by X
        int x,y;
        for(y=0;y<img.Height;y++)
        {
          for(x=0;x<img.Width;x++)
          {
            //generate the address of the colour pixel
            int index=y*bmdo.Stride+(x*4);
            //check its brightness
            if(Color.FromArgb(Marshal.ReadByte(bmdo.Scan0,index+2),
                            Marshal.ReadByte(bmdo.Scan0,index+1),
                            Marshal.ReadByte(bmdo.Scan0,index)).GetBrightness()>0.5f)
              this.SetIndexedPixel(x,y,bmdn,true); //set it if its bright.
          }
        }
 
        //tidy up
        bm.UnlockBits(bmdn);
        img.UnlockBits(bmdo);
 
        //show the time taken to do the conversion
        TimeSpan ts=dt-DateTime.Now;
        System.Diagnostics.Trace.WriteLine("Conversion time was:"+ts.ToString());
 
        //display the 1bpp image.
        this.pictureBox2.Image=bm;

Bob
0
All Courses

From novice to tech pro — start learning today.