brackt/app/lib/avatar-colors.ts

20 lines
422 B
TypeScript

export const AVATAR_COLORS = [
"#adf661",
"#2ce1c1",
"#8b5cf6",
"#f59e0b",
"#ef4444",
"#3b82f6",
];
export function hashString(input: string): number {
let hash = 0;
for (let i = 0; i < input.length; i++) {
hash = (hash * 31 + input.charCodeAt(i)) & 0xffff;
}
return hash;
}
export function avatarColor(input: string): string {
return AVATAR_COLORS[hashString(input) % AVATAR_COLORS.length];
}