Browse the docs

parseColor

Reads a colour into a record with hex, r, g, b, a, h, s and l.

Details #

parseColor(text) takes one argument and returns a record:

  • hex: lower-case #rrggbb, or #rrggbbaa when the colour is see-through;

  • r, g, b: 0 to 255;

  • a: 0 to 1;

  • h (0 to 360), s and l (0 to 100).

It reads #rgb, #rgba, #rrggbb, #rrggbbaa (the # is optional), rgb() and rgba() with commas or spaces, / alpha, percent channels, hsl() and hsla() the same way, transparent, and the 148 CSS colour names such as tomato. Capitals don't matter.

Parameters #

Takes 1 argument. {{ parseColor(text) }}

Examples #

parseColor("tomato").hex
→ #ff6347
parseColor("rgb(255 128 0 / 50%)").a
→ 0.5
parseColor("hsl(120, 100%, 50%)").hex
→ #00ff00

Gotchas #

  • An unknown colour is an error: parseColor() doesn't know the colour 'plaid'.