Python Glossary Русский

all Python 3colorsysfunction

colorsys.hls_to_rgb()

HLS

Converts a colour from HLS (hue, lightness, saturation) back to RGB; all values are fractions from 0 to 1.

Syntax

r, g, b = colorsys.hls_to_rgb(h, l, s)

The arguments go in the order h, l, s: CSS hsl(210, 50%, 40%) is passed as hls_to_rgb(210/360, 0.4, 0.5), swapping the last two and turning degrees and percents into fractions. The round trip produces rounding errors like 0.19999999999999996 — compare with math.isclose or round.

Examples

import colorsys
rgb = colorsys.hls_to_rgb(210 / 360, 0.4, 0.5)  # CSS hsl(210, 50%, 40%)
print(rgb)  # → (0.19999999999999996, 0.3999999999999998, 0.6000000000000001)
print([round(c * 255) for c in rgb])  # → [51, 102, 153]
print(colorsys.hls_to_rgb(0, 0.5, 0))  # → (0.5, 0.5, 0.5) — насыщенность 0 даёт серый

See also

Open in the full glossarydocs.python.orgReport an inaccuracy