Python Glossary Русский

all Python 3colorsysfunction

colorsys.rgb_to_hls()

HLS

Converts a colour from RGB to HLS (hue, lightness, saturation); the components are fractions from 0 to 1.

Syntax

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

The order is H, L, S, not H, S, L as in CSS hsl(): unpacking h, s, l = rgb_to_hls(...) silently swaps lightness and saturation. Inputs are 0–1, and hue is a fraction of the circle, not degrees. Pure red has lightness 0.5, not 1: in HLS 1 means white.

Examples

import colorsys
h, l, s = colorsys.rgb_to_hls(0.2, 0.4, 0.6)  # порядок H, L, S
print(round(h * 360), round(l, 2), round(s, 2))  # → 210 0.4 0.5
print(colorsys.rgb_to_hls(1, 0, 0))  # → (0.0, 0.5, 1.0)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy