Map numeric values to colors in a rainbow in C#

Map numeric values to colors in a rainbow

Sometimes it’s useful to map numeric values to colors. For example, intensity of color could indicate population density, agricultural yield, rainfall, or other values on a map.

The following MapRainbowColor method maps a value between two bounds to a rainbow color. For example, you might want 0 to be represented by red and 100 to be represented by blue. In that case, the bounds would be 0 and 100.

// Map a value to a rainbow color.
private Color MapRainbowColor(
    float value, float red_value, float blue_value)
    // Convert into a value between 0 and 1023.
    int int_value = (int)(1023 * (value - red_value) /
        (blue_value - red_value));

    // Map different color bands.
    if (int_value < 256)
        // Red to yellow. (255, 0, 0) to (255, 255, 0).
        return Color.FromArgb(255, int_value, 0);
    else if (int_value < 512)
        // Yellow to green. (255, 255, 0) to (0, 255, 0).
        int_value -= 256;
        return Color.FromArgb(255 - int_value, 255, 0);
    else if (int_value < 768)
        // Green to aqua. (0, 255, 0) to (0, 255, 255).
        int_value -= 512;
        return Color.FromArgb(0, 255, int_value);
        // Aqua to blue. (0, 255, 255) to (0, 0, 255).
        int_value -= 768;
        return Color.FromArgb(0, 255 - int_value, 255);

The code first converts the value into an integer between 0 and 1023. It then determines the part of the rainbow that should hold the color, subtracts an appropriate amount from the integer value to put it within the correct range, and calculates an appropriate color within the range.

The following Paint event handler uses the MapRainbowColor method to fill the form with two rainbows.

// Draw rainbow colors on the form.
private void Form1_Paint(object sender, PaintEventArgs e)
    int wid = this.ClientSize.Width;
    int hgt = this.ClientSize.Height;
    int hgt2 = (int)(hgt / 2);
    for (int x = 0; x < wid; x++)
        using (Pen the_pen = new Pen(MapRainbowColor(x, 0, wid)))
            e.Graphics.DrawLine(the_pen, x, 0, x, hgt2);
        using (Pen the_pen = new Pen(MapRainbowColor(x, wid, 0)))
            e.Graphics.DrawLine(the_pen, x, hgt2, x, hgt);

This code gets the form's dimensions wid and hgt and then loops over the form's width one pixel at a time. For each pixel, the code maps the pixel's X coordinate to the range 0 to wid. It draws a vertical line segment on the top half of the form using the returned color. The result is a rainbow that shades from red to blue.

The code then maps the pixel's X coordinate again, this time switching the roles of the red and blue boundary values. It draws another vertical line, this time on the lower half of the form. This time the result is a rainbow that shades from blue to red.

Note that this method is really intended to calculate colors for data values, not to fill in large areas as it is used here. A LinearGradientBrush can fill areas with color gradients much more efficiently.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in graphics, mathematics and tagged , , , , , , , , , , . Bookmark the permalink.

3 Responses to Map numeric values to colors in a rainbow in C#

  1. Pingback: Draw a fractal generated by prime numbers in C# -

  2. Pingback: Use digits of Pi to draw a picture in C# - C# HelperC# Helper

  3. Pingback: Animate rainbow colors in C# - C# HelperC# Helper

Comments are closed.