Click here to Skip to main content
Click here to Skip to main content

Random Color Generator

By , 15 Sep 2011
 
This is an old version of the currently published tip/trick.

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.

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:

// 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

License

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

About the Author

Eric Ouellet
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberconmajia19 Jul '12 - 3:49 
GeneralRe: My vote of 5memberEric Ouellet19 Jul '12 - 3:54 
GeneralRe: Oh yes ! I just uncomment the lines in ColorGenerator to hav...memberherves29 Sep '11 - 22:26 
GeneralReason for my vote of 4 nicememberkiran dangar4 Oct '11 - 19:11 
GeneralJust what i need today (for a EPG program)!. Thanksmemberherves29 Sep '11 - 5:42 
GeneralRe: I'm happy to know. I hope it will be as expected ! memberEric Ouellet29 Sep '11 - 6:10 
GeneralReason for my vote of 5 nice, why didn't you make it a short...memberjohannesnestler21 Sep '11 - 4:28 
GeneralRe: Thanks a lot. I didn't write an article because I thought th...memberEric Ouellet21 Sep '11 - 5:27 
GeneralReason for my vote of 4 Interesting... Must admit the first...memberBrianBissell20 Sep '11 - 3:16 
GeneralRe: Thanks for your vote ! I agree the picture is a kind of hook...memberEric Ouellet20 Sep '11 - 3:30 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Eric Ouellet
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid