Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectIdentity.ts

43 lines1.4 KB

1/** Visual identity tokens for a generated project badge. */
2export interface ProjectIdentity {
3 readonly monogram: string;
4 readonly background: string;
5 readonly highlight: string;
6}
7​
8function normalizeProjectName(projectName: string): string {
9 return projectName.normalize("NFKC").trim();
11​
12function projectMonogram(projectName: string): string {
13 const words = normalizeProjectName(projectName).match(/[\p{L}\p{N}]+/gu) ?? [];
14 const firstWord = words[0];
15 if (!firstWord) return "PR";
16​
17 const glyphs = Array.from(firstWord);
18 const first = glyphs[0] ?? "P";
19 const second =
20 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
21 (words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
22 first;
23 return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
25​
26function projectHue(projectName: string): number {
27 const seed = normalizeProjectName(projectName).toLocaleLowerCase("en-US") || "project";
28 let hue = 0;
29 for (const glyph of seed) {
30 hue = (hue * 31 + (glyph.codePointAt(0) ?? 0)) % 360;
31 }
32 return hue;
34​
35/** Derives the stable monogram and generated colors used when a project has no icon. */
36export function deriveProjectIdentity(projectName: string): ProjectIdentity {
37 const hue = projectHue(projectName);
38 return {
39 monogram: projectMonogram(projectName),
40 background: `hsl(${hue} 48% 36%)`,
41 highlight: `hsl(${(hue + 24) % 360} 58% 48%)`,
42 };