Skip to main content
edited title
Link
Krythic
  • 1.5k
  • 1
  • 11
  • 20

Generating Unique Random Colors

fixed typos and improved some of the text
Source Link
BCdotWEB
  • 11.3k
  • 2
  • 27
  • 45

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".

enter image description here

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 ColorsColors 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?

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".

enter image description here

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?

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".

enter image description here

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?

deleted 2 characters in body
Source Link
Krythic
  • 1.5k
  • 1
  • 11
  • 20

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".

enter image description here

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?

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".

enter image description here

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?

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".

enter image description here

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?

Source Link
Krythic
  • 1.5k
  • 1
  • 11
  • 20
Loading