Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All RGB colors combinations in Javascript

Tags:

javascript

rgb

I want all RGB colors in Javascript. I have made this schema;

R   G   B
0   0   0
255 255 255
0   255 255
255 0   255
255 255 0
0   255 0
0   0   255
255 0   0

And I made this in Javascript: click

Do I have now all possible combinations with the RGB colors?

like image 543
DazDylz Avatar asked May 09 '14 15:05

DazDylz


People also ask

How many RGB color combinations are there?

Each color channel is expressed from 0 (least saturated) to 255 (most saturated). This means that 16,777,216 different colors can be represented in the RGB color space.

What color that has a value of 255 0 0?

255, 0, 0 is red. 0, 255, 0 is green.


1 Answers

If you want to iterate through all 16,777,216 possible 24-bit RGB colours, this can be achieved quite simply with one loop:

for( i=0; i < 1<<24; i++) {
    r = (i>>16) & 0xff;
    g = (i>>8) & 0xff;
    b = i & 0xff;
    colour = "rgb("+r+","+g+","+b+")";
}

In your code, at an interval of 100, this will take almost 20 days to run through a single cycle.

If you're okay with fewer colours, try this:

for( i=0; i < 1<<12; i++) {
    r = ((i>>8) & 0xf) * 0x11;
    g = ((i>>4) & 0xf) * 0x11;
    b = (i & 0xf) * 0x11;
    colour = "rgb("+r+","+g+","+b+")";
}

This will basically reduce your colour range to 4 bits per channel, giving you #000000, #000011, #000022 and so on. Rutime at 100ms interval will be 41 seconds, and span 4,096 colours.

like image 102
Niet the Dark Absol Avatar answered Oct 19 '22 05:10

Niet the Dark Absol