Public
Edited
Jul 24, 2020
2 stars
Insert cell
Insert cell
function hwb2rgb(h, w, b) {
// Assume w, b in [0, 1] and h is an angle in degrees
// (CSS4 states that w, b outside [0, 1] are invalid)
// Returns RGB coordinates in [0, 255]
var col = d3.hsl(h, 1, 0.5).rgb(),
s = w + b;
// Normalise whiteness and blackness
if (s > 1) {
w /= s;
b /= s;
s = 1;
}
return [col.r, col.g, col.b].map(x => x * (1 - s) + w * 255);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function rgb2hwb(r, g, b) {
// Essentially an inverse to the hwb2rgb function defined above
return [d3.hsl(d3.rgb(r, g, b)).h,
Math.min(r, g, b) / 255,
1 - Math.max(r, g, b) / 255];
}
Insert cell
rgb2hwb(...hwb2rgb(hue, whiteness, blackness))
Insert cell
Insert cell
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