Animate rainbow colors in C#

[example]

The example Map numeric values to and from colors in a color gradient in C# draws a color gradient that uses rainbow colors. This example animates that one.

When you click on the color gradient, the following code starts or stops the animation.

// Start or stop animating the rainbow colors.
private void picRainbow_MouseClick(object sender, MouseEventArgs e)
{
    // See if we should start or stop.
    if (tmrMoveSample.Enabled)
    {
        // Stop animating.
        tmrMoveSample.Enabled = false;
        return;
    }
    
    // Get the mouse position as a fraction
    // of the width of the PictureBox.
    float rainbow_color = e.X / (float)picRainbow.ClientSize.Width;

    // Convert into the corresponding color.
    SelectedColor = Rainbow.RainbowNumberToColor(rainbow_color);

    // Convert back into the corresponding number.
    SelectedRainbowNumber = Rainbow.ColorToRainbowNumber(SelectedColor);

    // Redraw.
    picRainbow.Refresh();
    picSample.Refresh();

    // Start animating.
    tmrMoveSample.Enabled = true;
}

If the Timer tmrMoveSample is enabled, then the program is currently animating the rainbow colors. In that case the code disables the Timer to stop the animation.

If the animation is not currently running, the code gets the numeric position of the color that you clicked and uses the RainbowNumberToColor method to convert it into a color. It uses the SelectedRainbowNumber method to convert that back into a number. (See the previous example for information about those methods.)

The method finishes by refreshing the rainbow and sample PictureBox controls and starting the Timer.

The following code shows the Timer component’s Tick event handler.

private const float ColorDelta = 0.02f;

// Continue animating the rainbow colors.
private void tmrMoveSample_Tick(object sender, EventArgs e)
{
    // Update the current color.
    SelectedRainbowNumber += ColorDelta;
    if (SelectedRainbowNumber > 1f)
        SelectedRainbowNumber = 0f;
    SelectedColor =
        Rainbow.RainbowNumberToColor(SelectedRainbowNumber);
   
    // Draw the new color.
    picRainbow.Refresh();
    picSample.Refresh();
}

This code adds the value ColorDelta to the selected color’s value. The value should be between 0 and 1, so if the result is greater than 1, the code resets it to 0.

The code uses the RainbowNumberToColor method to convert the new color number into a color and then refreshes the two PictureBox controls.

Download the example and see the previous example for additional details.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, animation, graphics, mathematics and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *