Skip to content

JSON Export Format

The JSON exporter creates JSON files with configurable structure and color formats, ideal for JavaScript applications, configuration files, and APIs.

PropertyValue
Identifierjson
Extension.json
Use caseJavaScript apps, config files, APIs

Default output (array structure with hex colors):

[
"#440154",
"#3b528b",
"#21918d",
"#5cc863",
"#fde725"
]
Terminal window
# Run with uvx (no installation required)
uvx palettize create viridis --format json --output colors.json --steps 10
# Or if installed
palettize create viridis --format json --output colors.json --steps 10
# With structure option
palettize create viridis -f json -O structure=full --steps 5
# With color format option
palettize create viridis -f json -O color_format=rgb --steps 5
from palettize import create_colormap, get_scaler_by_name, get_exporter
import json
cmap = create_colormap(preset="viridis")
scaler = get_scaler_by_name("linear", domain_min=0, domain_max=100)
exporter = get_exporter("json")
output = exporter.export(
colormap=cmap,
scaler=scaler,
domain_min=0,
domain_max=100,
options={"num_colors": 5, "structure": "full"}
)
data = json.loads(output)
OptionTypeDefaultDescription
num_colorsint256Number of colors to generate
structurestr"array"JSON structure (see below)
color_formatstr"hex"Color format (see below)
indentint2JSON indentation (0 for compact)

Simple array of colors:

["#440154", "#3b528b", "#21918d", "#5cc863", "#fde725"]

Array of objects with position and value:

[
{"position": 0.0, "value": 0.0, "color": "#440154"},
{"position": 0.25, "value": 25.0, "color": "#3b528b"},
{"position": 0.5, "value": 50.0, "color": "#21918d"},
{"position": 0.75, "value": 75.0, "color": "#5cc863"},
{"position": 1.0, "value": 100.0, "color": "#fde725"}
]

Object with 0-255 position keys (common for raster colormaps):

{
"0": "#440154",
"64": "#3b528b",
"128": "#21918d",
"191": "#5cc863",
"255": "#fde725"
}

Complete metadata object:

{
"name": "viridis",
"num_colors": 5,
"domain": [0, 100],
"interpolation_space": "oklch",
"colors": ["#440154", "#3b528b", "#21918d", "#5cc863", "#fde725"]
}
FormatDescriptionExample
hexHex strings (default)"#440154"
rgbRGB arrays[68, 1, 84]
rgbaRGBA arrays[68, 1, 84, 255]
hslHSL arrays[282.0, 97.0, 17.0]
// Fetch colormap from API or file
const colors = await fetch('viridis.json').then(r => r.json());
// Use with D3
const colorScale = d3.scaleLinear()
.domain([0, colors.length - 1])
.range(colors);
// Use with Chart.js
const chart = new Chart(ctx, {
data: { ... },
options: {
plugins: {
colors: {
forceOverride: true
}
}
}
});
{
"visualization": {
"colormap": ["#440154", "#3b528b", "#21918d", "#5cc863", "#fde725"]
}
}
Terminal window
# Generate API-ready JSON
palettize create viridis -f json -O structure=array_objects -O indent=0 --steps 256
import colors from './viridis.json';
function ColorBar() {
return (
<div style={{ display: 'flex' }}>
{colors.map((color, i) => (
<div key={i} style={{ backgroundColor: color, flex: 1, height: 20 }} />
))}
</div>
);
}
Terminal window
# RGB arrays for canvas/WebGL
palettize create viridis -f json -O color_format=rgb --steps 256

Output:

[[68, 1, 84], [59, 82, 139], [33, 145, 141], ...]

Use in JavaScript:

const [r, g, b] = colors[128];
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
StructureBest For
arraySimple color lists, most JS libraries
array_objectsWhen you need position/value mapping
objectRaster colormap compatibility (0-255 keys)
fullComplete metadata, documentation