Published
Edited
Mar 18, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
apcaTable = {
const table = d3.create("table");
const thead = table.append("thead");
thead
.append("tr")
.selectAll("th")
.data(["", ...colours])
.join("th")
.style("color", (d) => d)
.style("text-shadow", "none")
.style("background", "white")
.text((d) => d);
const tbody = table.append("tbody");
colours.forEach((colour, row) => {
tbody
.append("tr")
.selectAll("td")
.data([colour, ...colours])
.join("td")
.style("text-shadow", "none")
.style("background", colour)
.style("color", (d, i) => (i ? d : "black"))
/*.style("font-weight", (d, i) =>
i > 0 && apca(d, colour) < 45 ? "normal" : "bold"
)*/
.text((d, i) =>
i === 0 ? d : i === row + 1 ? "" : apca(d, colour).toFixed(1)
);
});

return table.node();
}
Insert cell
Insert cell
extremes = {
let extremes = { worst: Infinity, best: 0 };
colours.forEach((d, i) => {
colours.forEach((e, j) => {
if (i <= j) return;
let contrast = chroma.contrast(d, e);
extremes.worst = Math.min(extremes.worst, contrast);
extremes.best = Math.max(extremes.best, contrast);
});
});
return extremes;
}
Insert cell
Insert cell
Insert cell
chroma = require("chroma-js")
Insert cell
chroma("#aabbcc").rgba()
Insert cell
apca = (fg, bg) =>
apcaw3.APCAcontrast(
apcaw3.sRGBtoY(chroma(fg).rgba()),
apcaw3.sRGBtoY(chroma(bg).rgba())
)
Insert cell
apcaw3 = import("https://cdn.skypack.dev/apca-w3")
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