color — convert a colour between hex, RGB, HSL and CSS names
SYNOPSIS
color <colour> color (read the colour from piped input)
DESCRIPTION
Reads a colour in any common CSS notation and prints it in all of them — hexadecimal, rgb() and hsl() — next to a swatch, followed by its WCAG contrast ratio against white and against black.
Accepted notations:
#f80, #ff8800 hexadecimal, 3 or 6 digits (the # is optional)
#f80c, #ff8800cc hexadecimal with alpha, 4 or 8 digits
rgb(255, 136, 0) also rgb(255 136 0 / 50%) and rgba(…)
hsl(32, 100%, 50%) also hsl(32deg 100% 50% / 0.5) and hsla(…)
tomato, rebeccapurple any CSS colour name, and transparent
HOW IT WORKS
The three notations describe the same sRGB colour. Hex and rgb() give the red, green and blue channels (0-255); hsl() gives a hue (an angle on the colour wheel: 0 red, 120 green, 240 blue), a saturation and a lightness, which is easier to adjust by hand. Hex, rgb() and hsl() are converted with plain arithmetic; names are resolved by your browser's own CSS engine, so every name it knows is accepted.
The contrast ratio is the one defined by the WCAG accessibility guidelines: it compares the relative luminance of two colours and ranges from 1:1 (identical) to 21:1 (black on white). Text needs at least 4.5:1 to reach level AA (3:1 for large text) and 7:1 for level AAA. The ratio tells whether white or black text is the readable choice on that colour. Transparency is ignored for contrast: the ratio is the opaque colour's.
Everything is computed in your browser.
USE CASES
- turn the hex code of a mock-up into rgb() or hsl() for a stylesheet;
- find the hex value of a CSS colour name;
- decide between white and black text on a brand colour;
- check that a text/background pair meets WCAG AA before shipping.
NOTES
hsl() values are rounded to one decimal, so converting back and forth may move a channel by one. Wide-gamut colours (display-p3, lab, oklch) are outside the sRGB notations handled here.
EXAMPLES
color #ff8800 color f80 color "rgb(255, 136, 0)" color "hsl(210 50% 40% / 0.8)" color rebeccapurple