Skip to content

Instantly share code, notes, and snippets.

@exuanbo
Created July 27, 2022 11:08
Show Gist options
  • Save exuanbo/29cff0e6da2ca107b879680139756eac to your computer and use it in GitHub Desktop.
Save exuanbo/29cff0e6da2ca107b879680139756eac to your computer and use it in GitHub Desktop.
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