Public
Edited
Jul 5, 2023
Insert cell
Insert cell
viewof COLORMAPS_TEXT = Inputs.textarea({
rows: 24,
})
Insert cell
COLORMAPS = {
const text = COLORMAPS_TEXT;

const lines = text.split('\n');
const colormaps = [];
let colormap = null;
for (let i=0, n=lines.length; i<n; ++i) {
const line = lines[i];

if (line.startsWith(' ')) {
// colormap value
const [r, g, b] = line.matchAll(/(?:\d+\.)\d+/g);
colormap.values.push({
r: Number.parseFloat(r),
g: Number.parseFloat(g),
b: Number.parseFloat(b),
});

} else if (line.startsWith(' {"')) {
// colormap name
const [name] = line.match(/\w+/);
colormaps.push(colormap = {
name,
values: [],
});
} else if (line.startsWith(' }}')) {
// emit colormap
}
}

return colormaps;
}
Insert cell
{
const colormaps = COLORMAPS;

let codes = [];
for (const { name, values } of colormaps) {
EMIT(` { "${name}", {`);
for (let i=0, n=values.length; i<n; ++i) {
// if (i !== n-1 && i % 16 != 0) continue;
const { r, g, b } = values[i];
EMIT(` /* ${`${i}`.padStart(3, ' ')} */ { ${r}, ${g}, ${b} },`);
}
EMIT(` } },`);
}

return htl.html`<textarea rows=24 cols=80>${codes.join("\n")}`;

function EMIT(...lines) {
codes = codes.concat(lines);
}
}
Insert cell
viewof OPACITYMAPS_TEXT = Inputs.textarea({
rows: 24,
})
Insert cell
OPACITYMAPS = {
const text = OPACITYMAPS_TEXT;

const lines = text.split('\n');
const maps = [];
let map = null;
for (let i=0, n=lines.length; i<n; ++i) {
const line = lines[i];

if (line.startsWith('//')) {
continue;
} else if (line.startsWith('std::vector<float> ')) {
const [_, name] = line.match(/std::vector<float> (\w+)/);
maps.push(map = {
name,
values: [],
});
} else if (line.match(/\d+/)) {
const values = Array.from(line.matchAll(/(?:\d+\.)\d+/g)).map((d) => Number.parseFloat(d));
map.values = map.values.concat(values);
}
}

return maps;
}
Insert cell
{
const maps = OPACITYMAPS;

let codes = [];
for (const { name, values } of maps) {
EMIT(` { "${name}", {`);
for (let i=0, n=values.length; i<n; ++i) {
// if (i !== n-1 && i % 16 != 0) continue;
const value = values[i];
EMIT(` /* ${`${i}`.padStart(3, ' ')} */ ${value},`);
}
EMIT(` } },`);
}

return htl.html`<textarea rows=24 cols=80>${codes.join("\n")}`;

function EMIT(...lines) {
codes = codes.concat(lines);
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more