Conversions between color systems using Python (colorsys)

The RGB color model uses red, green, and blue light to reproduce various colors. While RGB is widely used in electronic displays, Python's colorsys module provides functions to convert between RGB and other color systems like YIQ, HLS, and HSV.

Alternative color representations include ?

YIQ: Luminance and Chrominance (used by composite video signals)

HLS: Hue, Luminance, Saturation

HSV: Hue, Saturation, Value

In the YIQ model, Y values range from 0 to 1, while I and Q values may be positive or negative. In RGB, HLS, and HSV models, all values are between 0 and 1.

Available Functions

The colorsys module provides conversion functions between RGB and each color system ?

Function Description
rgb_to_yiq() Convert RGB to YIQ
yiq_to_rgb() Convert YIQ to RGB
rgb_to_hls() Convert RGB to HLS
hls_to_rgb() Convert HLS to RGB
rgb_to_hsv() Convert RGB to HSV
hsv_to_rgb() Convert HSV to RGB

Converting RGB to Other Color Systems

Here's how to convert RGB values to different color systems using colorsys ?

import colorsys

# RGB values (normalized between 0 and 1)
r, g, b = 1, 0.753, 0.80

# Convert to HLS
hls = colorsys.rgb_to_hls(r, g, b)
print("RGB to HLS:", hls)

# Convert to HSV
hsv = colorsys.rgb_to_hsv(r, g, b)
print("RGB to HSV:", hsv)

# Convert to YIQ
yiq = colorsys.rgb_to_yiq(r, g, b)
print("RGB to YIQ:", yiq)
RGB to HLS: (0.9682860998650472, 0.8765000000000001, 1.0)
RGB to HSV: (0.9682860998650472, 0.247, 1)
RGB to YIQ: (0.83227, 0.1328331, 0.06727970000000007)

Converting Back to RGB

You can also convert from other color systems back to RGB ?

import colorsys

# Convert HLS back to RGB
h, l, s = 0.9682860998650472, 0.8765000000000001, 1.0
rgb_from_hls = colorsys.hls_to_rgb(h, l, s)
print("HLS to RGB:", rgb_from_hls)

# Convert HSV back to RGB
h, s, v = 0.9682860998650472, 0.247, 1
rgb_from_hsv = colorsys.hsv_to_rgb(h, s, v)
print("HSV to RGB:", rgb_from_hsv)

# Convert YIQ back to RGB
y, i, q = 0.83227, 0.1328331, 0.06727970000000007
rgb_from_yiq = colorsys.yiq_to_rgb(y, i, q)
print("YIQ to RGB:", rgb_from_yiq)
HLS to RGB: (1.0000000000000002, 0.7530000000000001, 0.8)
HSV to RGB: (1.0, 0.753, 0.8000000000000002)
YIQ to RGB: (1.0000000000000002, 0.7529999999999999, 0.8000000000000002)

Practical Example with 8-bit Colors

When working with typical 8-bit RGB values (0-255), you need to normalize them first ?

import colorsys

# 8-bit RGB values (0-255)
rgb_8bit = (255, 192, 204)

# Normalize to 0-1 range
r = rgb_8bit[0] / 255
g = rgb_8bit[1] / 255  
b = rgb_8bit[2] / 255

print(f"Normalized RGB: ({r:.3f}, {g:.3f}, {b:.3f})")

# Convert to HSV
h, s, v = colorsys.rgb_to_hsv(r, g, b)
print(f"HSV: ({h:.3f}, {s:.3f}, {v:.3f})")

# Convert back and scale to 8-bit
rgb_back = colorsys.hsv_to_rgb(h, s, v)
rgb_8bit_back = tuple(int(x * 255) for x in rgb_back)
print(f"Back to 8-bit RGB: {rgb_8bit_back}")
Normalized RGB: (1.000, 0.753, 0.800)
HSV: (0.968, 0.247, 1.000)
Back to 8-bit RGB: (255, 192, 204)

Conclusion

The colorsys module provides essential color conversion functions between RGB and YIQ, HLS, and HSV color systems. Remember to normalize RGB values to the 0-1 range before conversion, and scale back when working with 8-bit colors.

Updated on: 2026-03-25T05:26:13+05:30

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements