colors.ts 2.4 KB
/**
 * 配色。
 *
 * 类型配色对齐本体闭集(17 类 + 其他),不再像 v1 那样面对 41 种自由类型只能
 * 靠 hash 生成随机色 —— 那样相邻类型经常撞色,图例也读不出来。
 * 社区配色另起一套,用于"按社区着色"视图。
 */
export const TYPE_COLORS: Record<string, string> = {
  品牌: "#6366f1",
  集团: "#8b5cf6",
  门店: "#0ea5e9",
  组织角色: "#84cc16",
  协议: "#f59e0b",
  指标: "#ec4899",
  口径: "#14b8a6",
  周期: "#ef4444",
  产品: "#22c55e",
  数据源: "#3b82f6",
  数仓层: "#64748b",
  分析模型: "#a855f7",
  策略: "#f97316",
  业务规则: "#d946ef",
  流程: "#06b6d4",
  功能项: "#0891b2",
  任务: "#10b981",
  其他: "#94a3b8",
};

// 色相均匀分布、明度统一的定性色板,保证在浅色/深色底上都可辨
export const COMMUNITY_COLORS = [
  "#6366f1", "#f97316", "#14b8a6", "#ec4899", "#84cc16", "#0ea5e9",
  "#a855f7", "#f59e0b", "#22c55e", "#ef4444", "#06b6d4", "#d946ef",
  "#3b82f6", "#eab308", "#10b981", "#f43f5e",
];

/**
 * HSL → #rrggbb。
 *
 * 必须转成十六进制再交给 Sigma:它的 WebGL 着色器用自己的解析器处理颜色,
 * 只认 #hex / rgb() / rgba(),遇到 `hsl(...)` 会静默退化成黑色 ——
 * 表现就是图上莫名其妙出现一堆黑点。
 */
export function hslToHex(h: number, s: number, l: number): string {
  const a = (s / 100) * Math.min(l / 100, 1 - l / 100);
  const f = (n: number) => {
    const k = (n + h / 30) % 12;
    const v = l / 100 - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
    return Math.round(255 * v).toString(16).padStart(2, "0");
  };
  return `#${f(0)}${f(8)}${f(4)}`;
}

export function typeColor(t?: string | null): string {
  if (!t) return TYPE_COLORS["其他"];
  if (TYPE_COLORS[t]) return TYPE_COLORS[t];
  let h = 0;
  for (const c of t) h = (h * 31 + c.charCodeAt(0)) % 360;
  return hslToHex(h, 58, 55);
}

export function communityColor(c?: number | null): string {
  if (c == null) return "#9ca3af";
  return COMMUNITY_COLORS[c % COMMUNITY_COLORS.length];
}

/** 用于 hover 时把无关节点/边淡化 —— LightRAG 的标志性交互 */
export function dim(hex: string, alpha = 0.12): string {
  const m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  if (!m) return `rgba(148,163,184,${alpha})`;
  const [r, g, b] = [1, 2, 3].map((i) => parseInt(m[i], 16));
  return `rgba(${r},${g},${b},${alpha})`;
}