Skip to content

dev.colors

make, convert, blend and adjust colors, and check text is readable on a background

Colors are strings in the hex form web pages use, "#rrggbb": two hex digits each for how much red, green and blue there is, from 00 to ff.

Every function here that takes a color also accepts a CSS name like “tomato”, the short form “#f80”, or a list of three numbers from 0 to 255.

Tags: conversion

  • color a color in “#rrggbb” form, from a CSS name, “#rgb”, “#rrggbb” or a list [r, g, b]
  • rgb make a color from red, green and blue amounts (0-255), or give one argument to get a color’s three amounts
  • hsl make a color from hue, saturation and lightness, or give one argument to get a color’s three values
  • mix blend two colors, where t is how far to go from a toward b, 0 to 1
  • lighten make a color lighter, keeping its hue
  • darken make a color darker, keeping its hue
  • invert for a color, the opposite color on the color wheel, like white for black
  • grayscale the gray that looks as bright as the color does
  • contrast how readable one color is on the other, as the WCAG contrast ratio from 1 (identical) to 21 (black on white)
  • swatch a block of the color followed by its hex code, to preview it
color(c: str|list) -> str

a color in “#rrggbb” form, from a CSS name, “#rgb”, “#rrggbb” or a list [r, g, b]

1
color("tomato")"#ff6347"
2
color("#f80")"#ff8800"
3
color([255, 0, 128])"#ff0080"
Example outputCtrl+Enter to run

See also rgb, hsl

rgb(r: num, g: num, b: num) -> str / rgb(c: str|list) -> list

make a color from red, green and blue amounts (0-255), or give one argument to get a color’s three amounts

1
rgb(255, 136, 0)"#ff8800"
2
"orange".rgb[255, 165, 0]
Example outputCtrl+Enter to run

See also hsl, color

hsl(h: num, s: num, l: num) -> str / hsl(c: str|list) -> list

make a color from hue, saturation and lightness, or give one argument to get a color’s three values

Hue is the position on the color wheel in degrees (0 red, 120 green, 240 blue), saturation how vivid it is and lightness how close to white, both from 0 to 100

1
hsl(210, 80, 50)"#1980e6"
2
"teal".hsl[180, 100, 25]
Example outputCtrl+Enter to run

See also rgb, color

mix(a: str|list, b: str|list, t: num = 0.5) -> str

blend two colors, where t is how far to go from a toward b, 0 to 1

1
mix("red", "blue")"#800080"
2
mix("white", "black", 25%)"#bfbfbf"
Example outputCtrl+Enter to run

See also lighten, darken

lighten(c: str|list, amount: num) -> str

make a color lighter, keeping its hue

amount, like 20%, is added to its HSL lightness

1
lighten("#3478f6", 20%)"#96b9fa"
Example outputCtrl+Enter to run

See also darken, mix

darken(c: str|list, amount: num) -> str

make a color darker, keeping its hue

amount, like 20%, is taken off its HSL lightness

1
darken("#3478f6", 20%)"#0847bc"
Example outputCtrl+Enter to run

See also lighten, mix

invert(c: str|list) -> str

for a color, the opposite color on the color wheel, like white for black

1
invert("navy")"#ffff7f"
Example outputCtrl+Enter to run

See also grayscale

grayscale(c: str|list) -> str

the gray that looks as bright as the color does

1
grayscale("tomato")"#969696"
Example outputCtrl+Enter to run

See also invert

contrast(a: str|list, b: str|list) -> float

how readable one color is on the other, as the WCAG contrast ratio from 1 (identical) to 21 (black on white)

Body text needs 4.5 or more and large text 3

1
contrast("white", "black")21
2
contrast("#777", "white")4.48
Example outputCtrl+Enter to run

See also swatch

swatch(c: str|list) -> str

a block of the color followed by its hex code, to preview it

Needs a terminal with full (24-bit) color

1
swatch("tomato") #ff6347
2
["red", "gold", "teal"].map(swatch).join(" ") #ff0000 #ffd700 #008080
Example outputCtrl+Enter to run

See also color

1
# named to hex
2
color("rebeccapurple")"#663399"
3
# halfway between
4
mix("red", "blue")"#800080"
5
# a darker shade for a hovered button
6
darken("#3478f6", 10%)"#0a5aed"
7
# is white text readable on gray?
8
contrast("white", "#777")4.48
9
# five hues evenly around the wheel
10
(0..5).map(|i| hsl(i * 72, 70, 50))["#d92626", "#b5d926", "#26d96e", "#266ed9", "#b526d9"]
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.