Python Glossary Русский

all Python 3colorsysfunction

colorsys.rgb_to_hsv()

HSV

Converts a colour from RGB to HSV (hue, saturation, value); every component is a fraction from 0 to 1, not 0–255.

Syntax

h, s, v = colorsys.rgb_to_hsv(r, g, b)

The function does not reject 0–255 values, it silently returns nonsense: rgb_to_hsv(255, 0, 0) gives (0.0, 1.0, 255). Divide by 255 before the call. Hue is a fraction of the circle too, not degrees: 0.5 means 180°, so multiply h by 360 for the usual notation.

Examples

import colorsys
h, s, v = colorsys.rgb_to_hsv(0.2, 0.4, 0.6)
print(round(h * 360), round(s, 2), v)  # → 210 0.67 0.6
print(colorsys.rgb_to_hsv(255, 0, 0))  # → (0.0, 1.0, 255) — забыли поделить на 255

See also

Open in the full glossarydocs.python.orgReport an inaccuracy