Question:
How do I convert a Hexadecimal ( #FFFFFF
) color to RGB ( R ed, G reen, B lue)?
Answer:
It's more of a math/computational math question than programming. Here is my contribution:
The RGB code, as the name says, is composed of 3 elements: Red, Green and Blue. In this case, the format #ffffff
is a hexadecimal representation of the color code, where each two letters represent a color (in order). To 'translate' this number, we must make a base change – from hexadecimal (16) to decimal (10).
The first 10 numbers in the hexadecimal base are equal to the decimal base (0 to 9), the remaining 6 are (and their representation in decimal): A (10); B (11); C (12); D (13); E (14) and F (15).
The base conversion must be done digit by digit of each color, from right to left. For example:
#ffffff:
ff = 15 * 16 ^ 0 + 15 * 16 ^ 1 = 15 * 1 + 15 * 16 = 255
Portanto, em decimal, é (255, 255, 255)
#f5ffff
f5 = 5 * 16 ^ 0 + 15 * 16 ^ 1 = 5 * 1 + 15 * 16 = 245
ff = 15 * 16 ^ 0 + 15 * 16 ^ 1 = 15 * 1 + 15 * 16 = 255
Portanto, em decimal é (245, 255, 255)