Untitled

mail@pastecode.io avatar
unknown
typescript
25 days ago
696 B
1
Indexable
Never
function stringToHash(str: string): number {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        hash = str.charCodeAt(i) + ((hash << 5) - hash);
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}

function hashToHexColor(hash: number): string {
    const color = ((hash >> 24) & 0xFF).toString(16).padStart(2, '0') +
        ((hash >> 16) & 0xFF).toString(16).padStart(2, '0') +
        ((hash >> 8) & 0xFF).toString(16).padStart(2, '0');
    return `#${color}`;
}

export function getRandomHexColorFromSeed(seed: string): string {
    const hash = stringToHash(seed);
    const color = hashToHexColor(hash);
    return color;
}
Leave a Comment