Reputation: 19
I would like to make a function that creates an unique color based on int variables. The problem is range of these variables (from 1 to 10 000).
So i have an array with 10 000 fields of which each has its own ID. And now i need to set color of each field based on that id;
void setColor(int ID); // set R G B
Any suggestions?
Upvotes: 0
Views: 282
Reputation:
You can omit the table and build a hash function. A simple one is:
rgb(unsigned x) {
b = x & 0xFF;
x >>= 8
g = x & 0xFF;
x >>= 8
r = x & 0xFF;
}
The distribution is not nice here: 2^24 - 10000 = 16767216
An alternative hash could calculate distributed = id * 2^24 / 10000.
Upvotes: 0
Reputation: 21813
The simplest solution is simply to convert the ID into a RGB value directly. It certainly won't result in a visually distinctive range of colours, however.
Upvotes: 0
Reputation: 1271
You have 10,000 possible input options, whereas 256 x 256 x 256 = 16 million output options. Assuming you want to vary RGB values equally, you can at max use 21 diff values for R,G,B as that would yield 21.5^3 ~ 10000.
So you need to take a step of 256/21 ~ 12 to serve your purpose.
So, basically my point is :
int RGB value
1 0,0,0
2 0,0,12
3 0,0,24
.
..
22 0,0,253
23 0,12,0
.
.
.
10000 255,255,255
Upvotes: 4