unique-window-colors
unique-window-colors copied to clipboard
Bad hash function
Given two strings which only differs at the last character, the hash code does not differ much. As a result, the resulted colors of these two strings are very similar.
e.g.
hashCode('my-repo-1') = 1331048407
hashCode('my-repo-2') = 1331048408
Here is the source code I found in the repo:
// https://www.designedbyaturtle.co.uk/convert-string-to-hexidecimal-colour-with-javascript-vanilla/
// Hash any string into an integer value
// Then we'll use the int and convert to hex.
function hashCode(str: string) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
From my experience, quite a lot of developers clone one repo and add suffixes like -1
, -2
, etc. That's because we want to avoid switching between branches.
My suggestion is to use a better hash function. For example, you can use this lightweight PRNG library, https://www.npmjs.com/package/random-seed.
I somehow always get green even if the name of my repo is quite different. On Ubuntu if that makes any difference