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 or I guess I should say "unique shade of red and black".
When I first ran it, I sworecould swear that their wasthere were obvious duplicates. But their isn't, but there aren't. Not only did the picking work flawlessly, but I also ran through the ColorsColor
s and didn't get a single collision. Though I am still wondering, though, could if any problems could arise from how the generator is currently built—besides hitting integer max, which will also never happen.
And alsoAlso, do you guys know how can I could randomize the colors more, so instead of getting different shades of red, I could recievereceive more colors along the spectrum?