Published
Edited
Dec 3, 2020
Insert cell
md`# Color Demo`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
colors = {
let colorArray = [];

let seedColor = d3.hsl(
Math.random() * 60 + 300,
traditionSlider,
(traditionSlider + extravaganceSlider) / 2
);

let seedComplement1 = seedColor.copy();
seedComplement1.h += 120 % 360;

let seedComplement2 = seedColor.copy();
seedComplement2.h += 240 % 360;

seedComplement2.l = extravaganceSlider;

colorArray.push(seedColor);
colorArray.push(seedComplement1);
colorArray.push(seedComplement2);

return colorArray;
}
Insert cell
{
let svg = d3
.create('svg')
.attr("width", width)
.attr('height', height)
.attr('id', 'mainViz');

let bg = svg
.selectAll('.palettes')
.data(colors)
.enter()
.append('rect')
.attr("width", width / colorCount)
.attr('height', height)
.attr('fill', d => d)
.attr('x', (d, i) => (i * width) / colorCount)
.attr('class', 'palettes');

return svg.node();
}
Insert cell
genColor
Insert cell
aScale = d3
.scaleLinear()
.domain([0, .5, .75, 1])
.range([0, .1, .2, .3, 1])
Insert cell
colorScale = d3
.scaleLinear()
.domain([0, .25, .5, .75, 1])
.range([
d3.color('blue'),
d3.color('pink'),
d3.color('yellow'),
d3.color('blue'),
d3.color('purple')
])
Insert cell
d3.interpolateViridis(.5)
Insert cell
colorScale(.432)
Insert cell
function genColor(a, e, t) {
let hue = (0 + a * 25 + e * 80 + t * 9) % 360;
let sat = e * .6 + a * a * a;
let bri = aScale(a) + 1 - (a + e) * t * t;

return d3.hsl(hue, sat, bri);
}
Insert cell
function transformColor(color, a,e,t){
}
Insert cell
genColor(accessiblitySlider, extravaganceSlider, traditionSlider)
Insert cell
Insert cell
{
for (let i = 0; i < colors.length; i++) {
let tempColor = d3.rgb(colors[i]).copy();
if (tempColor.r > accessiblitySlider) {
tempColor.r += 100;
} else {
tempColor.r -= 100;
}
colors[i] = tempColor;
}

updateColors();
}
Insert cell
// updateColors()
Insert cell
updateColors = function() {
d3.select('#mainViz')
.selectAll('.palettes')
.data(colors)
.transition()
.duration(2000)
.attr('fill', d => d);
}
Insert cell
d3 = require("d3@5")
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