56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
export function getInitials(name: string): string {
|
|
const s = name.trim();
|
|
if (!s) return "SN";
|
|
const clean = s.replace(/[^a-zA-Z0-9]+/g, " ").trim();
|
|
const parts = clean ? clean.split(/\s+/) : [s];
|
|
const a = (parts[0] || s).charAt(0) || "S";
|
|
const b = (parts[1] || "").charAt(0) || ((parts[0] || s).charAt(1) || "N");
|
|
return (a + b).toUpperCase();
|
|
}
|
|
|
|
function hashString(str: string): number {
|
|
let hash = 2166136261;
|
|
for (let i = 0; i < str.length; i++) {
|
|
hash ^= str.charCodeAt(i);
|
|
hash = (hash * 16777619) >>> 0;
|
|
}
|
|
return hash >>> 0;
|
|
}
|
|
|
|
function hslToRgb(h: number, s: number, l: number): string {
|
|
s /= 100;
|
|
l /= 100;
|
|
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
const m = l - c / 2;
|
|
let r = 0, g = 0, b = 0;
|
|
|
|
if (0 <= h && h < 60) {
|
|
r = c; g = x; b = 0;
|
|
} else if (60 <= h && h < 120) {
|
|
r = x; g = c; b = 0;
|
|
} else if (120 <= h && h < 180) {
|
|
r = 0; g = c; b = x;
|
|
} else if (180 <= h && h < 240) {
|
|
r = 0; g = x; b = c;
|
|
} else if (240 <= h && h < 300) {
|
|
r = x; g = 0; b = c;
|
|
} else if (300 <= h && h < 360) {
|
|
r = c; g = 0; b = x;
|
|
}
|
|
r = Math.round((r + m) * 255);
|
|
g = Math.round((g + m) * 255);
|
|
b = Math.round((b + m) * 255);
|
|
return `rgb(${r}, ${g}, ${b})`;
|
|
}
|
|
|
|
export function getLogoGradient(name: string): { color1: string; color2: string } {
|
|
const h = hashString(name);
|
|
const hue = h % 360;
|
|
const hue2 = (hue + 36 + (h % 24)) % 360;
|
|
return {
|
|
color1: hslToRgb(hue, 85, 58),
|
|
color2: hslToRgb(hue2, 85, 48),
|
|
};
|
|
}
|