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

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