patterncsharpMinor
Generating Unique Colors
Viewed 0 times
uniquegeneratingcolors
Problem
I just wrote a small class that generates perfectly unique colors(or that is the plan anyway). I found the original index shifting snippet online, and was just wondering if it could be improved in anyway, or if any problems could arise from using it.
Here is the class which is used to create the unique colors.
I am using this for "geometry picking" in OpenGL. It works beautifully, too. Here is a picture of 16,384 quads with their own unique color... or I guess I should say "unique shade of red and black".
When I first ran it, I could swear that there were obvious duplicates, but there aren't. Not only did the picking work flawlessly, but I also ran through the
Also, how can I randomize the colors more, so instead of getting different shades of red, I could receive more colors along the spectrum?
Here is the class which is used to create the unique colors.
public class UniqueColorGenerator
{
private int _colorIndex;
public UniqueColorGenerator()
{
}
public Color Next()
{
_colorIndex++;
byte red = ( byte )( _colorIndex & 0x000000FF );
byte green = ( byte )( ( _colorIndex & 0x0000FF00 ) >> 08 );
byte blue = ( byte )( ( _colorIndex & 0x00FF0000 ) >> 16 );
return Color.FromArgb( red , green , blue );
}
}I am using this for "geometry picking" in OpenGL. It works beautifully, too. Here is a picture of 16,384 quads with their own unique color... or I guess I should say "unique shade of red and black".
When I first ran it, I could swear that there were obvious duplicates, but there aren't. Not only did the picking work flawlessly, but I also ran through the
Colors and didn't get a single collision. Though I am still wondering if any problems could arise from how the generator is currently built—besides hitting integer max, which will also never happen.Also, how can I randomize the colors more, so instead of getting different shades of red, I could receive more colors along the spectrum?
Solution
You don't need the constructor, as it's empty.
You're taking red as the low bits and blue as the high bits, which is backwards.
You appear to be using
You also wouldn't need to use
You're taking red as the low bits and blue as the high bits, which is backwards.
You appear to be using
System.Drawing and not System.Windows.Media, judging by the overloads. If so, there is an overload that takes an int:public class UniqueColorGenerator
{
private int _colorIndex;
public Color Next()
{
return Color.FromArgb(++_colorIndex);
}
}You also wouldn't need to use
&, as casting to a byte will cut off the upper bits. return Color.FromArgb(
(byte)(_colorIndex >> 24),
(byte)(_colorIndex >> 16),
(byte)(_colorIndex >> 8),
(byte)_colorIndex);Code Snippets
public class UniqueColorGenerator
{
private int _colorIndex;
public Color Next()
{
return Color.FromArgb(++_colorIndex);
}
}return Color.FromArgb(
(byte)(_colorIndex >> 24),
(byte)(_colorIndex >> 16),
(byte)(_colorIndex >> 8),
(byte)_colorIndex);Context
StackExchange Code Review Q#87050, answer score: 7
Revisions (0)
No revisions yet.