DuckQueen
DuckQueen

Reputation: 810

Having 128 ints (as in ++i) how to generate 128 distinct RGB colors (int, int, int)?

I am iterating over polygon flags. I have 128 unique of tham. I wonder how to get for each a distingushable color (so that each flag would have its more or less distingushable color)? How to get such RGB ints from one int?

Having a frame based model stuff like rand()%255, rand()%255, rand()%255 does not make mush sense, while store colors fore each veriable is not what I intend. 255-i variations does not provide distingushable results. So I seek for an algorithm to get distingushable color from given i.

I have an iterator i and unsigned long long 128 bit mask (0001, 0010, 0100...).

Upvotes: 2

Views: 850

Answers (6)

Sandesh C
Sandesh C

Reputation: 1

how about this:

int get_red(int rep){
    int x,r=0,i,temp;
    i=0;
    temp=rep;
    while(temp!=0){
        i++;
        if(i==3) r=temp%2;
        temp=temp/2;
    }
    if(i>3) x=(long long int)255/(i-2);
    else x=255;
    temp=r*x;
    return temp;

}

int get_green(int rep){
    int x,g=0,i,temp;
    i=0;
    temp=rep;
    while(temp!=0){
        i++;
        if(i==2) g=temp%2;
        temp=temp/2;
    }
    if(i>3) x=(long long int)255/(i-2);
    else x=255;
    temp=g*x;
    return temp;
}

int get_blue(int rep){
    long long int x,b=0,i,temp;
    i=0;
    temp=rep;
    while(temp!=0){
        i++;
        if(i==1) b=temp%2;
        temp=temp/2;
    }
    if(i>3) x=(int)255/(i-2);
    else x=255;
    temp=b*x;
    return temp;
}

Upvotes: 0

Aki Suihkonen
Aki Suihkonen

Reputation: 20087

Simply using a grid r=4, g=8, b=4 is easy, but does not necessarily generate the best output, as there should be less different hues in the dark colors.

Processing in Lab-space would have the most equal perceptual distance between each unit, but I believe reasonable alternatives can be found from YUV or HSV.

Just from evaluating the heuristics / results from other answers (I'll try to find the best duplicate from gamedeveloper site), I think best results come other lattices than cubical.

   X   X   X
 X   X   X   X  <-- e.g. sampling points from hexagonical grid in HSV cone
   X   x   X    for some intensity level L.
 X   X   X   X
   X   X   X

The typical mistake IMO, is to align the next level L+-1 equally to L; Instead if the sampling points in the next intensity level L+-1 were rotated (by 45 degrees), there would be a difference in both hue and intensity of nearby colors. Another name for the idea is to give good hamming distance to nearby colors.

     x   x  L&1==0,    x    L&1==1
       *             x   x
     x   x             x

Also in the HSV cone there would be one black, one white, three hues in dark colors, three hues in bright colors, 6 or 12 colors in the next intensity level etc.

The second excellent remark in the game-dev answer was cultural bias. I would state the concept rather as that we give meanings to certain colours and can be very good in differentiating the color of sage from olive, or lavender from fuchsia.

EDIT The first answer in the link has 173 up-votes, even though e.g. I can't differentiate the colors 5 and 6. And there are only 10 colors. But the theory claims to be sound.

Upvotes: 2

JasonD
JasonD

Reputation: 16612

If you really need to generate these, rather than use a pre-generated palette, I would at least suggest not working in RGB.

For example, the Lab colour space is more closely related to the human visual system, and so might be more appropriate when trying to find perceptually distinct colours.

So you might try to generate colours which are far apart in Lab, and then convert back to RGB.

Upvotes: 1

rhughes
rhughes

Reputation: 9583

It depends on how distinct you want the follows.

If you are finding that creating the colors at runtime is too difficult, how about creating a lookup of pre-defined colors in a pallet that you know are distinct?

Upvotes: 1

Joni
Joni

Reputation: 111389

Generating a palette of optimally distinct colors is not easy. Since you say you need a fixed number of colors you can just pregenerate one, for example here:

http://tools.medialab.sciences-po.fr/iwanthue/

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

128 numbers is 7 bits worth of data. I would divide up the 7 bits into (2, 2, 3) and use those (in some order) as the high-order bits for red, green, and blue. That should spread out the colors as much as possible in the RGB color space.

Upvotes: 3

Related Questions