colorsys converts colors between RGB and the YIQ, HLS, and HSV models. It is a compact option for targeted calculations when code normalizes channels and documents conventions.
Convert RGB to HSV
import colorsys
rgb_255 = (30, 144, 255)
rgb = tuple(channel / 255 for channel in rgb_255)
h, s, v = colorsys.rgb_to_hsv(*rgb)
restored = colorsys.hsv_to_rgb(h, s, v)
assert all(abs(a - b) < 1e-12 for a, b in zip(rgb, restored))
Normalize at the boundary
Keep calculations as floats between 0 and 1, converting to integers only when producing CSS, an image, or a protocol that requires another scale. Rounding at every step accumulates error.
Confirm names and models
The API uses HLS, with lightness as its second component, while many tools advertise HSL. Verify channel order, ranges, and meanings at integration boundaries. colorsys does not perform ICC profile management or advanced perceptual conversion.
Keep learning
Continue with precise calculations with Decimal. The official Python documentation, accessed July 22, 2026, describes the conversion functions and mathematical references.