Created
July 27, 2022 11:08
-
-
Save exuanbo/29cff0e6da2ca107b879680139756eac 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
const stringToColor = (str: string) => { | |
let hash = 0 | |
for (let i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash) | |
} | |
let color = '#' | |
for (let i = 0; i < 3; i++) { | |
const value = (hash >> (i * 8)) & 0xff | |
color += `00${value.toString(16)}`.slice(-2) | |
} | |
return color | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment