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.
- 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
I started from the excellent code found here on code project from Guillaume Leparmentier
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
- I added
ColorRatio class and
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.
The main usage is defined in the header comments of
ColorGenerator colorGenerator = new ColorGenerator();
colorGenerator.Speed = 50;
colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.Black, 4));
colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.White, 1.2));
colorGenerator.UsedColors.Add(new ColorGenerator.ColorRatio(System.Windows.Media.Colors.LightGray, 1));
System.Windows.Media.Color c = colorGenerator.GetNextColor();
Hope it helps some
I'm a Canadian-french developer for many years now.
I'm actually programming in C# but I also had some fun in the past with: VB, C++, SQL, Java, etc.
I like to play games like Carcassone, Settlers of Catane, Chess, Poker...
I also like photography.