Skip to content

Instantly share code, notes, and snippets.

@rohitsaini1196
Last active May 8, 2022 13:04
Show Gist options
  • Save rohitsaini1196/432f0312c2b16be96709954776e1061b to your computer and use it in GitHub Desktop.
Save rohitsaini1196/432f0312c2b16be96709954776e1061b to your computer and use it in GitHub Desktop.
Generate Unique Color from a Unique string
function toColor(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var c = (hash & 0x00ffffff).toString(16).toUpperCase();
return "#" + c;
}
console.log(toColor("cool"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment