Skip to content

Colormap Class

The Colormap class is the core of Palettize, representing a colormap defined by color stops with optional positions.

The recommended way to create colormaps is using the create_colormap() factory function:

from palettize import create_colormap
# From a preset
cmap = create_colormap(preset="viridis")
# From custom colors
cmap = create_colormap(colors=["blue", "white", "red"])
ParameterTypeDefaultDescription
presetstr | NoneNoneName of a preset palette
colorslist | NoneNoneList of colors
interpolation_spacestr"oklch"Color space for interpolation
namestr | NoneNoneDisplay name for the colormap
cut_startfloat0.0Start of sub-segment (0-1)
cut_endfloat1.0End of sub-segment (0-1)

You must provide exactly one of preset or colors.

Colors can be specified in multiple formats:

# Named colors
create_colormap(colors=["red", "blue", "green"])
# Hex strings
create_colormap(colors=["#ff0000", "#00ff00", "#0000ff"])
# RGB tuples (0-255)
create_colormap(colors=[(255, 0, 0), (0, 255, 0), (0, 0, 255)])
# RGB tuples (0-1)
create_colormap(colors=[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)])
# Mixed formats
create_colormap(colors=["red", "#00ff00", (0, 0, 255)])

For more control, you can use the Colormap class directly.

from palettize import Colormap
cmap = Colormap.from_preset(
preset_name="viridis",
interpolation_space="oklch",
cut_start=0.0,
cut_end=1.0
)
from palettize import Colormap
cmap = Colormap.from_list(
colors=["blue", "white", "red"],
name="BWR",
interpolation_space="oklch",
cut_start=0.0,
cut_end=1.0
)

Returns the interpolated color at a position (0-1) in the specified format.

cmap = create_colormap(preset="viridis")
# Hex string (default)
hex_color = cmap.get_color(0.5) # "#21918d"
# RGB tuple (0-255)
rgb = cmap.get_color(0.5, output_format="rgb_tuple") # (33, 145, 141)
# RGBA tuple (0-255)
rgba = cmap.get_color(0.5, output_format="rgba_tuple") # (33, 145, 141, 255)
# RGB float (0-1) - useful for OpenGL, matplotlib
rgb_float = cmap.get_color(0.5, output_format="rgb_float") # (0.129, 0.567, 0.551)
# HSL tuple and string
hsl = cmap.get_color(0.5, output_format="hsl_tuple") # (177.9, 62.8, 34.8)
hsl_str = cmap.get_color(0.5, output_format="hsl_string") # "hsl(178, 63%, 35%)"
# OKLCH string (modern CSS)
oklch = cmap.get_color(0.5, output_format="oklch_string") # "oklch(59.5% 0.095 191.2)"
# Wide gamut (Display P3)
p3 = cmap.get_color(0.5, output_format="css_color", output_space="display-p3")
# "color(display-p3 0.2770 0.5585 0.5481)"
ParameterTypeDefaultDescription
positionfloatPosition in colormap (0-1)
output_formatstr"hex"Output format (see table below)
output_spacestr"srgb""srgb" or "display-p3". Affects rgb_*, rgba_*, and css_color. Hex is always sRGB
FormatReturn TypeExampleUse Case
hexstr"#21918d"CSS, general purpose. Always sRGB
rgb_tupletuple[int, ...](33, 145, 141)Image processing
rgba_tupletuple[int, ...](33, 145, 141, 255)Image processing with alpha
rgb_floattuple[float, ...](0.129, 0.567, 0.551)OpenGL, matplotlib, scientific
rgba_floattuple[float, ...](0.129, 0.567, 0.551, 1.0)OpenGL with alpha
hsl_tupletuple[float, ...](177.9, 62.8, 34.8)Color manipulation
hsl_stringstr"hsl(178, 63%, 35%)"CSS HSL
oklch_stringstr"oklch(59.5% 0.095 191.2)"Modern CSS, perceptual
css_colorstr"color(display-p3 ...)"Wide gamut CSS

The output_space parameter enables wide gamut output for modern displays:

# Standard sRGB (default)
srgb_color = cmap.get_color(0.5) # "#21918d"
# Display P3 (wider gamut)
p3_color = cmap.get_color(0.5, output_format="css_color", output_space="display-p3")
# "color(display-p3 0.2770 0.5585 0.5481)"
# RGB floats in P3 space
p3_rgb = cmap.get_color(0.5, output_format="rgb_float", output_space="display-p3")
# (0.277, 0.559, 0.548)

Returns the interpolated color as a ColorAide Color object for advanced manipulation:

color_obj = cmap.get_color_object(0.5)
# Access color properties
print(color_obj.space()) # "oklch"
print(color_obj.coords()) # Color coordinates
# Convert to other spaces
srgb = color_obj.convert("srgb")
lab = color_obj.convert("lab")

Maps a data value to a color using a scaling function:

from palettize import create_colormap, get_scaler_by_name
cmap = create_colormap(colors=["blue", "white", "red"])
scaler = get_scaler_by_name("linear", domain_min=-100, domain_max=100)
# Get colors for data values
color_neg = cmap.apply_scaler(-50, scaler) # Blue-ish
color_zero = cmap.apply_scaler(0, scaler) # White
color_pos = cmap.apply_scaler(50, scaler) # Red-ish
ParameterTypeDefaultDescription
data_valuefloatThe data value to map
scalerScalingFunctionA scaling function
output_formatstr"hex"Output format
PropertyTypeDescription
namestr | NoneColormap display name
stopslist[ColorStop]List of color stops
interpolation_spacestrColor interpolation space
cut_startfloatStart of sub-segment (0-1)
cut_endfloatEnd of sub-segment (0-1)

Calling a colormap returns a hex string: cmap(0.5). len(cmap) is the number of stops.

cmap.hex_colors(5) # ['#440154', '#3b528b', '#21918d', '#5cc863', '#fde725']
cmap.rgb_colors(5)
cmap.rgba_colors(5)
cmap.hsl_colors(5)
cmap.colors(5, "rgb_float")

Each method returns a new colormap. Cuts compose, and reversed() toggles an _r suffix on the name, as matplotlib does.

from palettize import Colormap
viridis = Colormap.from_preset("viridis")
viridis.reversed()
viridis.cut(0.2, 0.8)
viridis.resampled(11) # refit to 11 evenly spaced stops
viridis.quantized(5) # 5 hard-edged bands
viridis.blend(Colormap.from_preset("magma"), 0.5)
viridis + Colormap.from_preset("magma") # concat

renamed("My Map") returns a copy with a new name. See the transforms guide.

viridis.reversed().save("my-map.json")
Colormap.load("my-map.json")

Files record schema version 1. A newer schema is refused. See colormap files.

A ColorStop represents a single color in the colormap with an optional position.

from palettize import ColorStop
# Position is optional (auto-calculated if not provided)
stop1 = ColorStop(color="red")
stop2 = ColorStop(color="blue", position=0.5)
stop3 = ColorStop(color="#00ff00", position=1.0)
PropertyTypeDescription
colorInputColorOriginal color input
positionfloat | NonePosition in colormap (0-1)
parsed_colorColorParsed ColorAide Color object

The cut_start and cut_end parameters let you use only a portion of a colormap:

# Use only the middle portion of viridis
cmap = create_colormap(
preset="viridis",
cut_start=0.2, # Skip first 20%
cut_end=0.8 # Skip last 20%
)
# Position 0 now maps to what was position 0.2
# Position 1 now maps to what was position 0.8
color_start = cmap.get_color(0) # Color at original 0.2
color_end = cmap.get_color(1) # Color at original 0.8

The interpolation_space parameter controls how colors are blended. Common options:

SpaceDescription
oklchPerceptually uniform, good for most cases (default)
srgbStandard RGB, may produce muddy transitions
labCIELAB, perceptually uniform
hslHue-Saturation-Lightness
oklchImproved perceptual uniformity
# Compare interpolation spaces
colors = ["blue", "yellow"]
cmap_srgb = create_colormap(colors=colors, interpolation_space="srgb")
cmap_oklch = create_colormap(colors=colors, interpolation_space="oklch")
cmap_lab = create_colormap(colors=colors, interpolation_space="lab")
# Get midpoint color in each space
print(cmap_srgb.get_color(0.5)) # Different results
print(cmap_oklch.get_color(0.5))
print(cmap_lab.get_color(0.5))

See the Color Interpolation guide for more details.