(string: string)
| 1 | export function stringToColor(string: string) { |
| 2 | let hash = 0; |
| 3 | string.split("").forEach((char) => { |
| 4 | hash = char.charCodeAt(0) + ((hash << 5) - hash); |
| 5 | }); |
| 6 | |
| 7 | let color = "#"; |
| 8 | for (let i = 0; i < 3; i++) { |
| 9 | const value = (hash >> (i * 8)) & 0xff; |
| 10 | color += value.toString(16).padStart(2, "0"); |
| 11 | } |
| 12 | return color; |
| 13 | } |