function sortWithClusters(colorsToSort) {
const assignedColors = new Set();
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;
}
});
if (!assignedColors.has(color)) {
clusters[minDistanceClusterIndex].colors.push(color);
assignedColors.add(color);
}
});
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);
}