Generate Random Color Object in C#

This is a handy little snippet if you’re stubbing out a UI and need to initialize color pickers, enjoy!

private Color GetRandomColor()
{
    Random randomGen = new Random();
    KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
    KnownColor randomColorName = names[randomGen.Next(names.Length)];
    Color randomColor = Color.FromKnownColor(randomColorName);
    return randomColor;
}

Source: How to generate random color names in C#

Advertisements

Published by

Alan Feekery

Developer, Gamer, Musician, Cyclist and big Motorsport fan... enjoys the odd cup of coffee :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s