Files
t3code/apps/web/src/projectIdentity.ts
T

44 lines
1.5 KiB
TypeScript

import { PROJECT_ICON_COLORS } from "./projectIconColors";
import type { ProjectIconColor } from "@t3tools/contracts";
/** Visual identity tokens for a generated project badge. */
export interface ProjectIdentity {
readonly monogram: string;
readonly color: ProjectIconColor;
}
function normalizeProjectName(projectName: string): string {
return projectName.normalize("NFKC").trim();
}
function projectMonogram(projectName: string): string {
const words = normalizeProjectName(projectName).match(/[\p{L}\p{N}]+/gu) ?? [];
const firstWord = words[0];
if (!firstWord) return "PR";
const glyphs = Array.from(firstWord);
const first = glyphs[0] ?? "P";
const second =
glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
(words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
first;
return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
}
function projectColor(projectName: string): ProjectIconColor {
const seed = normalizeProjectName(projectName).toLocaleLowerCase("en-US") || "project";
let index = 0;
for (const glyph of seed) {
index = (index * 31 + (glyph.codePointAt(0) ?? 0)) % PROJECT_ICON_COLORS.length;
}
return PROJECT_ICON_COLORS[index]?.value ?? "blue";
}
/** Derives the stable monogram and generated colors used when a project has no icon. */
export function deriveProjectIdentity(projectName: string): ProjectIdentity {
return {
monogram: projectMonogram(projectName),
color: projectColor(projectName),
};
}