Skip to content

World's first open-source enterprise world model.

The interface has a light side (#599)

WaylandYang2767877ada80
diff --git a/crates/utopia-store/src/palette.rs b/crates/utopia-store/src/palette.rs
index c093e720..794341a4 100644
--- a/crates/utopia-store/src/palette.rs
+++ b/crates/utopia-store/src/palette.rs
@@ -27,7 +27,7 @@
/// 拿真实的图一看就否了:低饱和加中明度是为纸面调的,撞上近黑的画布会整片发灰,
/// 类与类之间分不开。深色底需要的是饱和度撑得住的颜色。
///
-/// **改这里就得同步改 `web/src/ui/index.tsx` 的 `ENTITY_PALETTE`**:
+/// **改这里就得同步改 `web/src/palette.ts` 的 `ENTITY_PALETTE`**:
/// 手动挑的色和自动取的色必须来自同一组,否则一张图里会出现两套配色。
/// 有测试盯着(见本文件末尾),改漏了会红。
pub const ENTITY_PALETTE: &[&str] = &[
@@ -138,7 +138,7 @@ mod tests {
/// 按 char code 遍历算出来就不一样了。
#[test]
fn the_frontend_has_a_matching_hash() {
- let ts = include_str!("../../../web/src/ui/index.tsx");
+ let ts = include_str!("../../../web/src/palette.ts");
assert!(
ts.contains("export function colorForKey"),
"前端缺 colorForKey——新建类的颜色就会跟后端对不上"
@@ -161,7 +161,7 @@ mod tests {
/// 界面上选得到、建的时候报 kind 不合法,只有端到端会撞上)。
#[test]
fn the_frontend_palette_matches_this_one() {
- let ts = include_str!("../../../web/src/ui/index.tsx");
+ let ts = include_str!("../../../web/src/palette.ts");
let start = ts
.find("export const ENTITY_PALETTE")
.expect("前端找不到 ENTITY_PALETTE");
diff --git a/docs/decisions/0038-the-interface-has-a-light-side.md b/docs/decisions/0038-the-interface-has-a-light-side.md
new file mode 100644
index 00000000..a5b4cc82
--- /dev/null
+++ b/docs/decisions/0038-the-interface-has-a-light-side.md
@@ -0,0 +1,133 @@
+# 0038 · The interface has a light side
+
+- **Status**: Implemented (#599) · a `data-theme` on `<html>`, a second token block in
+ `styles.css`, the canvas reads its colours from the tokens and re-reads them on a switch,
+ a `raw-colour` rule in the style guard · not in this cut: an entity palette tuned for
+ paper (the data colours are the same in both themes and must stay equal to the server's
+ copy), a theme choice that follows the account across browsers
+- **Written**: 2026-09-11 (conventions in the [README](README.md))
+- **Related**: [0004](0004-language-and-localization.md) put the reader's language in the
+ reader's hands; the theme is the same kind of choice and lives in the same place, the
+ browser. `web/DESIGN.md` rule 4 said colour belongs to data and the chrome is grey; this
+ record is what it took to make that sentence true when the grey flips.
+
+> The chrome was dark from the first screen, and was written as if that were the only case.
+> Forty-odd values sat outside the token block: `#ffffff` in a page rule, `rgba(255,255,255,
+> 0.08)` in a constant that feeds the graph renderer, a shadow string in a component. Each
+> was a small statement of "the ground is dark". A light theme done by changing tokens alone
+> was not possible, because the values were not all tokens. An enterprise floor is lit, and
+> the people on it look at a screen for eight hours; the interface should have a face for
+> that room.
+
+## The problem
+
+Three things were coupled that should not have been.
+
+**Values and rules.** `styles.css` defined the tokens once and then, in a hundred rules
+below, sometimes used them and sometimes wrote the number again. A rule that says
+`rgba(255, 255, 255, 0.06)` is correct on dark ground and invisible on paper, and there is
+no way to find it except by looking.
+
+**Chrome and canvas.** The graph is drawn by sigma.js on WebGL and by hand on a 2D canvas
+(the grid, the label slabs, the login scene). Neither can resolve `var(--u-line)`; they take
+a colour string. So the canvas colours were constants in TypeScript, computed at module
+load, and baked into the graph's node and edge attributes when the graph was built. A
+stylesheet change did nothing to them.
+
+**Alpha and colour.** The dark chrome is layers of white at low alpha over near-black. The
+hierarchy — a line is `0.08`, a strong line `0.14`, a surface `0.03` — is carried by the
+alpha, and the alpha was written together with the white it multiplied. On paper the same
+hierarchy is layers of black at the same alphas. The number that should survive the flip was
+fused to the number that should not.
+
+## Dead ends
+
+**A `prefers-color-scheme` block.** The obvious shape is two media-query blocks in CSS and
+no JavaScript. It fails on the first requirement: the choice is the reader's, not the
+operating system's, and a person on a light desktop may want the dark interface. A CSS block
+per source of truth (`data-theme` for the choice, the media query for "system") would be the
+same forty tokens written twice more, and two copies of a token set fork within a month.
+The stylesheet knows one attribute, `data-theme`, with two values; "system" is resolved to
+one of the two in JavaScript, once at start and again when the OS preference changes.
+
+**Rebuilding the canvas colours by hand.** The first pass read the tokens once before
+building the graph, and the theme switch worked on the chrome and not on the graph: the
+colours were already in the graph attributes. Re-reading the tokens is not enough; the graph
+has to be rebuilt from them. The switch now bumps a counter that the build effect depends
+on, so the same code path that builds the graph on data change builds it on theme change.
+
+**Trusting alpha on WebGL.** With the chrome and the graph attributes both reading tokens,
+the edges on paper came out *white*. sigma's edge shader blends with premultiplied alpha but
+writes un-premultiplied colour, which means an edge at alpha can only brighten what is under
+it, never darken it. On a dark ground that is invisible; on paper every edge is a pale line.
+The fix is not in the shader: the edge colours are flattened over the ground colour at read
+time and handed to sigma opaque. The light-theme edge tokens are therefore written as "how
+deep after flattening", not as an alpha the screen will see; the comment beside them says
+so.
+
+**A guard that only read `.tsx`.** The style guard scanned page files. The grid colour was a
+hard-coded white in a `.ts` file that the guard did not open, and it stayed white on paper
+until someone looked. The guard now walks `.ts` as well, ignores block comments (so a rule
+can be quoted where it is explained), and skips only the two files that are allowed to hold
+a value.
+
+## Decisions
+
+1. **Dark is the default; light is a choice; system is a third.** Stored as `utopia.theme`
+ in the browser, never sent to the backend. A script in `index.html` runs before first
+ paint and sets `data-theme` from storage, so a refresh does not flash dark before turning
+ light. The menu under the avatar offers the three.
+
+2. **Only the tokens change.** `:root[data-theme="light"]` redefines every token that carries
+ a colour and nothing else; not one rule below the token blocks is rewritten. Ground is
+ paper (`#fafafa`), not white; text is near-black, not black; lines and surfaces go from
+ white-at-alpha to black-at-alpha with the alpha unchanged. Semantic colours (ok, warn,
+ danger, contest, derived) become a deeper shade of the same hue, because the pale ones
+ built for dark ground cannot be read on paper.
+
+3. **Alpha describes hierarchy; the triplet is what the theme swaps.** A shade of white or
+ black in a rule is `rgba(var(--u-ink-rgb), α)` or `rgba(var(--u-ground-rgb), α)`. The
+ `-rgb` triplets exist for this: they let a rule keep its alpha and let the theme keep the
+ colour. The rgb-triplet tokens for the semantic colours exist for the same reason.
+
+4. **The canvas has one reader.** `pages/graphVisuals.ts` reads the tokens the canvas needs
+ through `getComputedStyle`, exports them as live bindings, and re-reads them in
+ `refreshPalette()` before every graph build and on every theme change. The graph pages
+ subscribe to the switch, refresh the palette, rebuild the graph, push the label colours
+ into sigma's settings (they are captured at construction, not read per frame), and redraw
+ the grid. Edge colours pass through the flattening above when the theme is light.
+
+5. **A colour value has two homes.** The token blocks in `styles.css`, and the two reader
+ files (`graphVisuals.ts`, and `palette.ts` for the entity colours, which are data and must
+ stay byte-identical to `palette.rs` on the server). The `raw-colour` rule in the style
+ guard fails CI on a `#rrggbb` or `rgba(…)` anywhere else in `web/src`, `.ts` and `.tsx`
+ alike; `rgba(0,0,0,0)` is transparency, not a colour, and passes. `styles.css` itself
+ holds no value outside its token blocks; that is checked by eye, not by the guard, and is
+ the one gap here.
+
+6. **The entity palette does not change with the theme.** Colour belongs to data (rule 4),
+ and the same entity type must be the same colour on every screen and in every export.
+ The palette was tuned on dark ground and some hues are pale on paper; a paper-tuned set
+ would have to change the server's copy too, and that is a separate decision.
+
+## What the look found (2026-09-11)
+
+The first build of this cut passed the guard, the type check and the tests, and was looked
+at in both themes before it merged. Four things were wrong that no check could have caught:
+the grid was gone on paper (a white line in an exempt file), the edges were white (the
+shader, above), the labels on the graph followed the old theme (captured settings), and a
+small graph stopped showing its predicates after the switch — the label threshold was
+written for a large graph at a zoomed-out ratio, and a graph of nine edges at ratio 1 should
+show them regardless. The threshold now has a size clause. Each of these is why the merge
+rule for interface work is "look first" and not "green first".
+
+## Open questions
+
+- **A paper palette.** Whether the entity colours get a second set for light ground, held on
+ both the client and the server, or the dark set is adjusted once so that it reads on both.
+- **Following the account.** The theme is a property of the browser today. If a reader moves
+ between machines and expects the choice to follow, it becomes a user setting on the
+ backend; language will face the same question first (0004).
+- **`styles.css` under the guard.** The token blocks are the only place a value may appear
+ in the stylesheet; nothing enforces it. A rule that scans the stylesheet below its token
+ blocks is a small addition when it is next touched.
diff --git a/docs/decisions/README.md b/docs/decisions/README.md
index d5538f9a..54218dd8 100644
--- a/docs/decisions/README.md
+++ b/docs/decisions/README.md
@@ -61,6 +61,7 @@ The test for writing one: if someone (including us) looks at a piece of code in
| 0035 | [A vector index is built by a job](0035-a-vector-index-is-built-by-a-job.md) | Implemented · a partial HNSW index per dimension on `chunks.embedding` and `entities.profile_embedding`, requested by the first write of that dimension and built by a `build_vector_index` job outside any transaction · the two nearest-neighbour reads write the dimension as a literal, cast both sides and set `hnsw.iterative_scan = relaxed_order` · type resolution gathers its neighbours eight at a time in order and remembers descendant sets per batch (#512, #514) · dimensions above 2000 stay on the exact path |
| 0036 | [Exploration aligns a schema to the ontology](0036-exploration-aligns-a-schema-to-the-ontology.md) | Written, not implemented · exploration proposes an alignment per table (the class it is a table of, its attributes, its relations, a conversion tree per column) through `ontology_proposals`, adopted as one thing · a definition is a rule over aligned attributes, written by a person; a shared convention is one rule others read · conversions are 0032's expression tree, text parsed by `sqlparser`, never stored as SQL · `Metric` / `Dimension` retire and `concept_mappings` becomes rendered · the schema document leaves extraction · where a rule runs against the source stays open |
| 0037 | [A relation carries its own attributes](0037-a-relation-carries-its-own-attributes.md) | Written · cut 1 in progress: qualifier tables (0049), declaration, extraction, panel, export · entity-valued qualifier reserved, conflict-as-two-rows and the canvas label next |
+| 0038 | [The interface has a light side](0038-the-interface-has-a-light-side.md) | Implemented (#599) · `data-theme` on `<html>`, chosen by the reader and stored in the browser, `system` resolved to one of the two before first paint · a second token block in `styles.css` and not one rule rewritten: alpha stays, the triplet swaps · the canvas reads its colours from the tokens through one reader file and rebuilds on a switch; edges are flattened over the ground on light because the WebGL shader can only brighten · a `raw-colour` guard rule over `.ts` and `.tsx` · the entity palette is the same in both themes; a paper-tuned set is open |
## Not a decision record
diff --git a/web/DESIGN.md b/web/DESIGN.md
index 924d9844..0ea1874a 100644
--- a/web/DESIGN.md
+++ b/web/DESIGN.md
@@ -30,6 +30,8 @@ Text is `text-ink` or `text-ink-2` — two levels: the content, and what is said
Glass is a surface treatment, not a colour: `glass` for a panel in peripheral vision, `glass-strong` for one being read, and both go solid under the pointer (see the note above `--u-surface-strong-hover`). A page does not write `backdrop-blur`.
+The same rule holds outside class names. A colour value — `#rrggbb`, `rgba(…)` — does not appear in a `.ts` or `.tsx` file, nor in a rule in `styles.css`: every value lives in one of the token blocks at the top of `styles.css`, once for dark (`:root`) and once for light (`:root[data-theme="light"]`), and a page never knows which theme it is in. Two files are the readers, and the only ones allowed to hold a value: `pages/graphVisuals.ts`, which reads the tokens the canvas needs through `getComputedStyle` (canvas cannot resolve `var()`) and re-reads them when the theme changes; and `palette.ts`, the entity colours, which are data (rule 4 allows them) and must stay byte-identical to `crates/utopia-store/src/palette.rs`. A shade of white or black is `rgba(var(--u-ink-rgb), α)` / `rgba(var(--u-ground-rgb), α)` with the alpha left where it was — alpha describes hierarchy, the triplet is what the theme swaps. `rgba(0,0,0,0)` is transparency, not a colour, and passes.
+
## 5. State lives in the component
Hover, focus, active, disabled and motion are defined once, in `web/src/ui/`, and a page never writes `hover:`, `focus:`, `transition` or `duration-`. Every control shows a visible focus ring for keyboard users (`--u-ring`); every disabled control is `opacity-40` with `cursor-not-allowed`; every hover settles in `--u-fast` (120 ms) and leaves in `--u-base` (260 ms). A page that needs a control that does not exist adds it to `ui/`, with all five states, and then uses it.
@@ -56,7 +58,7 @@ A floating panel **shows**; it does not edit. A class, a property, an entity, a
## How this is enforced
-`web/scripts/style-guard.mjs` scans `web/src/**/*.tsx` for the patterns above and fails CI on any hit. It runs first in `pnpm build`. While the pages were being migrated, `web/style-guard.baseline.json` listed the ones not yet done; every page passes now and the file is gone. A new file is checked from its first commit.
+`web/scripts/style-guard.mjs` scans `web/src/**/*.{ts,tsx}` for the patterns above and fails CI on any hit (the `web` job runs `pnpm build`, and the guard runs first in it). Block comments are ignored, so a rule can be quoted where it is explained. The `raw-colour` rule skips the two reader files named in rule 4 and `*.test.ts(x)` fixtures. While the pages were being migrated, `web/style-guard.baseline.json` listed the ones not yet done; every page passes now and the file is gone. A new file is checked from its first commit.
## Migration order
diff --git a/web/index.html b/web/index.html
index b5aecf3f..bc2d63ce 100644
--- a/web/index.html
+++ b/web/index.html
@@ -10,6 +10,17 @@
href="https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700&family=Inter:wght@400;500;600;700&family=Marcellus&display=swap"
/>
<title>Utopia</title>
+ <!-- 首帧前定主题:与 src/theme.ts 同一套解法。放在 head 里、样式之前,
+ 刷新时才不会先闪一下暗底再变浅 -->
+ <script>
+ (function () {
+ var t = "dark";
+ try { t = localStorage.getItem("utopia.theme") || "dark"; } catch (e) {}
+ if (t === "system")
+ t = window.matchMedia && matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
+ document.documentElement.dataset.theme = t === "light" ? "light" : "dark";
+ })();
+ </script>
</head>
<body>
<div id="root"></div>
diff --git a/web/scripts/style-guard.mjs b/web/scripts/style-guard.mjs
index 06526f9c..3831d127 100644
--- a/web/scripts/style-guard.mjs
+++ b/web/scripts/style-guard.mjs
@@ -103,6 +103,19 @@ const RULES = [
why: "hover / focus / disabled / 动效在组件里定一次,页面不写(规矩 5)",
ui: false,
},
+ {
+ id: "raw-colour",
+ // 页面和组件里不出现色值本身:令牌在 styles.css 里定义一次,浅色/暗色各一套,
+ // 写死一个 #ffffff 就是写死了「暗底」。两个例外文件是**令牌的读者**:
+ // graphVisuals.ts 从 CSS 变量里把画布要的颜色读出来(canvas 不认 var()),
+ // palette.ts 是实体类型的数据色(规矩 4:彩色只属于数据),暗浅两套都在那里。
+ // `rgba(0,0,0,0)` 是透明,不是颜色,放行
+ re: /#[0-9a-fA-F]{6}(?![0-9a-fA-F])|(?<![a-zA-Z_])rgba?\(/g,
+ why: "颜色只用令牌(规矩 4);画布从 graphVisuals 的调色板读,数据色在 palette.ts",
+ ui: true,
+ skip: (rel) => /src\/(pages\/graphVisuals\.ts|palette\.ts)$/.test(rel) || /\.test\.tsx?$/.test(rel),
+ allow: /rgba\(0,\s*0,\s*0,\s*0\)/g,
+ },
{
id: "native-confirm",
re: /\bwindow\.(confirm|alert)\(|(?<![.\w])(confirm|alert)\(/g,
@@ -115,7 +128,7 @@ function walk(dir, out = []) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) walk(full, out);
- else if (entry.name.endsWith(".tsx")) out.push(full);
+ else if (entry.name.endsWith(".tsx") || entry.name.endsWith(".ts")) out.push(full);
}
return out;
}
@@ -137,11 +150,37 @@ for (const rel of files) {
m.replace(/[^\n]/g, " "),
);
const lines = text.split("\n");
+ // 块注释里的行也不算:/* … */ 跨行时逐行看不出自己在注释里,这里记个状态
+ let inBlock = false;
+ const commentFree = lines.map((line) => {
+ let out = "";
+ let i = 0;
+ while (i < line.length) {
+ if (inBlock) {
+ const end = line.indexOf("*/", i);
+ if (end < 0) return out;
+ inBlock = false;
+ i = end + 2;
+ } else {
+ const start = line.indexOf("/*", i);
+ if (start < 0) {
+ out += line.slice(i);
+ break;
+ }
+ out += line.slice(i, start);
+ inBlock = true;
+ i = start + 2;
+ }
+ }
+ return out;
+ });
for (const rule of RULES) {
if (inUi && !rule.ui) continue;
- lines.forEach((line, i) => {
+ if (rule.skip && rule.skip(rel)) continue;
+ commentFree.forEach((line, i) => {
// 注释里提到旧写法不算(规矩要能在注释里被引用)
- const code = line.replace(/\/\/.*$/, "").replace(/\{\/\*.*?\*\/\}/g, "");
+ let code = line.replace(/\/\/.*$/, "");
+ if (rule.allow) code = code.replace(rule.allow, "");
const hits = code.match(rule.re);
if (!hits) return;
failures += 1;
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index 7f3bfc90..c981b69a 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -111,6 +111,8 @@ export const en = {
passwordChanged: "Password updated",
avatarHint: "Avatars are generated from your name for now.",
language: "Language",
+ theme: "Theme",
+ themeNames: { dark: "Dark", light: "Light", system: "Follow system" } as Record<"dark" | "light" | "system", string>,
kbsNav: "Knowledge bases",
kbsTitle: "Knowledge bases",
kbsFilter: "Filter by name…",
diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts
index 331a2f8c..c24c8002 100644
--- a/web/src/i18n/zh.ts
+++ b/web/src/i18n/zh.ts
@@ -100,6 +100,8 @@ export const zh: Strings = {
passwordChanged: "密码已更新",
avatarHint: "头像目前由你的名字生成。",
language: "语言",
+ theme: "主题",
+ themeNames: { dark: "深色", light: "浅色", system: "跟随系统" } as Record<"dark" | "light" | "system", string>,
kbsNav: "知识库",
kbsTitle: "知识库",
kbsFilter: "按名字筛选…",
diff --git a/web/src/main.tsx b/web/src/main.tsx
index 3986e24a..b8a6b849 100644
--- a/web/src/main.tsx
+++ b/web/src/main.tsx
@@ -5,8 +5,12 @@ import { RouterProvider } from "@tanstack/react-router";
import { router } from "./router";
import { ToastHost } from "./toast";
import { DEFAULT_STALE_MS, applyQueryDefaults } from "./queryDefaults";
+import { initTheme } from "./theme";
import "./styles.css";
+// 主题先落到 <html> 再渲染:画布第一次读令牌时它得已经是对的
+initTheme();
+
// staleTime 按键分档,表在 queryDefaults.ts;这里只给全局默认(#518)
const queryClient = new QueryClient({
defaultOptions: {
diff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsx
index b51c3940..c10485ba 100644
--- a/web/src/pages/Graph.tsx
+++ b/web/src/pages/Graph.tsx
@@ -16,18 +16,32 @@ import FA2Layout from "graphology-layout-forceatlas2/worker";
import Sigma from "sigma";
import EdgeCurveProgram from "@sigma/edge-curve";
import { EdgeRectangleProgram } from "sigma/rendering";
+import { onThemeChange } from "../theme";
import {
- drawWorldGrid,
+ EDGE,
+ EDGE_CONTEST,
+ EDGE_DERIVED,
+ EDGE_DERIVED_DIM,
+ EDGE_DIM,
+ EDGE_FOCUS,
+ EDGE_FOCUS_CONTEST,
+ EDGE_FOCUS_DERIVED,
+ EDGE_INFERRED,
HOVER_MUTE,
- lerpColor,
- mix,
MUTED_SHELL,
NODE_BORDER_BASE,
NODE_CORE_BASE,
NODE_CORE_MIX,
NODE_SHELL_BASE,
NODE_TINT_MIX,
+ SCRUB_FUTURE,
+ SCRUB_PAST,
+ SCRUB_PLAY,
TRANSPARENT,
+ drawWorldGrid,
+ lerpColor,
+ mix,
+ refreshPalette,
} from "./graphVisuals";
// 画布那台机器是两页共用的(#496):构造选项、状态表、相机、拖拽都在那边,
// 这个文件只管把实例与事实投影成一张图、说清楚每个节点是什么颜色
@@ -103,20 +117,6 @@ import { usePopoverFlip } from "../ui/popoverFlip";
import { useKb, useKbId } from "../kb";
import { toast } from "../toast";
-const EDGE_COLOR = "rgba(163,163,163,0.2)"; // 纯灰(应用户要求,不用钢蓝)
-// 本体没认下的关系:同色更淡。名字来自原文,不该跟词表里的关系看着一样重
-const EDGE_COLOR_INFERRED = "rgba(163,163,163,0.1)";
-// 推出来的边(R1)。**跟上面两者说的不是一件事**:那两个说「这条边的名字从哪来」,
-// 这个说「这条边根本不是谁说的,是引擎推的」。所以给它自己的色相而不是再淡一档灰——
-// 用户要在余光里就分得出「文档里写的」和「推出来的」
-const EDGE_COLOR_DERIVED = "rgba(231,197,124,0.42)";
-const EDGE_COLOR_DERIVED_DIM = "rgba(231,197,124,0.14)";
-/* 争议(0017 §3):珊瑚橙 `--u-contest`。金是派生、琥珀是警告、粉是危险,
- 它得跟三个都拉开。**整条边换色**——环在节点上、边还是灰的,余光分不出来 */
-const EDGE_COLOR_CONTEST = "rgba(255,106,61,0.55)";
-const EDGE_FOCUS_CONTEST = "rgba(255,106,61,1)";
-
-/** 相邻两条弧之间的曲率差。太小仍然糊,太大在长边上会甩得离节点很远 */
const EDGE_CURVATURE_STEP = 0.18;
/** 一条边画在哪条弧上;`curvature === 0` = 直线。 */
@@ -230,19 +230,11 @@ const LEGEND_MAX = 6;
——它只影响看得清还是拖得动,用户要的是「多点/少点」,不是 237 这个数。
最大值与后端 GRAPH_NODE_CAP_MAX 对齐;再高先垮的是拖动,不是清晰度 */
const NODE_BUDGETS: number[] = [150, 300, 600, 1000];
-// 注意:sigma 边着色器在预乘混合(ONE, ONE_MINUS_SRC_ALPHA)下不预乘 RGB,
-// alpha 无法压暗边——暗度必须编码进 RGB(不透明近背景色)
-const EDGE_DIM = "#141414";
-/* 幽灵边:没落地的派生。同一个色相往 EDGE_DIM 混(预乘混合下 alpha 压不暗边),
- 更细。sigma 默认的边程序画不了虚线,也不为此另写一个 */
-const EDGE_GHOST = lerpColor("rgba(255,106,61,1)", EDGE_DIM, 0.55);
-const EDGE_GHOST_FOCUS = lerpColor("rgba(255,106,61,1)", EDGE_DIM, 0.2);
-const EDGE_FOCUS = "rgba(255,255,255,0.55)";
-// 选中/悬停时的派生边。**不能跟着走白**:选中恰恰是看得最仔细的时候,
-// 而这时候「这条边是推出来的、没人写过」比任何时候都该说清楚。
-// 从前一律 EDGE_FOCUS,一选中金线就变白,等于把来历抹掉了。
-// 比常态的金更亮更实——它同样要表达「被选中了」
-const EDGE_FOCUS_DERIVED = "rgba(255,214,140,0.95)";
+/* 边色全部来自调色板(graphVisuals,读的是当前主题的令牌,0038)。
+ 幽灵边(没落地的派生)同一个色相往 EDGE_DIM 混——sigma 的边着色器在预乘混合
+ 下 alpha 压不暗边,暗度必须编码进 RGB——所以是函数,切主题后重算 */
+const edgeGhost = () => lerpColor(EDGE_FOCUS_CONTEST, EDGE_DIM, 0.55);
+const edgeGhostFocus = () => lerpColor(EDGE_FOCUS_CONTEST, EDGE_DIM, 0.2);
const DAY_MS = 24 * 3600 * 1000;
/* 播放淡入:解析 hex / rgb / rgba(含 alpha)并线性插值 */
@@ -259,6 +251,8 @@ export function Graph() {
const search = useSearch({ from: "/app/kb/$kbId/graph" });
const navigate = useNavigate();
const entityParam = search.entity;
+ // 主题一变,图要重构(颜色烤在属性里):见构图 effect
+ const [themeTick, setThemeTick] = useState(0);
const [focusEntity, setFocusEntity] = useState<string | null>(
search.focus ?? entityParam ?? null,
);
@@ -679,6 +673,9 @@ export function Graph() {
}, [timeT, recomputeActive]);
useEffect(() => {
+ // 节点壳色、边色是**烤进图属性**的,不是渲染时才取;所以构图前先把调色板
+ // 读成当前主题的值,切主题时靠 themeTick 让这里重跑一遍(0038)
+ refreshPalette();
if (!containerRef.current || !data.data) return;
const g = new Graphology({ multi: true });
for (const n of data.data.nodes) {
@@ -709,14 +706,14 @@ export function Graph() {
label: (e.contested ? "⚠ " : "") + (e.label ?? ""),
size: e.blocked ? 0.7 : 1,
color: e.blocked
- ? EDGE_GHOST
+ ? edgeGhost()
: e.contested
- ? EDGE_COLOR_CONTEST
+ ? EDGE_CONTEST
: e.derived
- ? EDGE_COLOR_DERIVED
+ ? EDGE_DERIVED
: e.inferred
- ? EDGE_COLOR_INFERRED
- : EDGE_COLOR,
+ ? EDGE_INFERRED
+ : EDGE,
contested: e.contested,
blocked: e.blocked,
// 独一条就走直线:曲线是为了把重叠分开,没有重叠就不必弯
@@ -870,6 +867,8 @@ export function Graph() {
};
sigmaRef.current?.kill();
+ // 画布颜色从令牌读(0038):建实例前读一次,切主题后再读一次并重画
+ refreshPalette();
const sigma = new Sigma(g, containerRef.current, {
...sigmaOptions({
defaultEdgeType: "line",
@@ -1031,11 +1030,11 @@ export function Graph() {
const ghost = attrs.blocked === true;
const from = !focused
? ghost
- ? EDGE_GHOST
- : EDGE_COLOR_DERIVED
+ ? edgeGhost()
+ : EDGE_DERIVED
: s === focused || t === focused
? ghost
- ? EDGE_GHOST_FOCUS
+ ? edgeGhostFocus()
: EDGE_FOCUS_DERIVED
: EDGE_DIM;
res.color = lerpColor(from, EDGE_DIM, k);
@@ -1044,10 +1043,10 @@ export function Graph() {
}
// 幽灵边不呼吸:它不是知识,是一条没走通的路
const pulse = attrs.blocked === true
- ? EDGE_GHOST
+ ? edgeGhost()
: lerpColor(
- EDGE_COLOR_DERIVED_DIM,
- EDGE_COLOR_DERIVED,
+ EDGE_DERIVED_DIM,
+ EDGE_DERIVED,
// 三角波而不是正弦:两端各停一瞬,看起来是「呼吸」不是「闪」
Math.abs(
((performance.now() % DERIVED_PULSE_MS) / DERIVED_PULSE_MS) * 2 -
@@ -1067,7 +1066,7 @@ export function Graph() {
const boost = () => {
res.color =
attrs.blocked === true
- ? EDGE_GHOST_FOCUS
+ ? edgeGhostFocus()
: attrs.contested === true
? EDGE_FOCUS_CONTEST
: isDerived
@@ -1137,6 +1136,12 @@ export function Graph() {
setFocusEntity(node);
setSelected(node);
});
+ const offTheme = onThemeChange(() => {
+ refreshPalette();
+ setThemeTick((t) => t + 1);
+ sigma.refresh();
+ if (gridRef.current) drawWorldGrid(gridRef.current, sigma);
+ });
sigma.on("clickStage", () => deselect());
sigma.on("enterNode", ({ node }) => {
hoverRef.current = node;
@@ -1167,7 +1172,9 @@ export function Graph() {
打上就绕开那条启发式 */
const updateEdgeLabels = () => {
const ratio = sigma.getCamera().ratio;
- sigma.setSetting("renderEdgeLabels", ratio < 0.7);
+ // 边少的图(≤ 40 条)始终写谓词:标签藏起来是为了大图不挤,
+ // 一个十几条边的库进来 ratio = 1,藏了只会让人以为谓词没了
+ sigma.setSetting("renderEdgeLabels", ratio < 0.7 || g.size <= 40);
const deep = ratio < 0.35;
if (deep !== deepZoomRef.current) {
deepZoomRef.current = deep;
@@ -1218,6 +1225,8 @@ export function Graph() {
}
recomputeActive(timeT);
return () => {
+ offTheme();
+
if (stabilizeTimer) clearTimeout(stabilizeTimer);
if (settleTimer) clearTimeout(settleTimer);
fa2?.kill();
@@ -1226,7 +1235,7 @@ export function Graph() {
sigmaRef.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [data.data]);
+ }, [data.data, themeTick]);
if (!kb)
return <div className="p-8 text-body text-ink-2">{S.nav.loading}</div>;
@@ -1614,7 +1623,7 @@ export function Graph() {
title={`${S.graph.derivedEdges(derivedCount)} · ${S.graph.derivedHint}`}
icon={<Waypoints size={15} />}
style={
- showDerived ? { color: "rgba(231,197,124,0.95)" } : undefined
+ showDerived ? { color: EDGE_FOCUS_DERIVED } : undefined
}
onClick={() => setShowDerived((v) => !v)}
/>
@@ -1958,7 +1967,7 @@ function TimeScrubber({
仍夹在视口内(calc 那一项),窄屏不会顶出去。
实测宽度:年 320 / 月 648 / 日 760。 */
<div
- className={`glass-strong absolute bottom-4 left-1/2 -translate-x-1/2 z-10 rounded-overlay px-3 py-2 flex items-center gap-3 shadow-[0_12px_40px_rgba(0,0,0,0.5)] u-scrub-island${playing ? " u-solid" : ""}`}
+ className={`glass-strong absolute bottom-4 left-1/2 -translate-x-1/2 z-10 rounded-overlay px-3 py-2 flex items-center gap-3 u-scrub-island${playing ? " u-solid" : ""}`}
style={{ width: `min(${trackW}px, calc(100vw - 4rem))` }}
>
<IconButton
@@ -2062,10 +2071,10 @@ function TimeScrubber({
// 归零等于假装那段没有数据,而它只是还没到
background:
value !== null && past && (playing || trackHover)
- ? "rgba(255,255,255,0.62)"
+ ? SCRUB_PLAY
: value === null || past
- ? "rgba(255,255,255,0.32)"
- : "rgba(255,255,255,0.04)",
+ ? SCRUB_PAST
+ : SCRUB_FUTURE,
}}
/>
</div>
diff --git a/web/src/pages/KbSettings.tsx b/web/src/pages/KbSettings.tsx
index c4a45ce7..1b0f7c4a 100644
--- a/web/src/pages/KbSettings.tsx
+++ b/web/src/pages/KbSettings.tsx
@@ -469,7 +469,7 @@ export function KbSettings() {
<Button variant="secondary" size="sm" className="shrink-0"
style={{
background: "var(--u-danger-solid)",
- color: "#ffffff",
+ color: "var(--u-on-danger)",
}}
onClick={() => setConfirmingDelete(true)}
>
diff --git a/web/src/pages/LoginScene.tsx b/web/src/pages/LoginScene.tsx
index 48e17cb7..edf81b4e 100644
--- a/web/src/pages/LoginScene.tsx
+++ b/web/src/pages/LoginScene.tsx
@@ -5,7 +5,7 @@
带一点由小到大的生长感。透视投影、结构大于视口(巨物应当出画)。
性能:形态几何只在切换时重算进 TypedArray;点按亮度分桶、每桶一次 fill;
DPR 封顶 1.5。纯中性白灰;prefers-reduced-motion 时静止单帧。 */
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
const U = 48; // 经向点数
const V = 26; // 纬向点数
@@ -57,7 +57,14 @@ function smoothstep(t: number): number {
return t * t * (3 - 2 * t);
}
+import { INK, inkAt, refreshPalette } from "./graphVisuals";
+import { onThemeChange } from "../theme";
+
export function LoginScene({ leaving }: { leaving?: boolean }) {
+ // 主题一变,粒子的墨色跟着变:重跑一遍挂载 effect(它预生成了亮度分桶的样式串)
+ const [themeTick, setThemeTick] = useState(0);
+ useEffect(() => onThemeChange(() => setThemeTick((t) => t + 1)), []);
+ refreshPalette();
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
@@ -114,7 +121,7 @@ export function LoginScene({ leaving }: { leaving?: boolean }) {
// 亮度分桶:预生成样式串,帧内无字符串拼接
const bucketStyle: string[] = [];
for (let b = 0; b < BUCKETS; b++)
- bucketStyle.push(`rgba(255,255,255,${(0.1 + (b / (BUCKETS - 1)) * 0.34).toFixed(3)})`);
+ bucketStyle.push(inkAt(+(0.1 + (b / (BUCKETS - 1)) * 0.34).toFixed(3)));
const buckets: number[][] = Array.from({ length: BUCKETS }, () => []);
// 原地闪烁:弱化为氛围层(主秀是下面的光脉冲)
@@ -281,7 +288,7 @@ export function LoginScene({ leaving }: { leaving?: boolean }) {
// 边:极淡的白,一次 stroke
ctx.lineWidth = 1;
- ctx.strokeStyle = "rgba(255,255,255,0.045)";
+ ctx.strokeStyle = inkAt(0.045);
ctx.beginPath();
for (let e = 0; e < edges.length; e += 2) {
const a = edges[e];
@@ -319,7 +326,7 @@ export function LoginScene({ leaving }: { leaving?: boolean }) {
}
// 氛围闪烁:尖峰脉冲(sin⁶),弱化版
- ctx.fillStyle = "rgba(255,255,255,0.92)";
+ ctx.fillStyle = inkAt(0.92);
for (let n = 0; n < TWINKLES; n++) {
const s = Math.sin(now * twSpeed[n] + twPhase[n]);
if (s <= 0) continue;
@@ -334,8 +341,8 @@ export function LoginScene({ leaving }: { leaving?: boolean }) {
}
// 光脉冲:头部亮点 + 沿走过的边渐隐的尾迹
- ctx.strokeStyle = "#ffffff";
- ctx.fillStyle = "#ffffff";
+ ctx.strokeStyle = INK;
+ ctx.fillStyle = INK;
ctx.lineWidth = 1.2;
for (const p of pulses) {
if (p.delay > 0 || p.fade <= 0) continue;
@@ -384,7 +391,7 @@ export function LoginScene({ leaving }: { leaving?: boolean }) {
cancelAnimationFrame(raf);
ro.disconnect();
};
- }, []);
+ }, [themeTick]);
return (
<canvas
diff --git a/web/src/pages/OntologySchemaGraph.tsx b/web/src/pages/OntologySchemaGraph.tsx
index 682d47b3..8c713703 100644
--- a/web/src/pages/OntologySchemaGraph.tsx
+++ b/web/src/pages/OntologySchemaGraph.tsx
@@ -24,6 +24,7 @@ import Graphology from "graphology";
import { circular } from "graphology-layout";
import forceAtlas2 from "graphology-layout-forceatlas2";
import Sigma from "sigma";
+import { onThemeChange } from "../theme";
import { EdgeArrowProgram, EdgeLineProgram } from "sigma/rendering";
import EdgeCurveProgram, { EdgeCurvedArrowProgram } from "@sigma/edge-curve";
import {
@@ -36,6 +37,23 @@ import {
NODE_TINT_MIX,
RING_SELECT_MIX,
TRANSPARENT,
+ EDGE_SUBCLASS,
+ EDGE_SUBCLASS_FOCUS,
+ EDGE_RELATION,
+ EDGE_RELATION_FOCUS,
+ EDGE_DISJOINT,
+ EDGE_DISJOINT_FOCUS,
+ EDGE_RULE,
+ EDGE_RULE_FOCUS,
+ EDGE_SCHEMA_DIM as EDGE_DIM,
+ LEGEND_SUBCLASS,
+ LEGEND_RELATION,
+ LEGEND_DISJOINT,
+ LEGEND_RULE,
+ INK,
+ refreshPalette,
+ CANVAS_TEXT,
+ CANVAS_TEXT_2,
} from "./graphVisuals";
// 画布那台机器是两页共用的(#496):构造选项、状态表、相机、拖拽都在那边,
// 这个文件只管把本体投影成一张图、说清楚每个节点是什么颜色
@@ -81,31 +99,6 @@ import { usePopoverFlip } from "../ui/popoverFlip";
看起来变暗(Graph.tsx 的 EDGE_DIM 处有同一条注释)。这里的边不需要
动画淡入淡出,所以不必再搬一套 lerp/parseRgba,几个状态各写一个
现成的颜色字面量就够了。 */
-const EDGE_SUBCLASS = "rgba(235,235,235,0.55)";
-const EDGE_SUBCLASS_FOCUS = "rgba(255,255,255,0.95)";
-// 与 Graph.tsx 的 EDGE_COLOR 同一个灰,RGB 再压一档:这里的线粗一倍
-// (MIN_EDGE_THICKNESS),同一个色值会显得更亮
-const EDGE_RELATION = "rgba(128,128,128,0.3)";
-const EDGE_RELATION_FOCUS = "rgba(255,255,255,0.6)";
-const EDGE_DISJOINT = "rgba(255,157,175,0.45)"; // --u-danger
-const EDGE_DISJOINT_FOCUS = "rgba(255,157,175,0.9)";
-/* 业务规则:紫(--u-violet)。**推出来的东西全站都是这个色**——图谱页的派生边、
- 文库里抽取完成的徽章都用它,所以「这条边上的类不是抽取来的,是规则算出来的」
- 不必再学一遍。弧线:规则的结论多半正是主类的子类,那对类之间已经有一条继承
- 边,直线会与它重叠 */
-/* 图例上的色块不用画布上那几个带 alpha 的值。**画布靠亮度区分继承与关系**
- (继承亮而细、关系灰而粗,两种线常常压在一起,只能这么分),可图例是四个
- 并排的小方块,同样两毫米高、一个 95% 白一个 60% 白,读出来不是"两种边",
- 是"这排线粗细不匀"——用户第一眼就是这么说的。
- 这里一律实色,同一强度,区别交给颜色本身;取的是各自的**常态**色而不是
- 聚焦色,因为常态才是画面上多数时候的样子 */
-const LEGEND_SUBCLASS = "#ebebeb";
-const LEGEND_RELATION = "#8c8c8c";
-const LEGEND_DISJOINT = "#ff9daf";
-const LEGEND_RULE = "#c4a5ff";
-const EDGE_RULE = "rgba(196,165,255,0.5)";
-const EDGE_RULE_FOCUS = "rgba(196,165,255,0.95)";
-const EDGE_DIM = "rgba(48,48,48,0.4)";
/** 三种边各自的语义——驱动颜色/暗淡/可点选,与「用哪个 sigma 程序画」分开管 */
const SUBCLASS_KIND = "subclass";
@@ -277,6 +270,8 @@ export function buildSchemaGraph(
* 条件写在规则里。得出属性值的那种没有目标节点,不画(它在规则表里) */
rules: BusinessRule[] = [],
): SchemaGraphResult {
+ // 壳色、边色烤进图属性,构图前先把调色板读成当前主题的值(0038)
+ refreshPalette();
const graph = new Graphology({ multi: true });
const byId = new Map(entityTypes.map((t) => [t.id, t]));
const depths = classDepths(entityTypes);
@@ -610,6 +605,8 @@ export function OntologySchemaGraph({
);
// 取景:大本体只画用得最多的那几个类,左栏点到的类补进来(见 schemaScope)
+ // 主题一变,模式图要重构(壳色烤在属性里)
+ const [themeTick, setThemeTick] = useState(0);
const [revealed, setRevealed] = useState<ReadonlySet<string>>(
() => new Set(),
);
@@ -635,8 +632,7 @@ export function OntologySchemaGraph({
// 取景,选中/悬停都是别的状态,不会触发这里
const schema = useMemo(
() => buildSchemaGraph(entityTypes, objectRelations, scope.drawn, rules),
- [entityTypes, objectRelations, scope.drawn, rules],
- );
+ [entityTypes, objectRelations, scope.drawn, rules, themeTick]);
/** 把一个类带到眼前:还在取景之外就先揭开、重建之后再对焦;画着但在视口
* 外就把相机推过去;已经在视口里就只高亮,画面不动 */
@@ -785,6 +781,8 @@ export function OntologySchemaGraph({
useEffect(() => {
const g = graphRef.current;
if (!containerRef.current || !g) return;
+ // 画布颜色从令牌读(0038):建实例前读一次,切主题后再读一次并重画
+ refreshPalette();
const sigma = new Sigma(g, containerRef.current, {
...sigmaOptions({
defaultEdgeType: EDGE_TYPE_ARROW,
@@ -827,7 +825,7 @@ export function OntologySchemaGraph({
// 这条关系的 domain 与 range 就是它连着的
const endpoints = new Set([...rel.domains, ...rel.ranges]);
if (endpoints.has(node)) {
- res.ringColor = mix(ownColorOf(attrs), "#ffffff", RING_SELECT_MIX);
+ res.ringColor = mix(ownColorOf(attrs), INK, RING_SELECT_MIX);
return neighborNode(res, base);
}
return mutedNode(res, base);
@@ -975,7 +973,19 @@ export function OntologySchemaGraph({
(window as unknown as Record<string, unknown>).__schemaGraph = g;
(window as unknown as Record<string, unknown>).__schemaSigma = sigma;
}
+ const offTheme = onThemeChange(() => {
+ refreshPalette();
+ // 标签色是建实例时按当时的调色板定死的(sigmaOptions),实例不重建就得改设置
+ sigma.setSetting("labelColor", { color: CANVAS_TEXT });
+ sigma.setSetting("edgeLabelColor", { color: CANVAS_TEXT_2 });
+ setThemeTick((t) => t + 1);
+ sigma.refresh();
+ // 世界网格只在相机动时重画:这里补一笔,不然它停在上一套墨色
+ if (gridRef.current) drawWorldGrid(gridRef.current, sigma);
+ });
return () => {
+ offTheme();
+
sigma.kill();
sigmaRef.current = null;
};
diff --git a/web/src/pages/UserMenu.tsx b/web/src/pages/UserMenu.tsx
index 40c7331e..80f7f109 100644
--- a/web/src/pages/UserMenu.tsx
+++ b/web/src/pages/UserMenu.tsx
@@ -5,6 +5,7 @@ import {
} from "../ui";
/* 用户菜单:顶栏右侧的头像胶囊 + 弹出面板(个人信息 / 系统管理 / 登出)。
Shell(KB 工作区)与 AccountShell(账户层)共用。 */
+import { useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import {
@@ -15,6 +16,7 @@ import {
LogOut,
ShieldCheck,
UserRound,
+ SunMoon,
} from "lucide-react";
import { api, type User } from "../api";
import { usePopoverFlip } from "../ui/popoverFlip";
@@ -38,7 +40,13 @@ export function Avatar({ name, size = 24 }: { name: string; size?: number }) {
);
}
+import { getTheme, setTheme, type Theme } from "../theme";
+
+// 三档的顺序就是菜单里的顺序:本色在前,跟系统在最后
+const THEMES: Theme[] = ["dark", "light", "system"];
+
export function UserMenu({ user }: { user: User }) {
+ const [theme, setThemeState] = useState<Theme>(() => getTheme());
// 原地变形(FLIP):胶囊"长成"面板。实现共用,见 ui/popoverFlip——
// 告警铃铛就在旁边,两处各写一遍迟早会差出一点点
const { open, setOpen, close, rootRef, anchorRef, panelRef } =
@@ -159,6 +167,33 @@ export function UserMenu({ user }: { user: User }) {
))}
</div>
+ {/* 主题(0038):暗是本色,浅色给白天对着它八小时的人;跟系统是第三档。
+ 与语言同一个道理——看的人自己定,不经过后端 */}
+ <div className="border-t border-line">
+ <div className="flex items-center gap-3 px-4 pt-3 pb-1 text-fine text-ink-2">
+ <SunMoon size={13} className="text-ink-2" />
+ {S.account.theme}
+ </div>
+ {THEMES.map((t) => (
+ <Row
+ density="menu"
+ className="gap-3 px-4 py-2 text-body"
+ key={t}
+ icon={
+ <span className="block w-[13px]">
+ {t === theme && <Check size={13} className="text-ink-2" />}
+ </span>
+ }
+ onClick={() => {
+ setTheme(t);
+ setThemeState(t);
+ }}
+ >
+ {S.account.themeNames[t]}
+ </Row>
+ ))}
+ </div>
+
<div className="border-t border-line">
<Row
density="menu"
diff --git a/web/src/pages/graphCanvas.ts b/web/src/pages/graphCanvas.ts
index 494ee422..f2390b24 100644
--- a/web/src/pages/graphCanvas.ts
+++ b/web/src/pages/graphCanvas.ts
@@ -33,6 +33,7 @@ import {
RING_HOVER_MIX,
RING_SELECT_MIX,
TRANSPARENT,
+ INK,
} from "./graphVisuals";
/* ============ 节点程序 ============ */
@@ -186,7 +187,7 @@ export function hoveredNode(
base: number,
): NodeAttrs {
res.size = Math.max(base * 1.08, 10.4);
- res.ringColor = mix(ownColorOf(attrs), "#ffffff", RING_HOVER_MIX);
+ res.ringColor = mix(ownColorOf(attrs), INK, RING_HOVER_MIX);
res.forceLabel = true;
res.labelSlab = true;
res.zIndex = 4;
@@ -219,7 +220,7 @@ export function selectedNode(
base: number,
): NodeAttrs {
res.size = Math.max(base * 1.02, 9.2);
- res.ringColor = mix(ownColorOf(attrs), "#ffffff", RING_SELECT_MIX);
+ res.ringColor = mix(ownColorOf(attrs), INK, RING_SELECT_MIX);
res.forceLabel = true;
res.labelSlab = true;
// **反色**:浅底深字。指到的那一个是深底浅字,同一副形状调个个儿——
diff --git a/web/src/pages/graphVisuals.ts b/web/src/pages/graphVisuals.ts
index b3347803..24d8dc54 100644
--- a/web/src/pages/graphVisuals.ts
+++ b/web/src/pages/graphVisuals.ts
@@ -11,9 +11,9 @@ import type Sigma from "sigma";
/* 画布调色板 —— 结构取自 Semantica GraphWorkspace 源码;基色已中性化:
Semantica 原版是钢蓝系(#0B1320/#5A7A9E/#7A92AE),按"chrome 零色偏、
彩色只属于数据"的既定原则换成同明度纯灰,类型色混入比例不变 */
-export const NODE_SHELL_BASE = "#121212"; // 节点外壳深底(原 #0B1320 的中性化)
-export const NODE_CORE_BASE = "#767676"; // 节点核心灰(原 #5A7A9E 的中性化)
-export const NODE_BORDER_BASE = "#909090"; // 节点描边(原 #7A92AE 的中性化)
+export let NODE_SHELL_BASE = "#121212"; // 节点外壳深底(原 #0B1320 的中性化)
+export let NODE_CORE_BASE = "#767676"; // 节点核心灰(原 #5A7A9E 的中性化)
+export let NODE_BORDER_BASE = "#909090"; // 节点描边(原 #7A92AE 的中性化)
export const NODE_TINT_MIX = 0.14; // 类型色只按 14% 混入外壳(高级感的关键)
export const NODE_CORE_MIX = 0.5; // 核心向类型色的混入比例
/* 状态环取**节点自己的类型色**,不是写死的色相。往白里混而不是直接用原色:
@@ -24,18 +24,18 @@ export const NODE_CORE_MIX = 0.5; // 核心向类型色的混入比例
export const RING_HOVER_MIX = 0.7; // 悬停:偏白,为的是跳出来
export const RING_SELECT_MIX = 0.35; // 选中:偏本色,为的是认得出
export const TRANSPARENT = "rgba(0,0,0,0)";
-export const MUTED_SHELL = "#151515";
+export let MUTED_SHELL = "#151515";
/* 悬停时其余的压暗程度。**比选中轻**(选中是压到底):悬停是随鼠标走的、
每划过一个节点就换一次,压到底会让整张画布不停明灭 */
export const HOVER_MUTE = 0.78;
-export const PILL_BG = "rgba(12,12,12,0.9)";
-export const PILL_BORDER = "rgba(255,255,255,0.14)"; // --u-line-strong
-export const PILL_TEXT = "#ededed"; // --u-text
+export let PILL_BG = "rgba(12,12,12,0.9)";
+export let PILL_BORDER = "rgba(255,255,255,0.14)"; // --u-line-strong
+export let PILL_TEXT = "#ededed"; // --u-text
/* 裸字的光晕:与画布同色(--u-ground)的一圈描边,只为把从字底下穿过的
连线压住。不是阴影——阴影会在一片细线里糊成一团脏 */
-export const LABEL_HALO = "rgba(10,10,10,0.92)";
+export let LABEL_HALO = "rgba(10,10,10,0.92)";
/** 浮层的面,抄 `.u-pop`(tooltip / toast 用的那一档近实底) */
-export const POP_BG = "rgba(16,16,16,0.98)";
+export let POP_BG = "rgba(16,16,16,0.98)";
/* 画布上的字与界面同一套刻度。**canvas 读不到 CSS 变量**,所以这里镜像一份
`styles.css` 的值——它是源头,改那边记得回来改这里。
@@ -43,8 +43,8 @@ export const POP_BG = "rgba(16,16,16,0.98)";
#a1a1a1):字号整体上移一档之后,画布成了全站唯一还在用旧刻度的地方,
而 9px 比界面里最小的字还小一半 */
export const CANVAS_FONT = '"Geist", "Inter", "Noto Sans SC", sans-serif';
-export const CANVAS_TEXT = "#ededed"; // --u-text
-export const CANVAS_TEXT_2 = "#a8a8a8"; // --u-text-2
+export let CANVAS_TEXT = "#ededed"; // --u-text
+export let CANVAS_TEXT_2 = "#a8a8a8"; // --u-text-2
/* 画在节点与连线之间的字比界面的底再小一档(11)。**画布不是界面**:
这些字压在一片线和点上,与它们比邻的是 5–13px 的节点,不是页面上的正文;
12 在这里显得比它标注的东西还重。浮在画布之上的悬浮卡不算——那是 tooltip,
@@ -138,7 +138,7 @@ export function drawNodeLabel(
位置,只有明暗调个个儿——"指着"与"选中"是同一件事的两个程度,从前一个是
浮起来的两行卡片、一个是这块底牌,看着像两种不同的东西 */
const h = size + padY * 2;
- ctx.shadowColor = "rgba(0,0,0,0.6)";
+ ctx.shadowColor = token("--u-shadow", "rgba(0,0,0,0.6)");
ctx.shadowBlur = 12;
ctx.beginPath();
ctx.roundRect(x - padX, y - h / 2, w + padX * 2, h, 4);
@@ -219,7 +219,8 @@ export function drawWorldGrid(canvas: HTMLCanvasElement, sigma: Sigma): void {
(ss - GRID_FADE_IN_PX) / (GRID_FULL_PX - GRID_FADE_IN_PX),
);
if (t <= 0) continue;
- ctx.strokeStyle = `rgba(255,255,255,${(GRID_MAX_ALPHA * t).toFixed(4)})`;
+ // 网格是墨色不是白色:暗底上是白,纸底上是近黑(0038)。透明度不变
+ ctx.strokeStyle = `rgba(${INK_RGB},${(GRID_MAX_ALPHA * t).toFixed(4)})`;
ctx.lineWidth = 1;
ctx.beginPath();
const startX = ((p0.x % ss) + ss) % ss;
@@ -237,3 +238,135 @@ export function drawWorldGrid(canvas: HTMLCanvasElement, sigma: Sigma): void {
ctx.stroke();
}
}
+
+/* ---------- 令牌的读者(0038 浅色版) ----------
+ * canvas 读不到 var(),所以画布用的每一个颜色都从 <html> 上算好的令牌里读一遍。
+ * 上面那些 `let` 的初值是暗色,只在第一帧前、以及没有 DOM 的测试里生效;
+ * `refreshPalette()` 在启动和每次切主题时把它们全部换成当前主题的值。
+ * ES 模块的导出是活绑定:这里赋值,import 的那一侧读到的就是新值。 */
+export let EDGE = "rgba(163,163,163,0.2)";
+export let EDGE_INFERRED = "rgba(163,163,163,0.1)";
+export let EDGE_DIM = "#141414";
+export let EDGE_FOCUS = "rgba(255,255,255,0.55)";
+export let EDGE_DERIVED = "rgba(231,197,124,0.42)";
+export let EDGE_DERIVED_DIM = "rgba(231,197,124,0.14)";
+export let EDGE_FOCUS_DERIVED = "rgba(255,214,140,0.95)";
+export let EDGE_CONTEST = "rgba(255,106,61,0.55)";
+export let EDGE_FOCUS_CONTEST = "rgba(255,106,61,1)";
+export let EDGE_SUBCLASS = "rgba(235,235,235,0.55)";
+export let EDGE_SUBCLASS_FOCUS = "rgba(255,255,255,0.95)";
+export let EDGE_RELATION = "rgba(128,128,128,0.3)";
+export let EDGE_RELATION_FOCUS = "rgba(255,255,255,0.6)";
+export let EDGE_DISJOINT = "rgba(255,157,175,0.45)";
+export let EDGE_DISJOINT_FOCUS = "rgba(255,157,175,0.9)";
+export let EDGE_RULE = "rgba(196,165,255,0.5)";
+export let EDGE_RULE_FOCUS = "rgba(196,165,255,0.95)";
+export let EDGE_SCHEMA_DIM = "rgba(48,48,48,0.4)";
+export let LEGEND_SUBCLASS = "#ebebeb";
+export let LEGEND_RELATION = "#8c8c8c";
+export let LEGEND_DISJOINT = "#ff9daf";
+export let LEGEND_RULE = "#c4a5ff";
+export let SCRUB_PAST = "rgba(255,255,255,0.32)";
+export let SCRUB_PLAY = "rgba(255,255,255,0.62)";
+export let SCRUB_FUTURE = "rgba(255,255,255,0.04)";
+/** 墨色最亮的那一档(暗底白、浅底近黑):环往它混、登录场景的粒子用它 */
+export let INK = "#ffffff";
+/** 墨的三元组,给需要自己调透明度的地方 */
+export let INK_RGB = "255,255,255";
+
+function token(name: string, fallback: string): string {
+ if (typeof document === "undefined") return fallback;
+ const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
+ return v || fallback;
+}
+function rgbOf(triplet: string, alpha: number): string {
+ return `rgba(${triplet.replace(/\s+/g, "")},${alpha})`;
+}
+
+/** 墨色按透明度:登录场景的粒子、边、脉冲都用它,页面里不再自己拼 rgba */
+export function inkAt(alpha: number): string {
+ return rgbOf(INK_RGB, alpha);
+}
+
+/** 浅色下把一个 rgba 边色按底色摊平成不透明的 rgb。
+ *
+ * sigma 的边着色器用预乘混合(ONE, ONE_MINUS_SRC_ALPHA)却不预乘 RGB,
+ * 于是透明度压不暗一条边,只会把它**加**到底上:黑底上加一点灰正好是一条淡线,
+ * 纸底上同一个 rgba(90,90,90,0.25) 加上去就溢出成白——「没选中时所有线都是白的」
+ * 就是这么来的。暗度必须编码进 RGB(Graph.tsx 里 EDGE_DIM 那条注释说的同一件事)。
+ * 暗色那一套是加法下调出来的,不动;浅色把 α 在这里就混掉。 */
+function flattenOnLight(color: string): string {
+ if (typeof document === "undefined" || document.documentElement.dataset.theme !== "light") {
+ return color;
+ }
+ const m = /^rgba\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)$/.exec(color);
+ if (!m) return color;
+ const a = Number(m[4]);
+ const ground = token("--u-ground-rgb", "250,250,250").split(",").map((v) => Number(v.trim()));
+ const ch = (i: number) => Math.round(Number(m[i]) * a + ground[i - 1] * (1 - a));
+ return `rgb(${ch(1)},${ch(2)},${ch(3)})`;
+}
+
+/** 启动时和切主题后调一次,然后 `sigma.refresh()`。 */
+export function refreshPalette() {
+ NODE_SHELL_BASE = token("--u-node-shell", NODE_SHELL_BASE);
+ NODE_CORE_BASE = token("--u-node-core", NODE_CORE_BASE);
+ NODE_BORDER_BASE = token("--u-node-border", NODE_BORDER_BASE);
+ MUTED_SHELL = token("--u-node-muted", MUTED_SHELL);
+ PILL_BG = token("--u-pill-bg", PILL_BG);
+ PILL_BORDER = token("--u-line-strong", PILL_BORDER);
+ PILL_TEXT = token("--u-text", PILL_TEXT);
+ LABEL_HALO = token("--u-halo", LABEL_HALO);
+ POP_BG = token("--u-pop-surface", POP_BG);
+ CANVAS_TEXT = token("--u-text", CANVAS_TEXT);
+ CANVAS_TEXT_2 = token("--u-text-2", CANVAS_TEXT_2);
+ EDGE = token("--u-edge", EDGE);
+ EDGE_INFERRED = token("--u-edge-inferred", EDGE_INFERRED);
+ EDGE_DIM = token("--u-edge-dim", EDGE_DIM);
+ EDGE_FOCUS = token("--u-edge-focus", EDGE_FOCUS);
+ EDGE_DERIVED = token("--u-edge-derived", EDGE_DERIVED);
+ EDGE_DERIVED_DIM = token("--u-edge-derived-dim", EDGE_DERIVED_DIM);
+ EDGE_FOCUS_DERIVED = token("--u-edge-derived-focus", EDGE_FOCUS_DERIVED);
+ EDGE_SUBCLASS = token("--u-edge-subclass", EDGE_SUBCLASS);
+ EDGE_SUBCLASS_FOCUS = token("--u-edge-subclass-focus", EDGE_SUBCLASS_FOCUS);
+ EDGE_RELATION = token("--u-edge-relation", EDGE_RELATION);
+ EDGE_RELATION_FOCUS = token("--u-edge-relation-focus", EDGE_RELATION_FOCUS);
+ EDGE_SCHEMA_DIM = token("--u-edge-schema-dim", EDGE_SCHEMA_DIM);
+ SCRUB_PAST = token("--u-scrub-past", SCRUB_PAST);
+ SCRUB_PLAY = token("--u-scrub-play", SCRUB_PLAY);
+ SCRUB_FUTURE = token("--u-scrub-future", SCRUB_FUTURE);
+ INK_RGB = token("--u-ink-rgb", INK_RGB);
+ INK = rgbOf(INK_RGB, 1);
+ // 语义色按三元组调透明度:争议边、不相交边、规则边
+ const contest = token("--u-contest-rgb", "255,106,61");
+ const danger = token("--u-danger-rgb", "255,157,175");
+ const violet = token("--u-violet-rgb", "196,165,255");
+ EDGE_CONTEST = rgbOf(contest, 0.55);
+ EDGE_FOCUS_CONTEST = rgbOf(contest, 1);
+ EDGE_DISJOINT = rgbOf(danger, 0.45);
+ EDGE_DISJOINT_FOCUS = rgbOf(danger, 0.9);
+ EDGE_RULE = rgbOf(violet, 0.5);
+ EDGE_RULE_FOCUS = rgbOf(violet, 0.95);
+ // 画布上的边:浅色下按底色摊平(见 flattenOnLight)
+ EDGE = flattenOnLight(EDGE);
+ EDGE_INFERRED = flattenOnLight(EDGE_INFERRED);
+ EDGE_FOCUS = flattenOnLight(EDGE_FOCUS);
+ EDGE_DERIVED = flattenOnLight(EDGE_DERIVED);
+ EDGE_DERIVED_DIM = flattenOnLight(EDGE_DERIVED_DIM);
+ EDGE_FOCUS_DERIVED = flattenOnLight(EDGE_FOCUS_DERIVED);
+ EDGE_CONTEST = flattenOnLight(EDGE_CONTEST);
+ EDGE_FOCUS_CONTEST = flattenOnLight(EDGE_FOCUS_CONTEST);
+ EDGE_SUBCLASS = flattenOnLight(EDGE_SUBCLASS);
+ EDGE_SUBCLASS_FOCUS = flattenOnLight(EDGE_SUBCLASS_FOCUS);
+ EDGE_RELATION = flattenOnLight(EDGE_RELATION);
+ EDGE_RELATION_FOCUS = flattenOnLight(EDGE_RELATION_FOCUS);
+ EDGE_DISJOINT = flattenOnLight(EDGE_DISJOINT);
+ EDGE_DISJOINT_FOCUS = flattenOnLight(EDGE_DISJOINT_FOCUS);
+ EDGE_RULE = flattenOnLight(EDGE_RULE);
+ EDGE_RULE_FOCUS = flattenOnLight(EDGE_RULE_FOCUS);
+ EDGE_SCHEMA_DIM = flattenOnLight(EDGE_SCHEMA_DIM);
+ LEGEND_SUBCLASS = token("--u-text", LEGEND_SUBCLASS);
+ LEGEND_RELATION = token("--u-text-2", LEGEND_RELATION);
+ LEGEND_DISJOINT = token("--u-danger", LEGEND_DISJOINT);
+ LEGEND_RULE = token("--u-violet", LEGEND_RULE);
+}
diff --git a/web/src/palette.ts b/web/src/palette.ts
new file mode 100644
index 00000000..85dabe73
--- /dev/null
+++ b/web/src/palette.ts
@@ -0,0 +1,49 @@
+/* 实体类型的数据色(规矩 4:彩色只属于数据)。从 ui/index.tsx 搬到这里(0038),
+ * 因为 style-guard 现在不许页面和组件里出现色值本身,而这一份**是数据**,且要与
+ * `crates/utopia-store/src/palette.rs` 逐字一致。浅色版暂时沿用同一套:换一套浅底
+ * 专用的数据色得连服务端那份一起换,另开一刀。 */
+
+/* ---------- ColorPicker(精选色板 + hex 兜底;实体色刻意不开放全色域) ----------
+ **改这里就得改 `crates/utopia-store/src/palette.rs`**:手动挑的色与自动按 key
+ 取的色必须来自同一组,否则一张图里会出现两套配色。那边有测试盯着,改漏了会红。 */
+export const ENTITY_PALETTE = [
+ "#7fd0ff",
+ "#5fa8ff",
+ "#5fd4d0",
+ "#63e2b7",
+ "#4cc38a",
+ "#a8d878",
+ "#ffd479",
+ "#f2b66d",
+ "#ff9d76",
+ "#ff8a9e",
+ "#ff9daf",
+ "#e797d8",
+ "#c4a5ff",
+ "#9fa8ff",
+ "#8ea5bd",
+ "#b3b9c4",
+];
+
+/**
+ * 类的 key → 颜色。**必须与 `crates/utopia-store/src/palette.rs` 的
+ * `color_for_key` 逐位一致**:新建类时前端先按 key 挑一个显示出来,
+ * 用户不改就这么存下去;而导入/消解那条路是后端算的。两边算得不一样,
+ * 同一个 key 就会因为「谁建的」而拿到不同颜色。
+ *
+ * FNV-1a + 雪崩混合。用 BigInt 是因为 JS 的位运算是 32 位的,
+ * 而这里要的是 64 位乘法——用 Number 做会静默丢高位,
+ * 算出来跟 Rust 对不上,且不会有任何报错。
+ */
+export function colorForKey(key: string): string {
+ let h = 0xcbf29ce484222325n;
+ const M = (1n << 64n) - 1n;
+ for (const b of new TextEncoder().encode(key)) {
+ h = (h ^ BigInt(b)) & M;
+ h = (h * 0x100000001b3n) & M;
+ }
+ h = (h ^ (h >> 33n)) & M;
+ h = (h * 0xff51afd7ed558ccdn) & M;
+ h = (h ^ (h >> 33n)) & M;
+ return ENTITY_PALETTE[Number(h % BigInt(ENTITY_PALETTE.length))];
+}
diff --git a/web/src/styles.css b/web/src/styles.css
index 899303ce..229463d7 100644
--- a/web/src/styles.css
+++ b/web/src/styles.css
@@ -134,6 +134,139 @@
--u-surface-strong-hover: rgba(10, 10, 10, 0.94);
--u-menu-surface: rgba(14, 14, 14, 0.85);
--u-menu-surface-hover: rgba(14, 14, 14, 0.97);
+
+ /* ---------- 以下令牌是浅色版(0038)加的:从前这些数写死在各处 ---------- */
+ /* 「墨」与「底」的 RGB 三元组:所有 rgba(255,255,255,α) / rgba(10,10,10,α)
+ 都归到这两个上,透明度留在用的地方。浅色版只换三元组,透明度一处不动 */
+ --u-ink-rgb: 255, 255, 255;
+ --u-ground-rgb: 10, 10, 10;
+ /* 实底主按钮上的字:与 --u-accent 相反的那一头 */
+ --u-on-accent: #0a0a0a;
+ /* 遮罩 / 弹层 / 对话框 / 不透明卡片的面 */
+ --u-scrim: rgba(0, 0, 0, 0.8);
+ --u-pop-surface: rgba(16, 16, 16, 0.98);
+ --u-modal-surface: rgba(12, 12, 12, 0.97);
+ --u-card-surface: rgba(12, 12, 12, 0.92);
+ /* 画布网格两级 */
+ --u-grid: rgba(var(--u-ink-rgb), 0.055);
+ --u-grid-2: rgba(var(--u-ink-rgb), 0.02);
+ /* 语义色的三元组:画布上的争议边、危险边、规则边要按透明度调 */
+ --u-contest-rgb: 255, 106, 61;
+ --u-danger-rgb: 255, 157, 175;
+ --u-violet-rgb: 196, 165, 255;
+ --u-derived-rgb: 231, 197, 124;
+ /* 画布:节点四层与几档边。canvas 读不到 var(),graphVisuals 从算出来的值里读 */
+ --u-node-shell: #121212;
+ --u-node-core: #767676;
+ --u-node-border: #909090;
+ --u-node-muted: #151515;
+ --u-edge: rgba(163, 163, 163, 0.2);
+ --u-edge-inferred: rgba(163, 163, 163, 0.1);
+ --u-edge-dim: #141414;
+ --u-edge-focus: rgba(var(--u-ink-rgb), 0.55);
+ --u-edge-derived: rgba(var(--u-derived-rgb), 0.42);
+ --u-edge-derived-dim: rgba(var(--u-derived-rgb), 0.14);
+ --u-edge-derived-focus: rgba(255, 214, 140, 0.95);
+ --u-edge-subclass: rgba(235, 235, 235, 0.55);
+ --u-edge-subclass-focus: rgba(var(--u-ink-rgb), 0.95);
+ --u-edge-relation: rgba(128, 128, 128, 0.3);
+ --u-edge-relation-focus: rgba(var(--u-ink-rgb), 0.6);
+ --u-edge-schema-dim: rgba(48, 48, 48, 0.4);
+ --u-pill-bg: rgba(12, 12, 12, 0.9);
+ --u-halo: rgba(var(--u-ground-rgb), 0.92);
+ /* 时间轴刻度:走过的、播放头、没到的 */
+ --u-scrub-past: rgba(var(--u-ink-rgb), 0.32);
+ --u-scrub-play: rgba(var(--u-ink-rgb), 0.62);
+ --u-scrub-future: rgba(var(--u-ink-rgb), 0.04);
+ /* 危险实底按钮上的字:深红上永远是白,与主题无关 */
+ --u-on-danger: #ffffff;
+ /* 浮岛的投影:暗底下要重,浅底下要轻 */
+ --u-shadow: rgba(0, 0, 0, 0.5);
+ /* 语义色的另外两个三元组:底色淡染(chip / mark)按透明度调 */
+ --u-ok-rgb: 76, 195, 138;
+ --u-warn-rgb: 242, 182, 109;
+ /* 危险实底按钮悬停时更亮的那一档 */
+ --u-danger-solid-hover: #d9464b;
+ /* 对话里代码高亮的八个色:数据色的近亲,浅底上要换深版才读得出 */
+ --u-code-quote: #7d8590;
+ --u-code-meta: #9fa8ff;
+ --u-code-addition: #63e2b7;
+ --u-code-symbol: #ffd479;
+ --u-code-section: #7fd0ff;
+ --u-code-class: #5fd4d0;
+ --u-code-template: #c4a5ff;
+ --u-code-deletion: #ff8a9e;
+}
+
+/* ---------- 浅色(0038):只改令牌,一条规则都不重写 ----------
+ 底是纸白不是纯白,字是近黑不是纯黑,线与面从「白的透明度」换成「黑的透明度」,
+ 透明度数值一律沿用暗色那套——它们描述的是层级差,不是颜色。
+ 语义色换成同一色相的深版:浅底上淡玫瑰/淡金读不出来。 */
+:root[data-theme="light"] {
+ color-scheme: light;
+ --u-ground: #fafafa;
+ --u-text: #171717;
+ --u-text-2: #5f5f5f;
+ --u-accent: #262626;
+ --u-accent-deep: 23, 23, 23;
+ --u-ink-rgb: 23, 23, 23;
+ --u-ground-rgb: 250, 250, 250;
+ --u-on-accent: #fafafa;
+ --u-ok: #1a7f4b;
+ --u-warn: #9a6a12;
+ --u-danger: #c0284f;
+ --u-danger-solid: #c9353a;
+ --u-contest: #c8471c;
+ --u-violet: #6d4fd6;
+ --u-line: rgba(0, 0, 0, 0.08);
+ --u-line-strong: rgba(0, 0, 0, 0.14);
+ --u-surface: rgba(0, 0, 0, 0.03);
+ --u-surface-2: rgba(0, 0, 0, 0.05);
+ --u-surface-3: rgba(0, 0, 0, 0.08);
+ --u-ring: rgba(0, 0, 0, 0.5);
+ --u-well: rgba(0, 0, 0, 0.05);
+ --u-surface-strong: rgba(250, 250, 250, 0.86);
+ --u-surface-strong-hover: rgba(250, 250, 250, 0.94);
+ --u-menu-surface: rgba(252, 252, 252, 0.9);
+ --u-menu-surface-hover: rgba(252, 252, 252, 0.98);
+ --u-scrim: rgba(0, 0, 0, 0.35);
+ --u-pop-surface: rgba(255, 255, 255, 0.98);
+ --u-modal-surface: rgba(255, 255, 255, 0.97);
+ --u-card-surface: rgba(255, 255, 255, 0.92);
+ --u-contest-rgb: 200, 71, 28;
+ --u-danger-rgb: 192, 40, 79;
+ --u-violet-rgb: 109, 79, 214;
+ --u-derived-rgb: 176, 120, 20;
+ --u-node-shell: #ffffff;
+ --u-node-core: #8a8a8a;
+ --u-node-border: #6f6f6f;
+ --u-node-muted: #ececec;
+ /* 画布上的边在浅色下会被 graphVisuals 按底色摊平成不透明色(WebGL 那边透明度
+ 压不暗线,只会加亮);这里的 α 因此是「摊平后多深」,不是屏幕上的透明度 */
+ --u-edge: rgba(23, 23, 23, 0.36);
+ --u-edge-inferred: rgba(23, 23, 23, 0.18);
+ --u-edge-dim: #e4e4e4;
+ --u-edge-focus: rgba(23, 23, 23, 0.85);
+ --u-edge-derived: rgba(176, 120, 20, 0.6);
+ --u-edge-derived-dim: rgba(176, 120, 20, 0.22);
+ --u-edge-derived-focus: rgba(150, 100, 10, 0.95);
+ --u-edge-subclass: rgba(23, 23, 23, 0.6);
+ --u-edge-subclass-focus: rgba(23, 23, 23, 0.9);
+ --u-edge-relation: rgba(23, 23, 23, 0.4);
+ --u-edge-relation-focus: rgba(23, 23, 23, 0.85);
+ --u-edge-schema-dim: rgba(23, 23, 23, 0.12);
+ --u-pill-bg: rgba(255, 255, 255, 0.92);
+ --u-shadow: rgba(0, 0, 0, 0.18);
+ --u-ok-rgb: 26, 127, 75;
+ --u-warn-rgb: 154, 106, 18;
+ --u-code-quote: #6b7280;
+ --u-code-meta: #4f5bd5;
+ --u-code-addition: #1a7f4b;
+ --u-code-symbol: #9a6a12;
+ --u-code-section: #1d6fb8;
+ --u-code-class: #0f8a86;
+ --u-code-template: #6d4fd6;
+ --u-code-deletion: #c0284f;
}
body {
@@ -145,7 +278,7 @@ body {
/* 选中态:中性白纱替掉浏览器默认蓝(零色偏原则同样管到这里)。
只设底色不改字色——半透明白纱下内容自身的颜色(代码令牌、实体色)仍透出 */
::selection {
- background: rgba(255, 255, 255, 0.22);
+ background: rgba(var(--u-ink-rgb), 0.22);
}
/* 空间感:极克制的中性顶光 + 暗角,零色偏 */
@@ -155,7 +288,7 @@ body::before {
inset: 0;
z-index: -1;
background:
- radial-gradient(1000px 600px at 50% -20%, rgba(255, 255, 255, 0.045), transparent 60%),
+ radial-gradient(1000px 600px at 50% -20%, rgba(var(--u-ink-rgb), 0.045), transparent 60%),
var(--u-ground);
}
@@ -223,7 +356,7 @@ body::before {
/* **中性,不掺色相**:原本是 rgba(12,12,14),蓝比红绿高 2——
在大面积深底上肉眼看得出偏冷。这产品的既定原则是
「chrome 零色偏、彩色只属于数据」,这里也照办 */
- background: rgba(12, 12, 12, 0.97);
+ background: var(--u-modal-surface);
/* saturate(1) 是恒等,加它只为**让滤镜列表与其他玻璃面同形**:
两个列表函数不一致时,backdrop-filter 只能离散跳变,插值动不了 */
backdrop-filter: blur(24px) saturate(1);
@@ -264,7 +397,7 @@ body::before {
}
}
.u-modal-scrim {
- background: rgba(0, 0, 0, 0.8);
+ background: var(--u-scrim);
backdrop-filter: blur(4px) saturate(1);
-webkit-backdrop-filter: blur(4px) saturate(1);
animation: u-modal-scrim-kf 0.14s ease-out both;
@@ -281,7 +414,7 @@ body::before {
/* 弹出层(下拉/取色器等 popover):近实底——下层内容透出只会干扰 */
.u-pop {
- background: rgba(16, 16, 16, 0.98);
+ background: var(--u-pop-surface);
backdrop-filter: blur(8px) saturate(1);
-webkit-backdrop-filter: blur(8px) saturate(1);
border: 1px solid var(--u-line-strong);
@@ -332,7 +465,7 @@ body::before {
background: linear-gradient(
90deg,
transparent,
- rgba(255, 255, 255, 0.18),
+ rgba(var(--u-ink-rgb), 0.18),
transparent
);
animation: u-sweep-kf 720ms cubic-bezier(0.4, 0, 0.2, 1) both;
@@ -411,7 +544,7 @@ body::before {
transition: background-color var(--u-base) ease;
backdrop-filter: blur(24px) saturate(0.3);
-webkit-backdrop-filter: blur(24px) saturate(0.3);
- border: 1px solid rgba(255, 255, 255, 0.14);
+ border: 1px solid var(--u-line-strong);
}
.u-menu-glass:hover {
background: var(--u-menu-surface-hover);
@@ -456,7 +589,7 @@ body::before {
.input-dark:focus {
outline: none;
border-color: var(--u-line-strong);
- box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.22);
+ box-shadow: 0 0 0 3px rgba(var(--u-ink-rgb), 0.22);
}
.input-dark:disabled {
opacity: 0.4;
@@ -482,7 +615,7 @@ textarea.u-input-sm {
padding-bottom: 0.375rem;
}
select.input-dark option {
- background: #111111;
+ background: var(--u-ground);
color: var(--u-text);
}
@@ -561,13 +694,13 @@ select.input-dark option {
.btn-primary {
background: var(--u-text);
border: 1px solid var(--u-text);
- color: #0a0a0a;
+ color: var(--u-on-accent);
font-weight: 600;
}
.u-btn-primary:hover:not(:disabled),
.btn-primary:hover:not(:disabled) {
- background: #ffffff;
- border-color: #ffffff;
+ background: var(--u-text);
+ border-color: var(--u-text);
}
.u-btn-secondary,
.u-btn-ghost,
@@ -581,7 +714,7 @@ select.input-dark option {
.btn-ghost:hover:not(:disabled) {
background: var(--u-surface-2);
color: var(--u-text);
- border-color: rgba(255, 255, 255, 0.22);
+ border-color: rgba(var(--u-ink-rgb), 0.22);
}
/* 无边框:行内动作、图标按钮、工具条 */
.u-btn-quiet {
@@ -597,12 +730,12 @@ select.input-dark option {
.u-btn-danger {
background: var(--u-danger-solid);
border: 1px solid var(--u-danger-solid);
- color: #ffffff;
+ color: var(--u-text);
font-weight: 600;
}
.u-btn-danger:hover:not(:disabled) {
- background: #d9464b;
- border-color: #d9464b;
+ background: var(--u-danger-solid-hover);
+ border-color: var(--u-danger-solid-hover);
}
/* 行里的小柄(树的折叠箭头):不是按钮——按钮里不能再嵌按钮——但要有 hover */
@@ -632,7 +765,7 @@ select.input-dark option {
background: var(--u-surface-2);
}
.u-tool.is-on {
- color: #ffffff;
+ color: var(--u-text);
background: var(--u-surface-3);
}
.u-tool:focus-visible {
@@ -706,7 +839,7 @@ select.input-dark option {
color: var(--u-text);
text-decoration: underline;
text-underline-offset: 2px;
- text-decoration-color: rgba(255, 255, 255, 0.3);
+ text-decoration-color: rgba(var(--u-ink-rgb), 0.3);
}
/* 只提亮不划线:整块引文那种链接 */
.u-hover-ink {
@@ -775,9 +908,9 @@ select.input-dark option {
background: var(--u-surface-2);
}
.u-choice.is-on {
- border-color: rgba(255, 255, 255, 0.6);
+ border-color: rgba(var(--u-ink-rgb), 0.6);
background: var(--u-surface-3);
- box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.3);
+ box-shadow: 0 0 0 1px rgba(var(--u-ink-rgb), 0.3);
}
.u-choice:focus-within {
outline: 2px solid var(--u-ring);
@@ -841,7 +974,7 @@ select.input-dark option {
}
.u-tab.is-active {
color: var(--u-text);
- border-bottom-color: #ffffff;
+ border-bottom-color: var(--u-text);
}
/* 选中的那一页从下划线往上透一点光:白 6% 在底,到一半高处就没了——说的是
「光从线上来」,不是给标签涂一块底。压在文字后面,不接指针 */
@@ -852,8 +985,8 @@ select.input-dark option {
z-index: -1;
background: linear-gradient(
to top,
- rgba(255, 255, 255, 0.06),
- rgba(255, 255, 255, 0) 50%
+ rgba(var(--u-ink-rgb), 0.06),
+ rgba(var(--u-ink-rgb), 0) 50%
);
pointer-events: none;
}
@@ -880,14 +1013,14 @@ select.input-dark option {
.u-mark {
border-radius: 3px;
padding: 0 0.25rem;
- background: rgba(242, 182, 109, 0.16);
+ background: rgba(var(--u-warn-rgb), 0.16);
color: var(--u-warn);
}
/* 对话输入框的外壳 */
.u-composer {
border-radius: var(--radius-panel);
- border: 1px solid rgba(255, 255, 255, 0.12);
+ border: 1px solid rgba(var(--u-ink-rgb), 0.12);
background: var(--u-surface);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
@@ -936,32 +1069,32 @@ select.input-dark option {
white-space: nowrap;
}
.u-chip-neutral {
- background: rgba(255, 255, 255, 0.08);
+ background: rgba(var(--u-ink-rgb), 0.08);
color: var(--u-text-2);
}
.u-chip-info {
- background: rgba(255, 255, 255, 0.12);
- color: #ededed;
+ background: rgba(var(--u-ink-rgb), 0.12);
+ color: var(--u-text);
}
.u-chip-success {
- background: rgba(76, 195, 138, 0.12);
+ background: rgba(var(--u-ok-rgb), 0.12);
color: var(--u-ok);
}
.u-chip-warn {
- background: rgba(242, 182, 109, 0.12);
+ background: rgba(var(--u-warn-rgb), 0.12);
color: var(--u-warn);
}
/* 争议(0017 §3):与图上的争议边同一个色相 */
.u-chip-contest {
- background: rgba(255, 106, 61, 0.14);
+ background: rgba(var(--u-contest-rgb), 0.14);
color: var(--u-contest);
}
.u-chip-danger {
- background: rgba(255, 157, 175, 0.12);
+ background: rgba(var(--u-danger-rgb), 0.12);
color: var(--u-danger);
}
.u-chip-violet {
- background: rgba(196, 165, 255, 0.12);
+ background: rgba(var(--u-violet-rgb), 0.12);
color: var(--u-violet);
}
/* 可点的 chip:指针停上提亮一点 */
@@ -980,16 +1113,16 @@ select.input-dark option {
主角是实体名,置信度和右侧的时间区间同属元信息——不该是整屏
唯一有填充色的东西。 */
.u-meta-warn {
- color: rgba(242, 182, 109, 0.55);
+ color: rgba(var(--u-warn-rgb), 0.55);
}
/* ---- graph canvas: 图纸式大小网格(细密小格 + 每 4 格一条更亮的主线) ---- */
.u-grid-bg {
background-image:
- linear-gradient(rgba(255, 255, 255, 0.055) 1px, transparent 1px),
- linear-gradient(90deg, rgba(255, 255, 255, 0.055) 1px, transparent 1px),
- linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
- linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
+ linear-gradient(var(--u-grid) 1px, transparent 1px),
+ linear-gradient(90deg, var(--u-grid) 1px, transparent 1px),
+ linear-gradient(var(--u-grid-2) 1px, transparent 1px),
+ linear-gradient(90deg, var(--u-grid-2) 1px, transparent 1px);
background-size:
104px 104px,
104px 104px,
@@ -1002,16 +1135,16 @@ select.input-dark option {
(输入框、控件、卡片),一条已经说完的话不是其中之一,给它描边就是在暗示
还能点。角色靠这块 8% 的底区分就够了 */
.u-bubble-user {
- background: rgba(255, 255, 255, 0.08);
+ background: rgba(var(--u-ink-rgb), 0.08);
}
.u-highlight {
- border-color: rgba(255, 255, 255, 0.45) !important;
- background: rgba(255, 255, 255, 0.06) !important;
- box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.12);
+ border-color: rgba(var(--u-ink-rgb), 0.45) !important;
+ background: rgba(var(--u-ink-rgb), 0.06) !important;
+ box-shadow: 0 0 0 2px rgba(var(--u-ink-rgb), 0.12);
}
.u-nav-active {
- background: rgba(255, 255, 255, 0.09);
- color: #ffffff;
+ background: rgba(var(--u-ink-rgb), 0.09);
+ color: var(--u-text);
}
/* 引用定位闪现:进场强光收敛到持有亮度(运动比静态边框更抓眼)。
@@ -1019,18 +1152,18 @@ select.input-dark option {
transition 淡回普通卡片 */
@keyframes u-flash-kf {
0% {
- border-color: rgba(255, 255, 255, 0.9);
- background-color: rgba(255, 255, 255, 0.14);
+ border-color: rgba(var(--u-ink-rgb), 0.9);
+ background-color: rgba(var(--u-ink-rgb), 0.14);
box-shadow:
- 0 0 0 3px rgba(255, 255, 255, 0.3),
- 0 0 36px rgba(255, 255, 255, 0.16);
+ 0 0 0 3px rgba(var(--u-ink-rgb), 0.3),
+ 0 0 36px rgba(var(--u-ink-rgb), 0.16);
}
100% {
- border-color: rgba(255, 255, 255, 0.55);
- background-color: rgba(255, 255, 255, 0.07);
+ border-color: rgba(var(--u-ink-rgb), 0.55);
+ background-color: rgba(var(--u-ink-rgb), 0.07);
box-shadow:
- 0 0 0 2px rgba(255, 255, 255, 0.16),
- 0 0 18px rgba(255, 255, 255, 0.06);
+ 0 0 0 2px rgba(var(--u-ink-rgb), 0.16),
+ 0 0 18px rgba(var(--u-ink-rgb), 0.06);
}
}
.u-flash {
@@ -1039,9 +1172,9 @@ select.input-dark option {
@media (prefers-reduced-motion: reduce) {
.u-flash {
animation: none;
- border-color: rgba(255, 255, 255, 0.55);
- background-color: rgba(255, 255, 255, 0.07);
- box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.16);
+ border-color: rgba(var(--u-ink-rgb), 0.55);
+ background-color: rgba(var(--u-ink-rgb), 0.07);
+ box-shadow: 0 0 0 2px rgba(var(--u-ink-rgb), 0.16);
}
}
@@ -1054,26 +1187,26 @@ select.input-dark option {
color: var(--u-text);
text-decoration: underline;
text-underline-offset: 3px;
- text-decoration-color: rgba(255, 255, 255, 0.3);
+ text-decoration-color: rgba(var(--u-ink-rgb), 0.3);
}
.u-link:hover {
- text-decoration-color: rgba(255, 255, 255, 0.8);
+ text-decoration-color: rgba(var(--u-ink-rgb), 0.8);
}
.u-title {
font-family: var(--font-display);
font-weight: 600;
letter-spacing: -0.01em;
- color: #ffffff;
+ color: var(--u-text);
}
.brand-gradient {
- color: #ffffff;
+ color: var(--u-text);
letter-spacing: -0.02em;
}
}
/* 登录卡:近实底,不用 backdrop-filter(对动画背景每帧重算模糊,代价高) */
.u-card-opaque {
- background: rgba(12, 12, 12, 0.92);
+ background: var(--u-card-surface);
border: 1px solid var(--u-line);
}
@@ -1136,7 +1269,7 @@ select.input-dark option {
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
- -webkit-box-shadow: 0 0 0 1000px #141414 inset;
+ -webkit-box-shadow: 0 0 0 1000px var(--u-ground) inset;
-webkit-text-fill-color: var(--u-text);
caret-color: var(--u-text);
}
@@ -1264,7 +1397,7 @@ a:hover > .u-mark-arrow {
background: linear-gradient(
90deg,
transparent,
- rgba(255, 255, 255, 0.05),
+ rgba(var(--u-ink-rgb), 0.05),
transparent
);
animation: u-skel-kf 1.4s ease-in-out infinite;
@@ -1310,7 +1443,7 @@ a:hover > .u-mark-arrow {
@supports not selector(::-webkit-scrollbar) {
* {
scrollbar-width: thin;
- scrollbar-color: rgba(255, 255, 255, 0.14) transparent;
+ scrollbar-color: var(--u-line-strong) transparent;
}
}
*::-webkit-scrollbar {
@@ -1321,11 +1454,11 @@ a:hover > .u-mark-arrow {
background: transparent;
}
*::-webkit-scrollbar-thumb {
- background: rgba(255, 255, 255, 0.14);
+ background: var(--u-line-strong);
border-radius: 4px;
}
*::-webkit-scrollbar-thumb:hover {
- background: rgba(255, 255, 255, 0.26);
+ background: rgba(var(--u-ink-rgb), 0.26);
}
*::-webkit-scrollbar-corner {
background: transparent;
@@ -1354,8 +1487,8 @@ a:hover > .u-mark-arrow {
width: 2px;
height: 28px;
border-radius: 1px;
- background: #ffffff;
- box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
+ background: var(--u-text);
+ box-shadow: 0 0 6px rgba(var(--u-ink-rgb), 0.5);
cursor: ew-resize;
}
.scrubber-range::-moz-range-thumb {
@@ -1363,8 +1496,8 @@ a:hover > .u-mark-arrow {
height: 28px;
border: none;
border-radius: 1px;
- background: #ffffff;
- box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
+ background: var(--u-text);
+ box-shadow: 0 0 6px rgba(var(--u-ink-rgb), 0.5);
cursor: ew-resize;
}
.scrubber-range::-webkit-slider-runnable-track {
@@ -1378,42 +1511,42 @@ a:hover > .u-mark-arrow {
与实体色板同族——颜色属于内容,chrome 保持无色 ---- */
.u-chat-prose .hljs-comment,
.u-chat-prose .hljs-quote {
- color: #7d8590;
+ color: var(--u-code-quote);
font-style: italic;
}
.u-chat-prose .hljs-keyword,
.u-chat-prose .hljs-selector-tag,
.u-chat-prose .hljs-meta {
- color: #9fa8ff;
+ color: var(--u-code-meta);
}
.u-chat-prose .hljs-string,
.u-chat-prose .hljs-regexp,
.u-chat-prose .hljs-addition {
- color: #63e2b7;
+ color: var(--u-code-addition);
}
.u-chat-prose .hljs-number,
.u-chat-prose .hljs-literal,
.u-chat-prose .hljs-symbol {
- color: #ffd479;
+ color: var(--u-code-symbol);
}
.u-chat-prose .hljs-title,
.u-chat-prose .hljs-title.function_,
.u-chat-prose .hljs-section {
- color: #7fd0ff;
+ color: var(--u-code-section);
}
.u-chat-prose .hljs-type,
.u-chat-prose .hljs-built_in,
.u-chat-prose .hljs-class .hljs-title {
- color: #5fd4d0;
+ color: var(--u-code-class);
}
.u-chat-prose .hljs-attr,
.u-chat-prose .hljs-attribute,
.u-chat-prose .hljs-variable,
.u-chat-prose .hljs-template-variable {
- color: #c4a5ff;
+ color: var(--u-code-template);
}
.u-chat-prose .hljs-deletion {
- color: #ff8a9e;
+ color: var(--u-code-deletion);
}
/* 对话气泡内的 markdown:紧凑排版,色彩服从气泡 */
@@ -1441,14 +1574,14 @@ a:hover > .u-mark-arrow {
margin: 0.15em 0;
}
.u-chat-prose strong {
- color: #ffffff;
+ color: var(--u-text);
font-weight: 600;
}
.u-chat-prose h1,
.u-chat-prose h2,
.u-chat-prose h3 {
font-weight: 600;
- color: #ffffff;
+ color: var(--u-text);
margin: 0.7em 0 0.3em;
font-size: 1em;
}
@@ -1460,12 +1593,12 @@ a:hover > .u-mark-arrow {
}
.u-chat-prose code {
font-size: 0.85em;
- background: rgba(255, 255, 255, 0.08);
+ background: rgba(var(--u-ink-rgb), 0.08);
border-radius: var(--radius-cell);
padding: 0.1em 0.35em;
}
.u-chat-prose pre {
- background: rgba(0, 0, 0, 0.45);
+ background: var(--u-well);
border: 1px solid var(--u-line);
border-radius: var(--radius-panel);
padding: 0.6em 0.8em;
@@ -1555,3 +1688,8 @@ a:hover > .u-mark-arrow {
font-size: 3rem;
line-height: 1;
}
+
+/* 时间岛:投影从页面里收进来(0038) */
+.u-scrub-island {
+ box-shadow: 0 12px 40px var(--u-shadow);
+}
diff --git a/web/src/theme.test.ts b/web/src/theme.test.ts
new file mode 100644
index 00000000..a2898d7b
--- /dev/null
+++ b/web/src/theme.test.ts
@@ -0,0 +1,11 @@
+import { describe, expect, it } from "vitest";
+import { resolve } from "./theme";
+
+describe("theme", () => {
+ it("system follows the OS, the other two do not", () => {
+ expect(resolve("system", "light")).toBe("light");
+ expect(resolve("system", "dark")).toBe("dark");
+ expect(resolve("light", "dark")).toBe("light");
+ expect(resolve("dark", "light")).toBe("dark");
+ });
+});
diff --git a/web/src/theme.ts b/web/src/theme.ts
new file mode 100644
index 00000000..8db843d5
--- /dev/null
+++ b/web/src/theme.ts
@@ -0,0 +1,70 @@
+/* 主题:暗 / 浅 / 跟系统。**默认暗**——那是这个界面的本色;浅色是给白天对着
+ * 它看八小时的人的。存在浏览器里(`utopia.theme`),不经过后端:看的人自己定,
+ * 与语言同一个道理(0004)。
+ *
+ * 落在 `<html data-theme="dark|light">` 上,且**只落这两个值**:「system」在这里
+ * 就解成两者之一,CSS 只认 data-theme,不另写一套 prefers-color-scheme 的块——
+ * 同一套令牌写两遍迟早分叉。首帧前 index.html 里有一段同样的逻辑,为的是
+ * 刷新时不闪一下暗底。 */
+
+export type Theme = "system" | "light" | "dark";
+export type Resolved = "light" | "dark";
+
+const KEY = "utopia.theme";
+const listeners = new Set<(t: Resolved) => void>();
+
+function stored(): Theme {
+ try {
+ const v = localStorage.getItem(KEY);
+ return v === "light" || v === "dark" || v === "system" ? v : "dark";
+ } catch {
+ return "dark";
+ }
+}
+
+export function systemPrefers(): Resolved {
+ return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: light)").matches
+ ? "light"
+ : "dark";
+}
+
+export function resolve(t: Theme, system: Resolved = systemPrefers()): Resolved {
+ return t === "system" ? system : t;
+}
+
+export function getTheme(): Theme {
+ return stored();
+}
+
+export function resolvedTheme(): Resolved {
+ return resolve(stored());
+}
+
+function apply(r: Resolved) {
+ document.documentElement.dataset.theme = r;
+ for (const fn of listeners) fn(r);
+}
+
+export function setTheme(t: Theme) {
+ try {
+ localStorage.setItem(KEY, t);
+ } catch {
+ /* 隐私模式:不存,这一次会话内仍生效 */
+ }
+ apply(resolve(t));
+}
+
+/** 主题落定后要重画的东西(画布读的是算出来的令牌,CSS 不会替它刷新)在这里挂 */
+export function onThemeChange(fn: (t: Resolved) => void): () => void {
+ listeners.add(fn);
+ return () => listeners.delete(fn);
+}
+
+/** 启动时调一次:把存的选择落到 <html>,并跟着系统偏好变(只在选了 system 时) */
+export function initTheme() {
+ apply(resolvedTheme());
+ if (typeof matchMedia !== "function") return;
+ matchMedia("(prefers-color-scheme: light)").addEventListener("change", () => {
+ if (stored() === "system") apply(systemPrefers());
+ });
+}
diff --git a/web/src/ui/index.tsx b/web/src/ui/index.tsx
index 1ff47e48..98f43cf7 100644
--- a/web/src/ui/index.tsx
+++ b/web/src/ui/index.tsx
@@ -655,50 +655,8 @@ export function MultiSearchSelect({
);
}
-/* ---------- ColorPicker(精选色板 + hex 兜底;实体色刻意不开放全色域) ----------
- **改这里就得改 `crates/utopia-store/src/palette.rs`**:手动挑的色与自动按 key
- 取的色必须来自同一组,否则一张图里会出现两套配色。那边有测试盯着,改漏了会红。 */
-export const ENTITY_PALETTE = [
- "#7fd0ff",
- "#5fa8ff",
- "#5fd4d0",
- "#63e2b7",
- "#4cc38a",
- "#a8d878",
- "#ffd479",
- "#f2b66d",
- "#ff9d76",
- "#ff8a9e",
- "#ff9daf",
- "#e797d8",
- "#c4a5ff",
- "#9fa8ff",
- "#8ea5bd",
- "#b3b9c4",
-];
-
-/**
- * 类的 key → 颜色。**必须与 `crates/utopia-store/src/palette.rs` 的
- * `color_for_key` 逐位一致**:新建类时前端先按 key 挑一个显示出来,
- * 用户不改就这么存下去;而导入/消解那条路是后端算的。两边算得不一样,
- * 同一个 key 就会因为「谁建的」而拿到不同颜色。
- *
- * FNV-1a + 雪崩混合。用 BigInt 是因为 JS 的位运算是 32 位的,
- * 而这里要的是 64 位乘法——用 Number 做会静默丢高位,
- * 算出来跟 Rust 对不上,且不会有任何报错。
- */
-export function colorForKey(key: string): string {
- let h = 0xcbf29ce484222325n;
- const M = (1n << 64n) - 1n;
- for (const b of new TextEncoder().encode(key)) {
- h = (h ^ BigInt(b)) & M;
- h = (h * 0x100000001b3n) & M;
- }
- h = (h ^ (h >> 33n)) & M;
- h = (h * 0xff51afd7ed558ccdn) & M;
- h = (h ^ (h >> 33n)) & M;
- return ENTITY_PALETTE[Number(h % BigInt(ENTITY_PALETTE.length))];
-}
+import { ENTITY_PALETTE } from "../palette";
+export { ENTITY_PALETTE, colorForKey } from "../palette";
export function ColorPicker({
value,