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:
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();
Conclusion
Hope it helps some