Published
Edited
May 6, 2019
Importers
Insert cell
md`# Color Utilities`
Insert cell
md`
~~~js
import {rampRGB} from "@bchoatejr/color-utilities"
~~~

`
Insert cell
rampRGB({interpolateRGB:cX})
Insert cell
cX = d3.interpolateRgb("#0db", "#b0d");
Insert cell
cX(0)
Insert cell
cX(0.5)
Insert cell
cX(1)
Insert cell
function rampRGB(cfg) {
let {c1="#fff", c2="#00f", interpolateRGB=false} = cfg;
let canvas;
// let dark;
let n=256;
if (interpolateRGB == false) interpolateRGB = d3.interpolateRgb(c1, c2);
// const interpolateRGB = d3.interpolateRgb(c1, c2);
// const interpolate = d3[`interpolate${name}`];
let colors = [];
// dark = d3.lab(interpolate(0)).l < 50;
for (let i = 0; i < n; ++i) {
colors.push(interpolateRGB(i/n));
}
const context = (canvas = DOM.canvas(n, 1)).getContext("2d");
canvas.style.margin = "0 -14px";
canvas.style.width = "calc(100% + 28px)";
canvas.style.height = "33px";
canvas.style.cursor = "pointer";
for (let i = 0; i < n; ++i) {
context.fillStyle = colors[i];
context.fillRect(i, 0, 1, 1);
}
const label = document.createElement("DIV");
label.textContent = name;
label.style.position = "absolute";
label.style.top = "4px";
// label.style.color = dark ? `#fff` : `#000`;
return html`${canvas}${label}`;
}
Insert cell
d3 = require("d3@5")
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