D
Documentation
v1.0.0
Color Conversion
Colorium provides seamless conversion between all supported color spaces. This guide covers every conversion method available.
Overview of Color Spaces
Colorium supports conversion between these color spaces:
| From | To | Method |
|---|---|---|
| Any | RGB | to_rgb() |
| Any | HSL | to_hsl() |
| Any | HWB | to_hwb() |
| Any | CMYK | to_cmyk() |
| Any | OKLCH | to_oklch() |
| Any | LAB | to_lab() |
| Any | LCH | to_lch() |
| Any | P3 | to_p3() |
String Conversions
Basic String Formats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from colorium import Color
color = Color(255, 0, 0)
# RGB string
print(color.to_rgb_string()) # rgb(255, 0, 0)
print(color.to_rgba_string()) # rgba(255, 0, 0, 1.00)
# HSL string
print(color.to_hsl_string()) # hsl(0, 100%, 50%)
print(color.to_hsla_string()) # hsla(0, 100%, 50%, 1.00)
# HWB string
print(color.to_hwb_string()) # hwb(0, 0%, 0%)
print(color.to_hwba_string()) # hwba(0, 0%, 0%, 1.00)
# CMYK string
print(color.to_cmyk_string()) # cmyk(0%, 100%, 100%, 0%)
# Hex string
print(color.to_hex_string()) # #FF0000Modern CSS Color Level 4 Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from colorium import Color
color = Color(255, 0, 0)
# OKLCH string
print(color.to_oklch_string()) # oklch(62.8% 0.258 29.2)
# LAB string
print(color.to_lab_string()) # lab(53.2% 80.1 67.2)
# LCH string
print(color.to_lch_string()) # lch(53.2% 104.5 40.0)
# P3 string
print(color.to_p3_string()) # color(display-p3 0.823 0.033 0.017)NCOL String
1
2
3
4
from colorium import Color
color = Color(255, 0, 0)
print(color.to_ncol_string()) # ncol(R0, 0%, 0%)Dictionary Conversions
RGB Dictionary
1
2
3
4
5
6
7
8
9
from colorium import Color
color = Color(100, 150, 200)
rgb = color.to_rgb()
print(rgb['r']) # 100
print(rgb['g']) # 150
print(rgb['b']) # 200
print(rgb['a']) # 1.0HSL Dictionary
1
2
3
4
5
6
7
8
9
from colorium import Color
color = Color(255, 0, 0)
hsl = color.to_hsl()
print(hsl['h']) # 0
print(hsl['s']) # 1.0
print(hsl['l']) # 0.5
print(hsl['a']) # 1.0HWB Dictionary
1
2
3
4
5
6
7
8
9
from colorium import Color
color = Color(100, 150, 200)
hwb = color.to_hwb()
print(hwb['h']) # Hue (0-360)
print(hwb['w']) # Whiteness (0-1)
print(hwb['b']) # Blackness (0-1)
print(hwb['a']) # Opacity (0-1)CMYK Dictionary
1
2
3
4
5
6
7
8
9
10
from colorium import Color
color = Color(100, 150, 200)
cmyk = color.to_cmyk()
print(cmyk['c']) # 0.5
print(cmyk['m']) # 0.25
print(cmyk['y']) # 0.0
print(cmyk['k']) # 0.22
print(cmyk['a']) # 1.0OKLCH Dictionary
1
2
3
4
5
6
7
8
from colorium import Color
color = Color(100, 150, 200)
oklch = color.to_oklch()
print(oklch['l']) # Lightness (0-1)
print(oklch['c']) # Chroma (0-0.4+)
print(oklch['h']) # Hue (0-360)LAB Dictionary
1
2
3
4
5
6
7
8
from colorium import Color
color = Color(100, 150, 200)
lab = color.to_lab()
print(lab['l']) # Lightness (0-100)
print(lab['a']) # a* (-128 to 128)
print(lab['b']) # b* (-128 to 128)LCH Dictionary
1
2
3
4
5
6
7
8
from colorium import Color
color = Color(100, 150, 200)
lch = color.to_lch()
print(lch['l']) # Lightness (0-100)
print(lch['c']) # Chroma (0-100+)
print(lch['h']) # Hue (0-360)P3 Dictionary
1
2
3
4
5
6
7
8
from colorium import Color
color = Color(100, 150, 200)
p3 = color.to_p3()
print(p3['r']) # Red (0-1)
print(p3['g']) # Green (0-1)
print(p3['b']) # Blue (0-1)Converting Between Spaces
Direct Conversion Methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from colorium import Color
color = Color(255, 0, 0)
# To RGB
rgb = color.to_rgb()
# To HSL
hsl = color.to_hsl()
# To HWB
hwb = color.to_hwb()
# To CMYK
cmyk = color.to_cmyk()
# To OKLCH
oklch = color.to_oklch()
# To LAB
lab = color.to_lab()
# To LCH
lch = color.to_lch()
# To P3
p3 = color.to_p3()Creating from Different Spaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from colorium import from_hsl, from_hwb, from_cmyk
from colorium import from_oklch, from_lab, from_lch, from_p3
# From HSL
hsl_color = from_hsl(200, 0.8, 0.5)
# From HWB
hwb_color = from_hwb(180, 0.2, 0.1)
# From CMYK
cmyk_color = from_cmyk(0.2, 0.8, 0.1, 0.3)
# From OKLCH
oklch_color = from_oklch(0.5, 0.2, 180)
# From LAB
lab_color = from_lab(50, 20, -30)
# From LCH
lch_color = from_lch(50, 30, 180)
# From P3
p3_color = from_p3(0.8, 0.3, 0.5)Round-Trip Conversion
Colorium ensures accurate round-trip conversions.
1
2
3
4
5
6
7
8
9
10
11
12
13
from colorium import Color, from_hsl
# Create a color
original = Color(100, 150, 200)
# Convert to HSL and back
hsl = original.to_hsl()
recreated = from_hsl(hsl['h'], hsl['s'], hsl['l'])
# Check accuracy
print(f"Original: {original.to_hex_string()}")
print(f"Recreated: {recreated.to_hex_string()}")
print(f"Match: {original == recreated}") # TrueConversion Accuracy
Precision
1
2
3
4
5
6
7
8
9
10
from colorium import Color
color = Color(100, 150, 200)
# Values are rounded to reasonable precision
hsl = color.to_hsl()
print(hsl['s']) # 0.48 (rounded to 2 decimals)
oklch = color.to_oklch()
print(oklch['l']) # 0.567 (rounded to 3 decimals)Tolerance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from colorium import Color
# Small differences are expected due to rounding
color1 = Color(100, 150, 200)
color2 = Color(101, 149, 201)
# Use tolerance for comparison
def colors_close(c1, c2, tolerance=2):
return (
abs(c1.red - c2.red) <= tolerance and
abs(c1.green - c2.green) <= tolerance and
abs(c1.blue - c2.blue) <= tolerance
)
print(colors_close(color1, color2)) # TrueConversion Performance
Caching
1
2
3
4
5
6
7
8
9
10
11
from colorium import Color
color = Color(100, 150, 200)
# Good - values are cached internally
hsl1 = color.to_hsl()
hsl2 = color.to_hsl() # Fast - uses cache
# Bad - unnecessary conversion
rgb1 = color.to_rgb()
rgb2 = color.to_rgb() # Still fast due to cacheBatch Conversion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from colorium import Color
def batch_convert_to_hsl(colors):
"""Convert multiple colors to HSL"""
return [color.to_hsl() for color in colors]
colors = [
Color(100, 150, 200),
Color(255, 0, 0),
Color(0, 255, 0)
]
hsl_values = batch_convert_to_hsl(colors)
for hsl in hsl_values:
print(f"H: {hsl['h']}, S: {hsl['s']}, L: {hsl['l']}")Common Conversion Patterns
RGB to Hex
1
2
3
4
5
from colorium import Color
color = Color(255, 0, 0)
hex_string = color.to_hex_string()
print(hex_string) # #FF0000Hex to RGB
1
2
3
4
from colorium import from_hex
color = from_hex("#FF0000")
print(color.to_rgb()) # {'r': 255, 'g': 0, 'b': 0}HSL to RGB
1
2
3
4
5
from colorium import from_hsl
color = from_hsl(200, 0.8, 0.5)
rgb = color.to_rgb()
print(rgb) # {'r': 50, 'g': 150, 'b': 200}RGB to CMYK
1
2
3
4
5
from colorium import Color
color = Color(255, 0, 0)
cmyk = color.to_cmyk()
print(cmyk) # {'c': 0.0, 'm': 1.0, 'y': 1.0, 'k': 0.0}LAB to RGB
1
2
3
4
5
from colorium import from_lab
color = from_lab(50, 20, -30)
rgb = color.to_rgb()
print(rgb) # {'r': 100, 'g': 150, 'b': 200}Conversion Reference Table
| Method | Returns | Format |
|---|---|---|
to_rgb() | Dict | {'r': 0-255, 'g': 0-255, 'b': 0-255, 'a': 0-1} |
to_hsl() | Dict | {'h': 0-360, 's': 0-1, 'l': 0-1, 'a': 0-1} |
to_hwb() | Dict | {'h': 0-360, 'w': 0-1, 'b': 0-1, 'a': 0-1} |
to_cmyk() | Dict | {'c': 0-1, 'm': 0-1, 'y': 0-1, 'k': 0-1, 'a': 0-1} |
to_oklch() | Dict | {'l': 0-1, 'c': 0-0.4+, 'h': 0-360} |
to_lab() | Dict | {'l': 0-100, 'a': -128-128, 'b': -128-128} |
to_lch() | Dict | {'l': 0-100, 'c': 0-100+, 'h': 0-360} |
to_p3() | Dict | {'r': 0-1, 'g': 0-1, 'b': 0-1} |
Next Steps
- Color Filters — Apply filters to colors
- Color Blending — Advanced blending techniques
- Color Distance — Color difference calculations
Previous: Color Manipulation Next: Color Filters →
On this page
32 sections