25 lines
957 B
TypeScript
25 lines
957 B
TypeScript
// frontend/src/utils/helpers.ts
|
|
|
|
export const stringToColor = (str: string | null | undefined): string => {
|
|
if (!str) return '#71717a';
|
|
|
|
const normalized: string = str.trim().toLowerCase();
|
|
const salt: string = 'volley-standard-salt-v5';
|
|
const COURT_COLORS: string[] = ['#ea580c', '#0284c7', '#059669', '#ca8a04', '#dc2626', '#0891b2', '#e11d48', '#65a30d'];
|
|
|
|
let hash: number = 0;
|
|
const combined: string = normalized + salt;
|
|
|
|
for (let i = 0; i < combined.length; i++) {
|
|
hash = combined.charCodeAt(i) + ((hash << 5) - hash);
|
|
}
|
|
|
|
return COURT_COLORS[Math.abs(hash) % COURT_COLORS.length];
|
|
};
|
|
|
|
export const printName = (name: string | null | undefined): string => {
|
|
if (!name) return '';
|
|
if (name.startsWith('Winner of #')) return name.replace('Winner of #', 'W') + ': _______________';
|
|
if (name.startsWith('Loser of #')) return name.replace('Loser of #', 'L') + ': _______________';
|
|
return name;
|
|
}; |