简体   繁体   中英

Color transition for C# .NET Framework?

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, etc 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM