简体   繁体   中英

generate random RGB color using rand() function

I need a function which will generate three numbers so I can use them as RGB pattern for my SVG.
While this is simple, I also need to make sure I'm not using the same color twice. How exactly do I do that? Generate one number at a time with simple rand (seed time active) and then what? I don't want to exclude a number, but maybe the whole pattern?
I'm kind of lost here.

To be precise, by first calling of this function I will get for example 218 199 154 and by second I'll get 47 212 236 which definitely are two different colors. Any suggestions?

Also I think a struct with int r, int g, int b would be suitable for this?

Edit: Colors should be different to the human eye. Sorry for not mentioning this earlier.

You could use a set to store the generated colors. First instanciate a new set. Then, every time you generate a color, look if the value is present in your set. If the record exists, skip it and retry for a new colour. If not, you can use it but dont forget to cache it in the Set after. This may become not performant if you need to generate a big quantity of colour.

The cheapest way to do this would be to use a Bloom filter which is very small memory wise, but leads to occasional false positives (ie, you will think you have used a colour, but you haven't). Basically, create three random numbers between 0-255, save them however you like, hash them as a triplet and place the hash in the filter.

Also, you might want to throw away the low bits of each channel since it's probably not easy to tell #FFFFF0 versus #FFFFF2.

Here is a simple way:

1.Generate a random integer.
2.Shift it 8 times to have 24 meaningful bits, store this integer value.
3.Use first 8 bits for R, second group of 8 bits for G,
      and the remaining 8 bits for B value.

For every new random number, shift it 8 times, compare all the other integer values that you stored before, if none of them matches with the new one use it for the new color(step3).

The differentiation by human eye is an interesting topic, because perceptional thresholds vary from one to another person. To achieve it shift the integer 14 times, get the first 6 bits for R(pad two 0s to get 8 bits again), get the second 6 bits for G, and last 6 bits for B. If you think that 6 bits are not good for it, decrease it 5,4...

Simple Run with 4 significant bits for each channel: My random integer is:

0101-1111-0000-1111-0000-1100-1101-0000

I shift(you can also use multiply or modulo) it to left 20 times:

0000-0000-0000-0000-0000-0101-1111-0000

store this value.

Then get first 4 bits for R second 4 bits for G and last 4 bits for B:

R: 0101
G: 1111
B: 0000

Pad them to make each of them 8 bits.

R: 0101-0000
G: 1111-0000
B: 0000-0000

Use those for your color components.

For each new random number after shifting it compare it with your stored integer values so far. If it is different, then store and use it for color.

One idea would be to use a bit vector to represent the set of colors generated. For 24-bit precision, the bit vector would be 2 24 bits long, which is 16,777,216 bits, or 2 MB. Certainly not a lot, these days, and it would be very fast to look up and insert colors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM