Color converter
Convert between HEX, RGB and HSL, and check how readable the color is with text.
This tool runs in your browser. We never send what you type or paste to a server.
Contrast and accessibility
WCAG 2 asks for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (24px, or 18.66px bold) at level AA. Level AAA raises these to 7:1 and 4.5:1. The checker shows the ratio of your color against white and black text.
Formats
#3a7bd5or short#37d: hexadecimal red, green, blue.rgb(58 123 213): the same values from 0 to 255.hsl(215 65% 53%): hue, saturation, lightness, easiest for making tints and shades.
Example
#3a7bd5 = rgb(58 123 213) = hsl(215 65% 53%)
Transparency is kept: #3a7bd580 becomes rgb(58 123 213 / 0.5). The contrast check shows whether white or black text on the colour meets WCAG AA (4.5:1 for normal text).
How the conversions work
HEX to RGB is direct: the six digits are three bytes, red, green and blue, each from 00 to FF (0 to 255). A three-digit form such as #37d doubles each digit, so it means #3377dd. An optional fourth or eighth digit is the alpha channel; in #3a7bd580 the 80 is 128 of 255, roughly half opaque.
RGB to HSL takes the largest and smallest of the three channels. Lightness is their average, saturation is how far apart they are relative to that lightness, and hue is the angle on the colour wheel decided by which channel is largest (red at 0°, green at 120°, blue at 240°). For #3a7bd5 blue is the largest and green sits between, which gives a hue of 215°.
The converter rounds HSL to whole numbers, and that loses a little. Converting hsl(215 65% 53%) back gives rgb(57 122 213), not 58 123 213. Keep the HEX or RGB value as the source of truth and treat HSL as a way of adjusting it.
Working out a contrast ratio
The contrast figures follow WCAG 2. Each channel is turned into linear light (small values are divided by 12.92, larger ones raised to the power 2.4), then weighted 0.2126 red, 0.7152 green and 0.0722 blue to give relative luminance, a value from 0 for black to 1 for white. The ratio is (lighter + 0.05) ÷ (darker + 0.05), so it runs from 1:1 to 21:1.
def luminance(r, g, b):
def f(c):
c /= 255
return c / 12.92 if c <= 0.03928 else ((c + 0.055) / 1.055) ** 2.4
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)
def contrast(a, b):
hi, lo = sorted((luminance(*a), luminance(*b)), reverse=True)
return (hi + 0.05) / (lo + 0.05)
print(round(contrast((58, 123, 213), (255, 255, 255)), 2)) # → 4.22 fails AA for body text
print(round(contrast((44, 100, 180), (255, 255, 255)), 2)) # → 5.85 a darker shade passes
print(round(contrast((118, 118, 118), (255, 255, 255)), 2)) # → 4.54 #767676, the lightest grey that passes on white
So the example blue, #3a7bd5, gets 4.22:1 with white text: fine for large headings, which the checker marks "AA large", but short of 4.5:1 for body text. Darkening it slightly fixes that. Green counts for most of the luminance, which is why a pure green background with white text fails badly while pure blue passes.
Converting in code
const hexToRgb = (hex) => hex.replace('#', '').match(/../g).map((x) => parseInt(x, 16));
const rgbToHex = (r, g, b) => '#' + [r, g, b].map((x) => x.toString(16).padStart(2, '0')).join('');
console.log(hexToRgb('#3a7bd5').join(' ')); // → 58 123 213
console.log(rgbToHex(58, 123, 213)); // → #3a7bd5
console.log(rgbToHex(5, 10, 15)); // → #050a0f without padStart this would be #5af
console.log(Math.round(0x80 / 255 * 100) / 100); // → 0.5 alpha byte 80 in #3a7bd580
Python's standard library has colorsys, which works with values from 0 to 1 and names its function rgb_to_hls, returning hue, lightness and saturation in that order, not the HSL order CSS uses:
h = '3a7bd5'
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
print(r, g, b) # → 58 123 213
print('#{:02x}{:02x}{:02x}'.format(58, 123, 213)) # → #3a7bd5
hue, light, sat = colorsys.rgb_to_hls(r / 255, g / 255, b / 255) # note the order: H, L, S
print(round(hue * 360), round(sat * 100), round(light * 100)) # → 215 65 53
Watch the alpha position when moving between platforms: CSS and this converter use #RRGGBBAA, with alpha last, while Android colour resources and some design tools put it first, as #AARRGGBB. The same eight digits are a different colour in each.
Questions
How do I convert HEX to RGB?
Split the six digits into three pairs and convert each from hexadecimal: #3A7BD5 → 3A = 58, 7B = 123, D5 = 213.
What contrast ratio do I need?
4.5:1 for body text and 3:1 for large text to meet WCAG AA.
Which format should I use in CSS?
Any of them works in every current browser. HEX is the most compact, and HSL is the easiest to adjust by hand, because you can change lightness without touching the hue.
How do I convert RGB to HEX?
Write each of the three values from 0 to 255 as two hexadecimal digits, padding with a leading zero, and join them after a #. rgb(5, 10, 15) becomes 05, 0A and 0F, so #050a0f. Forgetting the padding gives a wrong, shorter code.
Why does the contrast checker ignore transparency?
The contrast of a semi-transparent colour depends on what is behind it, which the converter cannot know. It works out the ratio as if the colour were fully opaque and shows a note when the alpha is below 1. Blend the colour with your real background first for an exact figure.
What is the difference between HSL and HSV?
Both use a hue angle, but HSL puts pure colours at 50% lightness, with 100% always white, while HSV (also called HSB) puts pure colours at 100% value. CSS supports hsl() but not HSV, which mostly appears in colour pickers in design tools.