colorsys module in Python

The colorsys module in Python allows bidirectional conversions of color values between RGB (Red Green Blue) and other color spaces. The three other color spaces it supports are YIQ (Luminance In-phase Quadrature), HLS (Hue Lightness Saturation), and HSV (Hue Saturation Value). All coordinates range between 0 and 1, except I and Q values in YIQ color space which can range from -1 to 1.

Available Functions

The colorsys module provides six conversion functions ?

Function Purpose Permitted Values
rgb_to_yiq Convert RGB coordinates to YIQ coordinates 0 to 1 (RGB), -1 to 1 (YIQ)
rgb_to_hls Convert RGB coordinates to HLS coordinates 0 to 1
rgb_to_hsv Convert RGB coordinates to HSV coordinates 0 to 1
yiq_to_rgb Convert YIQ coordinates to RGB coordinates -1 to 1 (YIQ), 0 to 1 (RGB)
hls_to_rgb Convert HLS coordinates to RGB coordinates 0 to 1
hsv_to_rgb Convert HSV coordinates to RGB coordinates 0 to 1

Example

Let's convert an "Electric Blue" color between different color spaces and verify the conversions ?

import colorsys as csys

# "Electric Blue" color in RGB
r, g, b = 0.47, 0.91, 1.00
print("The RGB Values for Electric Blue:", (r, g, b))

# Convert RGB to YIQ and back
y, i, q = csys.rgb_to_yiq(r, g, b)
print("YIQ", (y, i, q), "becomes", csys.yiq_to_rgb(y, i, q))

# Convert RGB to HSV and back
h, s, v = csys.rgb_to_hsv(r, g, b)
print("HSV", (h, s, v), "becomes", csys.hsv_to_rgb(h, s, v))

# Convert RGB to HLS and back
h, l, s = csys.rgb_to_hls(r, g, b)
print("HLS", (h, l, s), "becomes", csys.hls_to_rgb(h, l, s))
The RGB Values for Electric Blue: (0.47, 0.91, 1.0)
YIQ (0.7879, -0.292513, -0.06563100000000005) becomes (0.47, 0.9100000000000001, 1.0)
HSV (0.5283018867924528, 0.53, 1.0) becomes (0.47, 0.9099999999999999, 1.0)
HLS (0.5283018867924528, 0.735, 1.0) becomes (0.4700000000000001, 0.9099999999999998, 0.9999999999999999)

Understanding Color Spaces

Each color space represents colors differently ?

  • RGB: Based on red, green, and blue light intensities
  • HSV: Hue (color), Saturation (intensity), Value (brightness)
  • HLS: Hue (color), Lightness (brightness), Saturation (intensity)
  • YIQ: Used in NTSC television systems, separates luminance from color information

Practical Example

Here's how to adjust color brightness using HSV conversion ?

import colorsys

# Original color (red)
r, g, b = 1.0, 0.0, 0.0

# Convert to HSV to modify brightness
h, s, v = colorsys.rgb_to_hsv(r, g, b)
print(f"Original HSV: H={h:.2f}, S={s:.2f}, V={v:.2f}")

# Make it 50% darker
darker_v = v * 0.5
darker_rgb = colorsys.hsv_to_rgb(h, s, darker_v)
print(f"Darker RGB: {darker_rgb}")

# Make it 150% brighter (capped at 1.0)
brighter_v = min(v * 1.5, 1.0)
brighter_rgb = colorsys.hsv_to_rgb(h, s, brighter_v)
print(f"Brighter RGB: {brighter_rgb}")
Original HSV: H=0.00, S=1.00, V=1.00
Darker RGB: (0.5, 0.0, 0.0)
Brighter RGB: (1.0, 0.0, 0.0)

Conclusion

The colorsys module provides essential functions for converting between RGB and other color spaces like HSV, HLS, and YIQ. These conversions are particularly useful for color manipulation in graphics applications and image processing tasks.

Updated on: 2026-03-15T17:06:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements