Untitled

 avatar
unknown
typescript
10 months ago
696 B
2
Indexable
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;
}
Editor is loading...
Leave a Comment