Hex to RGB Color Converter



Rule of hex to rgb conversion | How to convert hex to rgb | Mathematical calculation to convert hex to rgb


Here's the step-by-step mathematical calculation to convert a hexadecimal (hex) color code to RGB values:

  1. 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.

  2. If the hex color code includes the "#" symbol, remove it.

  3. 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.

  4. 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.

  5. 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.

  6. Obtain the RGB values:

    • Multiply each normalized decimal value by 255 to bring it back to the 0-255 range.

  7. 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:

  1. Remove the "#" symbol: "7E91D1"

  2. The length is 6, so each pair of characters represents a component directly.

  3. Convert each pair of characters to decimal:

    • Red (R): "7E" → Decimal: 126

    • Green (G): "91" → Decimal: 145

    • Blue (B): "D1" → Decimal: 209

  4. Normalize the decimal values:

    1. R: 126 / 255 ≈ 0.494

    2. G: 145 / 255 ≈ 0.569

    3. B: 209 / 255 ≈ 0.820

  5. 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.