Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#
Tip/Trick

Random Color Generator

Rate me:
Please Sign up or sign in to vote.
4.67/5 (15 votes)
17 Jun 2014CPOL3 min read 84.8K   15   10

Before reading...

Update 2014-06-17: Please check the following link before reading further beause it is newer and appears to be a lot better than what I have made: https://www.nuget.org/packages/Colourful/

Thanks to Member10716758 which I suspect it is Tompazourek to point me out this :-).

Introduction

I wanted to create a random color generator.

 

The initial application was a plot color attribution for a graphic control.

 

I wanted to be able to put some weight to some color to ensure to stay away from them (like the background color). I haven’t found anything that does that. I decided to code it myself.

Needs

  • Never the same color twice
  • Each new color should be easily distinguishable from any previous one
  • Be able to set inital colors to reject
  • Be able to add a distance factor to any inital colors to reject in order to stay very far from them

Approach

I searched on the web for an already existing solution and I haven’t found any. I found many random generators but they didn’t keep tracks of already used colors and do not ensure to generate color far from used ones. Also, I needed to be able to add myself some color to reject. An additional nice feature would be to start generate color with very high distance between them and reduce the distance while color quantity increase (in order to introduce new ones).

After reading some article/code (like: http://www.codeproject.com/KB/recipes/colorspace1.aspx), I realized that it would be probably more easier/effective to work with HSB or HSL. I thought HSL would be better and go with it. Obtained results were sometimes with colors appearing to be too close to my eye. I decided to try to work with CieLab color distance and saw an improvement. The color appears to me to have a greater distance between them. This initial "Tips and tricks" was written with CieLab1997 algorithm. But Rob2412 (Thanks a lot Rob) give me the algorithm of CieLab2000 (http://www.codeproject.com/KB/recipes/colorspace1.aspx?msg=4018256#xx4018256xx) then I implemented it, tested it, and decided to update my code. Although, I haven’t seen a difference, the latest should give better results.

The Solution

I started from the excellent code found here on code project from Guillaume Leparmentier: http://www.codeproject.com/KB/recipes/colorspace1.aspx?msg=4018256#xx4018256xx

In fact, it was so good that I kept 99.9% of it and mainly just added 2 others classes (plus a Form to test).

I only added/modify:

  • I changed Color class used from WinForm (System.Drawing) to WPF (System.Windows.Media)
  • I added Distance calculation in CIELab.cs
  • I added a helper function GetColorDistanceCIELab in ColorSpaceHelper class
  • I added ColorGenerator, ColorRatio class and FormTestColorGenerator.

The class is based on random color generation with auto adjustable distance tolerance. High distance tolerance at start but decrease while adding new colors.

The code references Framework.Net 4.0 (and WPF) and the project is VS2010.

References

Usage

The main usage is defined in the header comments of ColorGenerator class

Comments copy:

C++
// Main usage:
    ColorGenerator colorGenerator = new ColorGenerator();
    
    // Faster, little bit more color with less distance 
    colorGenerator.Speed = 50; 
    
    // My background, 4 to stay far away 
    colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.Black, 4)); 
    // No White
    colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.White, 1.2)); 
    
    // Grid Color
    colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.LightGray, 1)); 
    System.Windows.Media.Color c = colorGenerator.GetNextColor();

Conclusion

Hope it helps some

Revision History

Latest version of the code include (2011-10-05):

  • CieLab2000 color distance calculation
  • Corrections on D65 white reference constants – calculation
  • Calculations are all made with D65 white reference

Thanks a lot to Rob2412 to bring me corrections and help me to understand a little bit better the color world (alternatives 4 and 5) .

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IREQ - Hydro-Quebec
Canada Canada
Hi,

I'm a french Canadian software developer.
Work at Hydro-Quebec / IREQ.
Live in Sainte-Julie, Quebec, Canada (South shore of Montreal)

WebPage: http://www.ericouellet.com
GMail: ericouellet2

Salut!

Comments and Discussions

 
GeneralMy vote of 5 Pin
conmajia19-Jul-12 3:49
conmajia19-Jul-12 3:49 
GeneralRe: My vote of 5 Pin
Eric Ouellet19-Jul-12 3:54
professionalEric Ouellet19-Jul-12 3:54 
GeneralRe: Oh yes ! I just uncomment the lines in ColorGenerator to hav... Pin
herves29-Sep-11 22:26
herves29-Sep-11 22:26 
GeneralReason for my vote of 4 nice Pin
kiran dangar4-Oct-11 19:11
kiran dangar4-Oct-11 19:11 
GeneralJust what i need today (for a EPG program)!. Thanks Pin
herves29-Sep-11 5:42
herves29-Sep-11 5:42 
GeneralRe: I'm happy to know. I hope it will be as expected ! Pin
Eric Ouellet29-Sep-11 6:10
professionalEric Ouellet29-Sep-11 6:10 
GeneralReason for my vote of 5 nice, why didn't you make it a short... Pin
johannesnestler21-Sep-11 4:28
johannesnestler21-Sep-11 4:28 
GeneralRe: Thanks a lot. I didn't write an article because I thought th... Pin
Eric Ouellet21-Sep-11 5:27
professionalEric Ouellet21-Sep-11 5:27 
GeneralReason for my vote of 4 Interesting... Must admit the first... Pin
BrianBissell20-Sep-11 3:16
BrianBissell20-Sep-11 3:16 
GeneralRe: Thanks for your vote ! I agree the picture is a kind of hook... Pin
Eric Ouellet20-Sep-11 3:30
professionalEric Ouellet20-Sep-11 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.