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.
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 swore that their was obvious duplicates. But their isn't. Not only did the picking work flawlessly, but I also ran through the Colors and didn't get a single collision. I am still wondering, though, could any problems arise from how the generator is currently built—besides hitting integer max, which will also never happen.
And also, do you guys know how I could randomize the colors more, so instead of getting different shades of red, I could recieve more colors along the spectrum?