Public
Edited
Mar 5, 2022
Insert cell
Insert cell
rgbList = FileAttachment("rgb.json").json()
Insert cell
_ = require('lodash')
Insert cell
chroma = require('chroma-js')
Insert cell
d3 = require('d3')
Insert cell
randomRGBColors = _.sampleSize(rgbList, 60)
Insert cell
{
const svg = d3.select(DOM.svg(1200, 750));

let words = randomRGBColors.map(d => ({
text: d.input,
name: d.input,
color: d.target
}));

let rectWidth = 145;
let rectHeight = 30;
let columnSize = 6;

function draw(words) {
let region = svg
.append('g')
.attr('transform', `translate(${rectWidth / 2 + 10}, ${0})`);

let groups = region
.selectAll("g.rect")
.data(words)
.enter()
.append("g")
.attr('class', 'rect')
.attr("transform", (d, i) => {
let x = (i % columnSize) * (rectWidth + 15);
let y = Math.floor(i / columnSize) * (rectHeight + 42);
return `translate(${x}, ${y})`;
});

groups
.append('rect')
.attr('x', -rectWidth / 2)
// .attr('y', -rectHeight / 2)
.attr('rx', 2)
.attr('width', rectWidth)
.attr('height', rectHeight)
.style('stroke', 'hsla(0, 0%, 50%)')
.style('fill', d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', d => {
let curColor = chroma(d.text).hcl();
if (curColor[2] > 0) {
return 'black';
} else {
return 'white';
}
})
.attr('y', rectHeight + 5)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
.text(d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 18)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
// .style('font-style', 'italic')
.text(d => d.color);
}

draw(words);

return svg.node();
}
Insert cell
md`## Generate HEX`
Insert cell
hexList = FileAttachment("hex.json").json()
Insert cell
randomHexColors = _.sampleSize(hexList, 60)
Insert cell
{
const svg = d3.select(DOM.svg(1200, 750));

let words = randomHexColors.map(d => ({
text: d.input,
name: d.input,
color: d.target
}));

let rectWidth = 145;
let rectHeight = 30;
let columnSize = 6;

function draw(words) {
let region = svg
.append('g')
.attr('transform', `translate(${rectWidth / 2 + 10}, ${0})`);

let groups = region
.selectAll("g.rect")
.data(words)
.enter()
.append("g")
.attr('class', 'rect')
.attr("transform", (d, i) => {
let x = (i % columnSize) * (rectWidth + 15);
let y = Math.floor(i / columnSize) * (rectHeight + 42);
return `translate(${x}, ${y})`;
});

groups
.append('rect')
.attr('x', -rectWidth / 2)
// .attr('y', -rectHeight / 2)
.attr('rx', 2)
.attr('width', rectWidth)
.attr('height', rectHeight)
.style('stroke', 'hsla(0, 0%, 50%)')
.style('fill', d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', d => {
let curColor = chroma(d.text).hcl();
if (curColor[2] > 0) {
return 'black';
} else {
return 'white';
}
})
.attr('y', rectHeight + 5)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
.text(d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 18)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
// .style('font-style', 'italic')
.text(d => d.color);
}

draw(words);

return svg.node();
}
Insert cell
md`## Generate HSL`
Insert cell
hslList = FileAttachment("hsl.json").json()
Insert cell
randomHSLColors = _.sampleSize(hslList, 48)
Insert cell
{
const svg = d3.select(DOM.svg(1300, 860));

let words = randomHSLColors.map(d => ({
text: d.input,
name: d.input,
color: d.target
}));

let rectWidth = 185;
let rectHeight = 30;
let columnSize = 4;

function draw(words) {
let region = svg
.append('g')
.attr('transform', `translate(${rectWidth / 2 + 10}, ${0})`);

let groups = region
.selectAll("g.rect")
.data(words)
.enter()
.append("g")
.attr('class', 'rect')
.attr("transform", (d, i) => {
let x = (i % columnSize) * (rectWidth + 15);
let y = Math.floor(i / columnSize) * (rectHeight + 42);
return `translate(${x}, ${y})`;
});

groups
.append('rect')
.attr('x', -rectWidth / 2)
// .attr('y', -rectHeight / 2)
.attr('rx', 2)
.attr('width', rectWidth)
.attr('height', rectHeight)
.style('stroke', 'hsla(0, 0%, 50%)')
.style('fill', d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', d => {
let curColor = chroma(d.text).hcl();
if (curColor[2] > 0) {
return 'black';
} else {
return 'white';
}
})
.attr('y', rectHeight + 5)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
.text(d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 18)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
// .style('font-style', 'italic')
.text(d => d.color);
}

draw(words);

return svg.node();
}
Insert cell
md`## Generate HCL`
Insert cell
hclList = FileAttachment("hcl.json").json()
Insert cell
randomHCLColors = _.sampleSize(hclList, 48)
Insert cell
randomHCLColors[0].input
Insert cell
getHCL = string => {
let reg = /hcl\(([\d\.]*),\s([\d\.]*),\s([\d\.]*)\)/;
// let string = "hcl(344.69, 22.12, 44.36)";
let results = [];
let matched = string.match(reg);
results.push(Number(matched[1]));
results.push(Number(matched[2]));
results.push(Number(matched[3]));
return results;
}
Insert cell
{
const svg = d3.select(DOM.svg(1300, 860));

let words = randomHCLColors.map(d => ({
text: d.input,
name: d.input,
color: d.target
}));

let rectWidth = 185;
let rectHeight = 30;
let columnSize = 4;

function draw(words) {
let region = svg
.append('g')
.attr('transform', `translate(${rectWidth / 2 + 10}, ${0})`);

let groups = region
.selectAll("g.rect")
.data(words)
.enter()
.append("g")
.attr('class', 'rect')
.attr("transform", (d, i) => {
let x = (i % columnSize) * (rectWidth + 15);
let y = Math.floor(i / columnSize) * (rectHeight + 42);
return `translate(${x}, ${y})`;
});

groups
.append('rect')
.attr('x', -rectWidth / 2)
// .attr('y', -rectHeight / 2)
.attr('rx', 2)
.attr('width', rectWidth)
.attr('height', rectHeight)
.style('stroke', 'hsla(0, 0%, 50%)')
.style('fill', d => {
let color = getHCL(d.text);
let colorC = chroma.hcl(color[0], color[1], color[2]);
console.log(colorC.hex());
return colorC.hex();
});

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 5)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
.text(d => d.text);

groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 18)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
// .style('font-weight', 600)
// .style('font-style', 'italic')
.text(d => d.color);
}

draw(words);

return svg.node();
}
Insert cell
jp = chroma('B0000F')
Insert cell
jp.rgb()
Insert cell
jp.hsl()
Insert cell
jp.hcl()
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