1

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?

Krythic
  • 4,184
  • 5
  • 26
  • 67
  • This question really is better suited towards CodeReview, [where you already posted](http://codereview.stackexchange.com/questions/87050/generating-unique-random-colors) this question. Although, your last sentence may be a better stand alone question for stack overflow – Sayse Apr 16 '15 at 06:40
  • @adricadar It's already on codereview. I just posted it. – Krythic Apr 16 '15 at 06:40
  • 1
    *'how I could (....) recieve more colors along the spectrum?'* Just don't stop after 16384 steps but run to 16777216 instead. – CiaPan Apr 16 '15 at 06:41
  • You can get some pretty awesome gradients if you increase the index growth, too. http://imgur.com/yUrboaC – Krythic Apr 16 '15 at 06:48
  • @CiaPan That actually is a false statement. I just toyed with the index, it only makes the gradient more spread out. – Krythic Apr 16 '15 at 06:50
  • Did you try to replace `>> 08` with `>> 8`...? – CiaPan Apr 16 '15 at 08:27

2 Answers2

1

If you work with HSL color model, you might have better results. RGB values may end up skewing towards darker or lighter colors. If you use HSL (Hue, Saturation and Light) then you would set Saturation and light to be either fixed or narrow-ranged values and then choose a random hue.

Saturation runs from 0% ( denatured ) to 100% (fully saturated) Light runs from 0% (black) to 100% (white) with 50% being avg. And hue is a value between 0 and 360º on the color wheel.

Using this color space may result in a better random color generator, or at least one with more fine-tuning control.

Armstrongest
  • 15,181
  • 13
  • 67
  • 106
  • 1
    @Krythic Better than that. A whole thread with some great examples: http://stackoverflow.com/questions/359612/how-to-change-rgb-color-to-hsv – Armstrongest Apr 16 '15 at 06:55
0

I just wanted to post my own little something. I won't be accepting this as the answer, but I just wanted to show a simple mockup using the suggestions that I had already recieved. Mainly from Codereview: Codereview Link

public class UniqueColorGenerator {
        private int _colorIndex;
        private readonly int _offsetIncrement;

        public UniqueColorGenerator() {
            this._offsetIncrement = 1;
        }

        public UniqueColorGenerator( uint offsetIncrement ) {
            this._offsetIncrement = ( int )offsetIncrement;
        }

        public Color Next() {
            return Color.FromArgb( _colorIndex += _offsetIncrement );
        }
    }

That class does the exact same thing as my original example, but is a lot simpler, and even adds room for a bit of uniqueness. The higher number that you add in the constructor, the more crazy the final pattern becomes; here is a quick example of what happens if you feed "2834783" into the constructor:

enter image description here

And yes, all of the colors are still unique. The only issue that I can see from this new code is an issue that would arise if the user put in a ridiculously high number.

Community
  • 1
  • 1
Krythic
  • 4,184
  • 5
  • 26
  • 67