On this page
colorsys
Prev: collections | Up: stdlib_module | Next: compileall
Rel: wave, gettext, locale, turtle, turtledemo
Ref: docs.python.org — colorsys
Tags: #public #python3 #stdlib
Import
import colorsys
Overview
Bidirectional color-space conversions between RGB and YIQ, HLS (Hue-Lightness-Saturation), and HSV (Hue-Saturation-Value). Reach for colorsys when generating color palettes, adjusting brightness/saturation programmatically, or converting UI/CSS colors without installing Pillow or NumPy.
Quick Start
import colorsys
# All inputs and outputs are floats in [0.0, 1.0]
h, s, v = colorsys.rgb_to_hsv(1.0, 0.0, 0.0) # pure red
assert (round(h, 2), round(s, 2), round(v, 2)) == (0.0, 1.0, 1.0)
r, g, b = colorsys.hsv_to_rgb(h, s, 0.5) # 50% darker red
assert (round(r, 2), round(g, 2), round(b, 2)) == (0.5, 0.0, 0.0)
Implementation Walkthrough
1. Generating an Evenly Spaced Hex Palette
Step around the hue wheel in HLS space (h in [0, 1)) and scale RGB channels to 0..255:
import colorsys
def hex_palette(count: int, lightness: float = 0.5, saturation: float = 0.75) -> list[str]:
colors = []
for i in range(count):
r, g, b = colorsys.hls_to_rgb(i / count, lightness, saturation)
colors.append(f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}")
return colors
palette = hex_palette(4)
assert len(palette) == 4 and all(c.startswith("#") and len(c) == 7 for c in palette)
Gotchas & Boundaries
- Normalized
[0.0, 1.0]floats, not0..255or0..360°: Divide 8-bit RGB channels by255.0and hue degrees by360.0before callingcolorsys, then scale back afterward. - HLS parameter order is
(h, l, s)—not HSL: CSS useshsl(H, S, L), whereascolorsysputs lightness before saturation (hls_to_rgb(h, l, s)). - Not perceptually uniform:
colorsysdoes not include CIE Lab, OKLCH, or sRGB gamma-transfer functions; usecolour-scienceorcoloraidefor perceptual color matching.
Common Functions
- colorsys.hls_to_rgb() —
hls_to_rgb(h, l, s)—colorsys.hls_to_rgb()function. - colorsys.hsv_to_rgb() —
hsv_to_rgb(h, s, v)—colorsys.hsv_to_rgb()function. - colorsys.rgb_to_hls() —
rgb_to_hls(r, g, b)—colorsys.rgb_to_hls()function. - colorsys.rgb_to_hsv() —
rgb_to_hsv(r, g, b)—colorsys.rgb_to_hsv()function. - colorsys.rgb_to_yiq() —
rgb_to_yiq(r, g, b)—colorsys.rgb_to_yiq()function. - colorsys.yiq_to_rgb() —
yiq_to_rgb(y, i, q)—colorsys.yiq_to_rgb()function.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/colorsys/hls_to_rgb
- tuts/python3/stdlib_module/colorsys/hsv_to_rgb
- tuts/python3/stdlib_module/colorsys/rgb_to_hls
- tuts/python3/stdlib_module/colorsys/rgb_to_hsv
- tuts/python3/stdlib_module/colorsys/rgb_to_yiq
- tuts/python3/stdlib_module/colorsys/yiq_to_rgb
- tuts/python3/stdlib_module/compileall
- tuts/python3/stdlib_module/gettext
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/locale
- tuts/python3/stdlib_module/shlex
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tkinter
- tuts/python3/stdlib_module/turtle
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/stdlib_module/wave
Related notes
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/hls_to_rgb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/hsv_to_rgb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/rgb_to_hls Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/rgb_to_hsv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/rgb_to_yiq Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/colorsys/yiq_to_rgb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/compileall Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gettext Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/locale Linked here; links to this page; shared tags: #stdlib