Public
Edited
Mar 31
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
viewof colorscale = ColorScaleSelector(regl, {
label: "Color scale",
value: "Spectral",
invalidation
})
Insert cell
ColorScaleSelector(regl, {
value: "Spectral",
invalidation
})
Insert cell
colorscale
Insert cell
Insert cell
Insert cell
Insert cell
draw = {
regl.poll();
fillWithColorScale({ colorscale });
}
Insert cell
fillWithColorScale = regl({
vert: `
precision highp float;
attribute vec2 xy;
varying vec2 uv;
void main () {
uv = 0.5 + 0.5 * xy;
gl_Position = vec4(xy, 0, 1);
}`,
frag: `
precision highp float;
uniform sampler2D colorscale;
varying vec2 uv;
void main () {
gl_FragColor = texture2D(colorscale, uv);
}`,
attributes: { xy: [-4, -4, 4, -4, 0, 4] },
uniforms: { colorscale: regl.prop("colorscale.texture") },
depth: { enable: false },
count: 3
})
Insert cell
Insert cell
Insert cell
function lookupTable(interpolator, n = 256) {
return d3.quantize(interpolator, n).map((c) => {
return (c = d3.rgb(c)), [c.r, c.g, c.b, 255];
});
}
Insert cell
function ColorScaleSelector(regl, args = {}) {
const input = Inputs.select(COLOR_SCALE_NAMES, args);
const gradientOutput = html`<div></div>`;
const invert = html`<input type="checkbox" checked=${
args.invert ? '"checked"' : '""'
}>`;
const wrapper = html`<div>${input}<label>${invert} Invert</label>${gradientOutput}</div>`;

invert.parentElement.style.marginRight = "10px";
wrapper.style.display = "flex";
wrapper.style.flexDirection = "row";
wrapper.style.alignItems = "center";
gradientOutput.style.width = "150px";
gradientOutput.style.height = "17px";
gradientOutput.style.border = "1px solid #888";
gradientOutput.style.borderRadius = "2px";
input.style.marginRight = "10px";
input.style.maxWidth = args.label ? "275px" : "198px";

let texture = null;
function createTexture(name) {
const data = lookupTable(d3[`interpolate${name}`]);
if (invert.checked) {
data.reverse();
}
gradientOutput.style.backgroundImage = `linear-gradient(90deg, ${data
.map(
([r, g, b], i) =>
`rgb(${r},${g},${b}) ${(100 * i) / (data.length - 1)}%`
)
.join(",")})`;

texture = (texture || regl.texture)({
width: 256,
height: 1,
data: new Uint8ClampedArray(data.flat()),
min: "linear",
mag: "linear"
});
return { texture, data };
}
if (args.invalidation) {
invalidation.then(() => {
if (texture) texture.destroy();
});
}
function emitTexture() {
wrapper.value = {
...createTexture(input.value),
name: input.value
};
wrapper.dispatchEvent(new CustomEvent("input"));
}
invert.addEventListener("change", emitTexture);
input.addEventListener("change", emitTexture);
emitTexture({ target: { value: COLOR_SCALE_NAMES.indexOf(input.value) } });

return wrapper;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more