Here's the step-by-step mathematical calculation to convert a hexadecimal (hex) color code to RGB values:
Obtain the hex color code. It is usually represented as a 6-digit code, such as "#RRGGBB" or a 3-digit code, such as "#RGB". The characters R, G, and B represent the hexadecimal digits (0-9, A-F) for the red, green, and blue color channels, respectively.
If the hex color code includes the "#" symbol, remove it.
Check the length of the hex color code:
If it has 3 characters, it represents a shorthand notation where each character is duplicated to form a 6-digit code. For example, "#ABC" is equivalent to "#AABBCC".
If it has 6 characters, each pair of characters represents the red, green, and blue components directly.
Convert each pair of characters (or duplicated characters) to decimal values:
For each pair of characters, convert them separately from hexadecimal to decimal. For example, if you have "AA" as the red component, convert "AA" to decimal.
Normalize the decimal values:
Divide each decimal value by 255 to normalize the range to 0-1. This step ensures consistency with the RGB color model, where each component ranges from 0 to 255.
Obtain the RGB values:
Multiply each normalized decimal value by 255 to bring it back to the 0-255 range.
The resulting decimal values represent the red (R), green (G), and blue (B) components of the RGB color model, respectively.
For example, let's convert the hex color code "#7E91D1" to RGB:
Remove the "#" symbol: "7E91D1"
The length is 6, so each pair of characters represents a component directly.
Convert each pair of characters to decimal:
Red (R): "7E" → Decimal: 126
Green (G): "91" → Decimal: 145
Blue (B): "D1" → Decimal: 209
Normalize the decimal values:
R: 126 / 255 ≈ 0.494
G: 145 / 255 ≈ 0.569
B: 209 / 255 ≈ 0.820
Obtain the RGB values:
R: 0.494 * 255 ≈ 126
G: 0.569 * 255 ≈ 145
B: 0.820 * 255 ≈ 209
Therefore, the RGB values for the hex color code "#7E91D1" are approximately R: 126, G: 145, B: 209.
By following these steps, you can convert any valid hex color code to its corresponding RGB values using mathematical calculations.