How to convert colors in Python: A Comprehensive Guide to RGB, HSL, HWB, CMYK, and HEX
Converting colors in Python is a fascinating mix of dictionary lookups (for names like "tomato") and coordinate geometry. While we can use the built-in colorsys module for some parts, we'll need the webcolors library to handle CSS names and some custom math to reach the more "exotic" formats like HWB and CMYK.
π¨ Understanding the Color Modelsβ
Before we code, it helps to visualize how these models differ. RGB is how your monitor thinks (light), CMYK is how your printer thinks (ink), and HSL/HWB are how humans think (perceptual).
π» The Implementationβ
This tool uses a "Hub and Spoke" architecture: we convert any input into a standard RGB format first, then translate that RGB into all other formats.
1. Requirementsβ
pip install webcolors
2. The Codeβ
I have bundled the logic into a single, copy-pasteable script.
# π Python: The Ultimate All-in-One Color Converter
This tool handles CSS names, HEX, and standardizes everything into a unified report.
```python
import colorsys
import webcolors
def rgb_to_cmyk(r, g, b):
# Scale RGB to 0-1
r_prime, g_prime, b_prime = r/255, g/255, b/255
k = 1 - max(r_prime, g_prime, b_prime)
if k == 1:
return 0, 0, 0, 100
c = (1 - r_prime - k) / (1 - k)
m = (1 - g_prime - k) / (1 - k)
y = (1 - b_prime - k) / (1 - k)
return round(c*100), round(m*100), round(y*100), round(k*100)
def rgb_to_hwb(r, g, b):
# HWB: Hue, Whiteness, Blackness
r_p, g_p, b_p = r/255, g/255, b/255
h, s, l = colorsys.rgb_to_hls(r_p, g_p, b_p)
w = min(r_p, g_p, b_p)
b_val = 1 - max(r_p, g_p, b_p)
return round(h*360), round(w*100), round(b_val*100)
def convert_color(color_input):
try:
# 1. Normalize Input to RGB
if color_input.startswith('#'):
rgb = webcolors.hex_to_rgb(color_input)
else:
# Try to catch CSS names like 'tomato' or 'steelblue'
rgb = webcolors.name_to_rgb(color_input)
r, g, b = rgb.red, rgb.green, rgb.blue
r_p, g_p, b_p = r/255, g/255, b/255
# 2. Perform Conversions
hex_val = webcolors.rgb_to_hex((r, g, b))
h, l, s = colorsys.rgb_to_hls(r_p, g_p, b_p)
c, m, y, k = rgb_to_cmyk(r, g, b)
h_hwb, w_hwb, b_hwb = rgb_to_hwb(r, g, b)
# 3. Print Report
print(f"--- π¨ Color Report for: {color_input} ---")
print(f"HEX : {hex_val.upper()}")
print(f"RGB : rgb({r}, {g}, {b})")
print(f"HSL : {round(h*360)}Β°, {round(s*100)}%, {round(l*100)}%")
print(f"HWB : {h_hwb}Β°, {w_hwb}%, {b_hwb}%")
print(f"CMYK : {c}%, {m}%, {y}%, {k}%")
except ValueError:
print(f"β Error: '{color_input}' is not a valid CSS name or HEX code.")
# --- Test it ---
convert_color("tomato")
print("\n")
convert_color("#4287f5")
π The Math Behind the Magicβ
While Python handles the heavy lifting, the formulas for these conversions are quite elegant.
CMYK Conversionβ
The "Key" (Black) is calculated first by finding the "distance" of the brightest color from white:
$$K = 1 - \max(R', G', B')$$
Then, Cyan, Magenta, and Yellow are calculated relative to that black value:
$$C = \frac{1 - R' - K}{1 - K}$$
HWB (Hue, Whiteness, Blackness)β
HWB is often considered more intuitive for artists. You pick a Hue, then decide how much White to add to brighten it and how much Black to add to shade it.
π Comparison Table of Color Formatsβ
| Format | Best For... | Component Logic |
|---|---|---|
| HEX | Web Development | Base-16 shorthand for RGB. |
| RGB | Digital Screens | Additive light (Red, Green, Blue). |
| HSL | UI/UX Design | Hue (color), Saturation (vibrancy), Lightness. |
| CMYK | Print Media | Subtractive ink (Cyan, Magenta, Yellow, Key). |
| HWB | Human Intuition | Simple "mixing" of white and black paint. |
π Sources & Technical Refsβ
- [1.1] Python Docs: colorsys module - Standard library for HLS/HSV conversions.
- [2.1] W3C: CSS Color Module Level 4 - The official specification for HWB and named colors.
- [3.1] webcolors: GitHub Repository - Comprehensive mapping of CSS3 and CSS4 color names.
- [4.1] CMYK Conversion: StackOverflow Discussion - Common formulas for RGB to CMYK conversion.
