A Hex to RGBA Converter that translates any hexadecimal colour code into every
CSS colour format you might need — rgba(), rgb(), hsl(), hsla(), an
8-digit hex, a CSS custom property and a Tailwind utility class — and displays
the WCAG accessibility contrast ratio against white and black. It accepts
3-digit, 4-digit (with alpha), 6-digit and 8-digit hex values, and lets you
override the alpha channel with a smooth opacity slider. Everything runs in
your browser; no colour data ever leaves your machine.
How it works
Hex colours encode three colour channels — red, green and blue — as pairs of base-16 digits. Each pair maps onto an integer from 0 to 255. Converting to RGBA is straightforward:
- Expand shorthand. A 3-digit
#rgbbecomes#rrggbbby doubling each digit (so#3bfexpands to#33bbff). A 4-digit#rgbaexpands similarly to 8 digits. - Parse channels. Split the 6-digit form into three 2-character slices and
convert each with
parseInt(slice, 16):
#3b82f6→ R = 59, G = 130, B = 246. - Map alpha. The slider expresses opacity as 0–100%. Divide by 100 to get the CSS alpha value (0–1). For 8-digit hex, the last two hex digits give a 0–255 integer which is divided by 255.
- Build output strings.
rgba(59, 130, 246, 0.8),hsl(217, 91%, 60%),#3b82f6cc, and so on, are assembled from the same four numbers.
The WCAG contrast ratio uses the full sRGB linearisation formula:
channels below 0.04045 are divided by 12.92; those above use
((v + 0.055) / 1.055) ^ 2.4. Luminance is the dot product
0.2126 R + 0.7152 G + 0.0722 B. The final ratio is
(L_lighter + 0.05) / (L_darker + 0.05).
Worked example
Start with Tailwind’s blue-500: #3b82f6 at 80% opacity.
| Channel | Hex pair | Decimal |
|---|---|---|
| Red | 3b | 59 |
| Green | 82 | 130 |
| Blue | f6 | 246 |
| Alpha | 80% → cc | 204/255 ≈ 0.80 |
Outputs:
- RGBA:
rgba(59, 130, 246, 0.8) - 8-digit hex:
#3b82f6cc - HSLA:
hsla(217, 91%, 60%, 0.8) - CSS variable:
--color: rgba(59, 130, 246, 0.8); - Tailwind:
bg-[#3b82f6] bg-opacity-80
Contrast at 100% opacity against white: 3.68:1 (AA Large pass); against black: 5.71:1 (AA pass).
Formula note
The alpha conversion is linear: alpha = a_255 / 255. The 8-digit hex
alpha byte is the reverse: a_255 = Math.round(alpha_0_to_1 * 255), then
encoded as two hex digits padded to at least two characters. Shorthand #rgba
follows the same doubling rule as #rgb — the digit 8 in #3b8f becomes
88 in the alpha channel, not 08.