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;
}