Friday 18 July 2008

Random Pastel Colour Generator

One of the nice things about WPF is that it sets us free from the world of "battle-ship gray" that is Windows Forms and Win32, and lets us enter the promised land of glorious technicolored, gradient-filled User Interfaces with all kinds of fancy typography and bouncy animated buttons.

Of course, with great power comes great responsibility not to offend users' sensibilities with garish colour schemes. For artistically challenged developers there are sites like Kuler that help with composing tasteful colour schemes1. And now there's my offering.

This is something that a colleague (hi Simon!) and I put together when we were developing a WPF chart control back in the days of .Net 3.0 beta 2. We wanted to be able to generate charts with randomly coloured series, avoiding the horrible mishmash of colours with which Excel paints its charts. We came up with a random pastel colour generator.

The principle is quite simple. You just need to observe that, in the RGB colour model pleasing pastel palettes pop out when you pick red, green and blue values fairly close to each other in the middle of the range 0-256. It generates results like this:

Pastel Colours 1

and this:

Pastel Colours 2

Here's the code (in C#)2:

using System;
using System.Windows.Media;

/// <summary>
/// Provides a range of tasteful random pastel colors
/// </summary>
public class RandomPastelColorGenerator
{
    private readonly Random _random;

    public RandomPastelColorGenerator()
    {
        // seed the generator with 2 because
        // this gives a good sequence of colors
        const int RandomSeed = 2;
        _random = new Random(RandomSeed);
    }

    /// <summary>
    /// Returns a random pastel brush
    /// </summary>
    /// <returns></returns>
    public SolidColorBrush GetNextBrush()
    {
        SolidColorBrush brush = new SolidColorBrush(GetNext());
        // freeze the brush for efficiency
        brush.Freeze();

        return brush;
    }

    /// <summary>
    /// Returns a random pastel color
    /// </summary>
    /// <returns></returns>
    public Color GetNext()
    {
        // to create lighter colours:
        // take a random integer between 0 & 128 (rather than between 0 and 255)
        // and then add 127 to make the colour lighter
        byte[] colorBytes = new byte[3];
        colorBytes[0] = (byte)(_random.Next(128) + 127);
        colorBytes[1] = (byte)(_random.Next(128) + 127);
        colorBytes[2] = (byte)(_random.Next(128) + 127);

        Color color = new Color();

        // make the color fully opaque
        color.A = 255;
        color.R = colorBytes[0];
        color.B = colorBytes[1];
        color.G = colorBytes[2];

        return color;
    }
}

Footnotes

  1. Here's a list of other colour-scheme generators.
  2. If you've not entered the promised land of WPF, it shouldn't be too hard to convert this to Windows Forms lingo. I'll leave that as an exercise to the reader...

8 comments:

Pikaroy Dinarta pinem said...

Is a Great.
thanks

Anonymous said...

Amazing!
This is exactly what I was looking for!
Thank you!

spadman said...

Nice article thanks

Gfsgfs said...

Very Good!!!

Poty said...

Is there any posible way to translate this to work in ASP .Net?

Kellerman said...

Thanks! Very good!

Expletive_Deletedca said...

Winforms
private Random _random = new Random( 2 );

public Color GetNext()
{
return Color.FromArgb( 255, _random.Next(128) + 127, _random.Next (128) + 127, _random.Next(128) + 127 );
}

Samuele Contardi said...

After so much time... still useful. Thanks!

Post a Comment