Skip to content

Instantly share code, notes, and snippets.

@drwpow
Last active November 15, 2021 01:04
Show Gist options
  • Save drwpow/0fabf0cc932285ad023ca39e6f9ed35d to your computer and use it in GitHub Desktop.
Save drwpow/0fabf0cc932285ad023ca39e6f9ed35d to your computer and use it in GitHub Desktop.
Count unique HSL colors

Count unique HSL colors

Convert every possible RGB value to HSL to map the overlapping color space.

Setup

  1. Save this locally
  2. Install the color-convert package
  3. Run with node (node map-hsl.js)

Result

duplicate colors: 14348830
% loss:           85.52569150924683

FAQ

Is the result accurate?

🤷‍♂️ Based on the RGB <> HSL formula used, you will get different numbers in the end. When I wrote my own algorithm I got different results than using the color-convert library. It call comes down to differences in rounding, and even questions on whether or not 0° and 360° hue are the same thing. When you try and implement HSL, you realize there’s a lot more ambiguity than anticipated.

But no matter what formula you end up using, you should end up somewhere within this ballpark.

import convert from "color-convert";
const TOTAL_RGB = 256 ** 3;
const hslColors = new Set();
let countDupes = 0;
for (let r = 0; r <= 255; r++) {
for (let g = 0; g <= 255; g++) {
for (let b = 0; b <= 255; b++) {
const hsl = convert.rgb.hsl(r, g, b).join(",");
if (hslColors.has(hsl)) {
countDupes++;
} else {
hslColors.add(hsl);
}
}
}
}
console.log(`duplicate colors: ${countDupes}`);
console.log(`% loss: ${(100 * countDupes) / TOTAL_RGB}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment