Elffuss BraidHub

kikocis/VibeCodeViewer public

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