function drawQuilt(
quiltData,
{
palette = "Spectral",
scheme = "Sequence",
center = false,
highlightIndex = -1,
size = 600
} = {}
) {
const maxSize = size;
const n = quiltData.n;
const cellSize = n > maxSize ? 1 : Math.floor(maxSize / n);
const quilt = quiltData.sequences;
const canvas = html`<canvas id="quilt" width="${n * cellSize}" height="${
n * cellSize
}" style="width: 100%; height: 100%"></canvas>`;
const ctx = canvas.getContext("2d", { alpha: false });
ctx.imageSmoothingEnabled = false;
const numSequences = quilt.length;
const offset = Math.floor(n / 2);
const grayscale = culori.filterSaturate(0);
const colors = quilt.map((sequence, index) => {
const [i, j] = sequence;
const scale = d3Scale["interpolate" + palette];
let color;
switch (scheme) {
case "Columns": {
const hue = scale(j / n);
const desaturate = culori.filterSaturate((n - i) / n);
color = scale(index / (numSequences + 1));
break;
}
case "Period": {
color = scale(
quiltData.periods().indexOf(sequence.length) /
(quiltData.periods.length - 1)
);
break;
}
case "Unique Nums": {
color = scale(new Set(sequence).size / n);
break;
}
default:
color = scale(index / (numSequences + 1));
}
if (+highlightIndex >= 0 && +highlightIndex !== i) {
let hsl = d3.hsl(color);
hsl.s = 0;
color = d3.color(hsl);
}
return d3.color(color).rgb();
});
const pixels = new Uint8ClampedArray(
4 * quiltData.table.length * cellSize * cellSize
);
for (let k = 0; k < quiltData.table.length; k++) {
let color = colors[quiltData.table[k]];
let i = Math.floor(k / n);
let j = k % n;
for (let ipix = i * cellSize; ipix < (i + 1) * cellSize; ipix++) {
for (let jpix = j * cellSize; jpix < (j + 1) * cellSize; jpix++) {
let kpix = ipix * n * cellSize + jpix;
pixels[4 * kpix] = Math.floor(color.r);
pixels[4 * kpix + 1] = Math.floor(color.g);
pixels[4 * kpix + 2] = Math.floor(color.b);
pixels[4 * kpix + 3] = 255;
}
}
}
const imageData = new ImageData(pixels, n * cellSize, n * cellSize);
ctx.putImageData(imageData, 0, 0);
return html`<div style="width: ${maxSize}px; height: ${maxSize}px;">${canvas}</div>`;
}