Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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