Created
August 8, 2023 15:43
-
-
Save johnhunter/e685cfd609337bfceada3a3c6c14e454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given color saturation and lightness percentages, | |
* returns a function that generates a unique css `hsl` | |
* color value for a given string. | |
*/ | |
const hashColor = (saturation: number, lightness: number) => ( | |
str: string, | |
): string => { | |
let hash = 0; | |
for (let i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
} | |
const hue = hash % 360; | |
return `hsl(${hue}deg, ${saturation}%, ${lightness}%)`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment