Color transition for C# .NET Framework?

480 Views Asked by At

So I'm trying to make an app with caption buttons: "close, minimize, maximize/restore", but I can't seem to figure out how to transition the R,G,B values of the color to another one smoothly.

I've gotten very good with Timers in C#, but I can't figure out how to calculate the values when transitioning.

My code so far: (the close, minimize, e.t.c stuffs are pre-made rectangles)

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    var pos = e.Location;
    
    if (close.Contains(pos))
    {
        Timer t = new Timer { Interval = 1 };
        t.Tick += delegate (object sender_, EventArgs e_)
        {
            // transition here
        };
        t.Start();
    }
    if (!close.Contains(pos))
    {
        Timer t = new Timer { Interval = 1 };
        t.Tick += delegate (object sender_, EventArgs e_)
        {
            // "reverse" code here
        };
        t.Start();
    }
}

NOTE: I AM NOT ASKING FOR AN ENTIRE CODE SNIPPET, just some mathematical solutions so I can do this easily.

1

There are 1 best solutions below

1
John Wu On BEST ANSWER

When the button is clicked, compute the time at which you wish the transition to complete. For example, you might take the current time and add 1 second. Store this value.

When the timer tick fires, perform these steps:

  1. Compute the distance between the color's current value and its final value
  2. Compute the time remaining until the transition is supposed to complete
  3. Divide 1 by 2.
  4. Nudge the color value by the result of 3.

Repeat this for each tick and you should always hit the final color right on time, even if some of the ticks fail or end up overlapping.