Color Interpolation
When creating colormaps, the choice of color space for interpolation significantly affects how colors blend between stops. Palettize supports multiple interpolation spaces via the ColorAide library.
Why Interpolation Space Matters
Section titled “Why Interpolation Space Matters”Consider a gradient from blue to yellow:
- sRGB interpolation: May produce muddy greens in the middle
- Oklch interpolation: Produces vibrant, perceptually uniform transitions
The interpolation space determines the path colors take through color space when blending.
Available Color Spaces
Section titled “Available Color Spaces”Oklch (Default)
Section titled “Oklch (Default)”Oklch is a perceptually uniform color space based on human vision. It provides:
- Consistent perceived brightness across the gradient
- Vibrant, natural-looking color transitions
- No unexpected muddy colors
palettize show --colors "blue,yellow" --space oklchThis is the default and recommended for most use cases.
Standard RGB interpolation. Colors blend in the same space as computer displays.
palettize show --colors "blue,yellow" --space srgbCharacteristics:
- Fast computation
- May produce muddy/desaturated midpoints
- Common in web/CSS gradients
LAB (CIELAB)
Section titled “LAB (CIELAB)”A perceptually uniform color space from 1976.
palettize show --colors "blue,yellow" --space labCharacteristics:
- Good perceptual uniformity
- Better than sRGB for scientific visualization
- May produce some hue shifts
HCL (LCH)
Section titled “HCL (LCH)”Cylindrical representation of LAB, using Hue, Chroma, and Lightness.
palettize show --colors "blue,yellow" --space hclCharacteristics:
- Intuitive hue interpolation
- Good for rainbow-like gradients
- May have chroma issues at extreme values
Hue, Saturation, Lightness - a common web color model.
palettize show --colors "blue,yellow" --space hslCharacteristics:
- Intuitive for designers
- Non-uniform perceptually
- Can produce unexpected results
Comparing Interpolation Spaces
Section titled “Comparing Interpolation Spaces”Visual Comparison
Section titled “Visual Comparison”# Compare the same gradient in different spacespalettize show --colors "blue,yellow" --space srgb --height 2palettize show --colors "blue,yellow" --space oklch --height 2palettize show --colors "blue,yellow" --space lab --height 2Python Comparison
Section titled “Python Comparison”from palettize import create_colormap
colors = ["blue", "yellow"]
for space in ["srgb", "oklch", "lab", "hcl"]: cmap = create_colormap(colors=colors, interpolation_space=space) midpoint = cmap.get_color(0.5) print(f"{space}: {midpoint}")Output:
srgb: #808000oklch: #a3b852lab: #9ab356hcl: #8cb866Notice how sRGB produces a darker, muddier olive color while the perceptual spaces produce brighter, more vibrant midpoints.
Choosing a Color Space
Section titled “Choosing a Color Space”Use Oklch When:
Section titled “Use Oklch When:”- Creating scientific visualizations
- You want perceptually uniform brightness
- Working with diverging colormaps
- Default choice for most applications
Use sRGB When:
Section titled “Use sRGB When:”- Matching existing CSS/web gradients
- Performance is critical
- You specifically need RGB blending behavior
Use LAB/HCL When:
Section titled “Use LAB/HCL When:”- Porting from tools that use these spaces
- Working with color science applications
- Creating perceptually balanced palettes
Impact on Exports
Section titled “Impact on Exports”The interpolation space affects the colors sampled during export:
from palettize import create_colormap, get_scaler_by_name, get_exporter
cmap_srgb = create_colormap(colors=["blue", "yellow"], interpolation_space="srgb")cmap_oklch = create_colormap(colors=["blue", "yellow"], interpolation_space="oklch")
scaler = get_scaler_by_name("linear", domain_min=0, domain_max=1)exporter = get_exporter("hex")
# Different midpoint colorsprint(exporter.export(cmap_srgb, scaler, 0, 1, {"num_colors": 3}))print(exporter.export(cmap_oklch, scaler, 0, 1, {"num_colors": 3}))Best Practices
Section titled “Best Practices”- Start with Oklch: It’s the default for good reason
- Test visually: Use
palettize showto preview before exporting - Consider your data: Scientific data often benefits from perceptually uniform spaces
- Be consistent: Use the same space throughout a project
Perceptual Uniformity Explained
Section titled “Perceptual Uniformity Explained”A “perceptually uniform” color space means equal numeric changes produce equal perceived changes in color.
In sRGB:
- Changing R from 100→150 might look very different from 150→200
- The same numeric difference produces different perceived differences
In Oklch:
- Equal steps in lightness produce equal perceived brightness changes
- Colors blend more naturally
This is especially important for data visualization where color represents numeric values.
Advanced: Color Space Reference
Section titled “Advanced: Color Space Reference”| Space | Type | Perceptual | Hue Handling |
|---|---|---|---|
srgb | RGB | No | N/A |
oklch | Cylindrical | Yes | Excellent |
lab | Rectangular | Yes | Good |
hcl | Cylindrical | Yes | Good |
hsl | Cylindrical | No | Direct |
hsv | Cylindrical | No | Direct |
okhsl | Cylindrical | Yes | Good |
okhsv | Cylindrical | Yes | Good |
Palettize supports any color space that ColorAide supports. See the ColorAide documentation for the full list.