Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created August 8, 2023 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhunter/e685cfd609337bfceada3a3c6c14e454 to your computer and use it in GitHub Desktop.
Save johnhunter/e685cfd609337bfceada3a3c6c14e454 to your computer and use it in GitHub Desktop.
/**
* 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