Elffuss BraidHub

kikocis/VibeCodeViewer public

9becfbfdcf77 · 3.9 KiBRawPermalink
1// Modos de coloreado: basic (estado actual), git (estado del fichero según
2// git) y ownership (un color por code owner). El color del fichero se
3// propaga atenuado a sus contenedores: habitación, planta, edificio, barrio…
4// Los colores base por rol salen del tema activo.
5
6export const GIT_STATUS = {
7 M: { label: 'Modificado', color: [0.95, 0.65, 0.10] },
8 A: { label: 'Añadido (staged)', color: [0.15, 0.85, 0.45] },
9 U: { label: 'Sin seguimiento', color: [0.20, 0.90, 0.30] },
10 R: { label: 'Renombrado', color: [0.30, 0.55, 1.00] },
11 D: { label: 'Borrado', color: [1.00, 0.25, 0.25] },
12};
13
14function lerp3(a, b, t) { return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t]; }
15
16function hsl(h, s, l) {
17 const f = (n) => {
18 const k = (n + h / 30) % 12;
19 return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
20 };
21 return [f(0), f(8), f(4)];
22}
23
24// paleta de owners: ángulo áureo para hues bien separados; saturación y
25// luminosidad ajustables por tema para que casen con su esquema
26export function ownerColor(i, theme) {
27 const p = theme?.ownerHsl ?? {};
28 return hsl((i * 137.5 + (p.offset ?? 24)) % 360, p.s ?? 0.85, p.l ?? 0.55);
29}
30
31// agrega por nodo: total de ficheros y recuento por clave (estado u owner)
32function aggregate(model, keyOf) {
33 const data = new Map();
34 (function walk(node) {
35 const acc = { counts: new Map(), total: 0 };
36 for (const fid of node.fileIds) {
37 acc.total++;
38 const k = keyOf(model.files[fid]);
39 if (k != null) acc.counts.set(k, (acc.counts.get(k) ?? 0) + 1);
40 }
41 for (const c of node.children) {
42 const sub = walk(c);
43 acc.total += sub.total;
44 for (const [k, v] of sub.counts) acc.counts.set(k, (acc.counts.get(k) ?? 0) + v);
45 }
46 data.set(node.id, acc);
47 return acc;
48 })(model.city);
49 return data;
50}
51
52function dominantTint(acc, colorOf) {
53 if (!acc || !acc.total || !acc.counts.size) return null;
54 let bestK = null, bestN = 0, mix = [0, 0, 0], n = 0;
55 for (const [k, v] of acc.counts) {
56 if (v > bestN) { bestN = v; bestK = k; }
57 const c = colorOf(k);
58 mix[0] += c[0] * v; mix[1] += c[1] * v; mix[2] += c[2] * v; n += v;
59 }
60 void bestK;
61 return { color: [mix[0] / n, mix[1] / n, mix[2] / n], frac: n / acc.total };
62}
63
64export function computeMode(model, mode, theme) {
65 const ROLE_COLOR = theme.roleColor;
66 // colores de estado git del tema (caen al estándar si no los define)
67 const gitColor = (st) => theme.gitColors?.[st] ?? GIT_STATUS[st].color;
68 if (mode === 'git' && model.hasGit) {
69 const agg = aggregate(model, f => f.git?.st ?? null);
70 const present = new Set(model.files.map(f => f.git?.st).filter(Boolean));
71 return {
72 fileColor: (f) => {
73 const st = f.git?.st;
74 return st ? gitColor(st).map(c => c * 0.85) : null;
75 },
76 nodeColor: (node) => {
77 const t = dominantTint(agg.get(node.id), k => gitColor(k));
78 if (!t) return null;
79 const base = ROLE_COLOR[node.role] ?? ROLE_COLOR.cajon;
80 return lerp3(base, t.color.map(c => c * 0.55), 0.25 + 0.55 * t.frac);
81 },
82 legend: [...present].map(st => ({ label: GIT_STATUS[st].label, color: gitColor(st) })),
83 };
84 }
85 if (mode === 'ownership' && model.owners.length) {
86 const agg = aggregate(model, f => f.owner);
87 const counts = model.owners.map((_, i) =>
88 model.files.reduce((a, f) => a + (f.owner === i ? 1 : 0), 0));
89 return {
90 fileColor: (f) => (f.owner != null ? ownerColor(f.owner, theme).map(c => c * 0.8) : null),
91 nodeColor: (node) => {
92 const t = dominantTint(agg.get(node.id), (i) => ownerColor(i, theme));
93 if (!t) return null;
94 const base = ROLE_COLOR[node.role] ?? ROLE_COLOR.cajon;
95 return lerp3(base, t.color.map(c => c * 0.5), 0.3 + 0.5 * t.frac);
96 },
97 legend: model.owners.map((o, i) => ({
98 label: `${o} · ${counts[i]}`, color: ownerColor(i, theme),
99 })),
100 };
101 }
102 return { fileColor: null, nodeColor: null, legend: null };
103}