Converting Hexadecimal to base 10

Hexadecimal is base 16. Represented by 0 – 9 – A – F

I am running into many walls here. I needed to create random values of the color RED.

Color is usually expressed in hexadecimal form. 0xFFFFFF is 255R 255G 255B

it works like so: (F*16^3)+(F*16^2)+(F*16^1)+(F*16^0)

The problem is that color is expressed as this single value for all three (or four) channels. Red, Green, Blue, and sometimes Alpha.

This just means you can’t multiply “the red color” by a random 0-1 value to get another shade of red. You will get all possible colors by multiplying 0xFFFFFF by 0-1, or 16776960 in decimal.

To get all shades of red, you have to do: (random 0-1)*255 << 16 to get your hex equivalent because the Red Channel is stored in the XX0000 value. 255 = FF in hex. FF0000 is 16711680. so thats full red.

0xF = 15

0xF0 = 240

240 + 15 = 255. there it is.

R << 16 | G << 8 | B = separate RGB 0-255 converted to Hex.

Full Red Channel : 255 << 16 | 0 | 0 = 16711680 aka 0xFF0000

Full Green Channel : 255 << 8 = 65280 aka 0x00FF00

Full Red + Green = 255 << 16 | 255 << 8 = 16776960 aka 0xFFFF00

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s