RGB and Hex values


#1

This lesson says " In both hex and decimal, we have three values, one for each color. Each can be one of 256 values. Specifically, 256 * 256 * 256 = 16,777,216 ." If each can only be one of 256 values, why one of them is 777?

https://www.codecademy.com/courses/learn-css-colors/lessons/color/exercises/color-entire-palette?action=resume_content_item&course_redirect=learn-css


#2

256^3 = 16,777,216
aka how many unique values you can produce


#3

Think in terms of 24-bit color…

2 ** 24 - 1  =>  16777215

The only one missing is black (rgb(0, 0, 0)).

That’s the thousands comma separation, not three separate values. The number is 16777216 when the commas are removed.

Many colors, but only three palettes, Red, Green, and Blue.

rgb( %(0..255) << 16%, %(0..255) << 8%, %(0..255)% )

#000000 .. #FFFFFF

#000 .. #fff

#4

Thank you for your help


#5

thank you, that makes sense


#6

In the above example I used placeholders with a hint of their placement in the hex notation. In the full color spectrum anything in the red pallete will have a value in the range of 1 through 255, but it will be left shifted 16 bits. Green will be left shifted 8-bits.

255 << 16    =>  16711680  =>  #FF0000  =>  #f00
1 << 16      =>  65536     =>  #010000  =>  no shorthand
255 << 8     =>  65280     =>  #00FF00  =>  #0f0
1 << 8       =>  256       =>  #000100  =>  no shorthand
255          =>  255       =>  #0000FF  =>  #00f
1            =>  1         =>  #000001  =>  no shorthand

16711680 + 65280 + 255  =>  16777215

plus black.

Why, ‘plus black’? Simple, there is no other way to represent 0 except as 0.

#000000 or #000

are valid because the API makes them valid. They are not actual numbers, though. They are 0.

0x0    => Does hex have a zero that is different from `0`? No.

0o0    => Does octal have a zero that is different from `0`? No.

0b0    => Does binary have a zero that is different from `0`? No.

They’re all just 0. From a named color point of view, this is one color we may accept as the defacto regardless the scheme, black.

>>> 0o10 == 10
False
>>> 0o10 == 8
True
>>> 0x0 == 0
True
>>> 0o0 == 0
True
>>> 0b0 == 0
True
>>> 

The above is not JavaScript output, but the logic is the same.