Hex to RGB Converter Formula
Understand the math behind the hex to rgb converter. Each variable explained with a worked example.
Formulas Used
Red (R)
red = floor(hex_value / 65536)Green (G)
green = floor((hex_value - floor(hex_value / 65536) * 65536) / 256)Blue (B)
blue = hex_value - floor(hex_value / 256) * 256Variables
| Variable | Description | Default |
|---|---|---|
hex_value | Hex Value (numeric, e.g. 3899126 for #3B82F6) | 3899126 |
How It Works
Hex to RGB Conversion
A hex color like #3B82F6 has a numeric value of 3,899,126.
Extracting Components
Example: 3899126
Worked Example
Convert hex value 3899126 (#3B82F6) to RGB.
hex_value = 3899126
- 01R = floor(3899126 / 65536) = 59
- 02G = floor((3899126 mod 65536) / 256) = 130
- 03B = 3899126 mod 256 = 246
- 04Result: rgb(59, 130, 246)
Ready to run the numbers?
Open Hex to RGB Converter