Public
Edited
Aug 1, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function sortWithClusters(colorsToSort) {
const assignedColors = new Set(); // Set to keep track of assigned colors

colorsToSort.forEach((color) => {
let minDistance;
let minDistanceClusterIndex;
clusters.forEach((cluster, clusterIndex) => {
const colorRgbArr = [color.RGB[0], color.RGB[1], color.RGB[2]];
const distance = colorDistance(colorRgbArr, cluster.leadColor);
if (typeof minDistance === 'undefined' || minDistance > distance) {
minDistance = distance;
minDistanceClusterIndex = clusterIndex;
}
});
// Check if color is already assigned to a cluster
if (!assignedColors.has(color)) {
clusters[minDistanceClusterIndex].colors.push(color);
assignedColors.add(color); // Add color to set of assigned colors
}
});

clusters.forEach((cluster) => {
const dim = ['white', 'grey', 'black'].includes(cluster.name) ? 'l' : 's';
cluster.colors = oneDimensionSorting(cluster.colors, dim);
});

return clusters.flatMap(cluster => cluster.colors);
}
Insert cell
Insert cell
Insert cell
Insert cell
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