Skip to content

Instantly share code, notes, and snippets.

@roryokane
Last active October 15, 2017 05:39
Show Gist options
  • Save roryokane/f15bb23abcf9938c0707 to your computer and use it in GitHub Desktop.
Save roryokane/f15bb23abcf9938c0707 to your computer and use it in GitHub Desktop.
find collisions between rounded HUSL triads (for https://github.com/husl-colors/husl-site/pull/9)
function intTo2DigitHex(integer) {
if (integer < 16) {
return '0' + integer.toString(16)
} else {
return integer.toString(16)
}
}
function findCollisions(numDigitsRounding, quitAfterFindingOne) {
var knownHusls = {};
var numCollisionsFound = 0;
for (var r = 0; r < 256; r++) {
for (var g = 0; g < 256; g++) {
for (var b = 0; b < 256; b++) {
var thisHex = intTo2DigitHex(r) + intTo2DigitHex(g) + intTo2DigitHex(b);
var husl = $.husl.fromHex(thisHex);
roundedHuslString = husl.map(function(num){ return num.toFixed(numDigitsRounding) }).join(',');
if (knownHusls[roundedHuslString]) {
var oldHex = knownHusls[roundedHuslString];
console.log("found collision", roundedHuslString, "for hexes", thisHex, "and", oldHex);
numCollisionsFound++;
if (quitAfterFindingOne) return;
} else {
knownHusls[roundedHuslString] = thisHex;
}
}
}
console.log("finished enumerating gs and bs for r =", r);
console.log("collisions found so far:", numCollisionsFound);
}
console.log("collisions found:", numCollisionsFound);
}
findCollisions(2, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment