@hia/theme-core 0.0.0-stage → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +12 -2
- package/dist/analyze/analyze-v2.d.ts +34 -0
- package/dist/analyze/analyze-v2.js +447 -0
- package/dist/analyze/analyze.d.ts +3 -0
- package/dist/analyze/analyze.js +176 -0
- package/dist/analyze/types-v2.d.ts +111 -0
- package/dist/analyze/types-v2.js +8 -0
- package/dist/analyze/types.d.ts +32 -0
- package/dist/analyze/types.js +2 -0
- package/dist/analyze/v1-compat.d.ts +18 -0
- package/dist/analyze/v1-compat.js +126 -0
- package/dist/color/color-name.d.ts +29 -0
- package/dist/color/color-name.js +94 -0
- package/dist/color/contrast.d.ts +8 -0
- package/dist/color/contrast.js +29 -0
- package/dist/color/convert.d.ts +21 -0
- package/dist/color/convert.js +111 -0
- package/dist/color/harmony.d.ts +55 -0
- package/dist/color/harmony.js +206 -0
- package/dist/color/scale.d.ts +14 -0
- package/dist/color/scale.js +81 -0
- package/dist/data/brand-colors-part0.d.ts +3 -0
- package/dist/data/brand-colors-part0.js +2 -0
- package/dist/data/brand-colors-part1.d.ts +3 -0
- package/dist/data/brand-colors-part1.js +2 -0
- package/dist/data/brand-colors-part2.d.ts +3 -0
- package/dist/data/brand-colors-part2.js +2 -0
- package/dist/data/brand-colors-part3.d.ts +3 -0
- package/dist/data/brand-colors-part3.js +2 -0
- package/dist/data/brand-colors-part4.d.ts +3 -0
- package/dist/data/brand-colors-part4.js +2 -0
- package/dist/data/brand-colors.d.ts +20 -0
- package/dist/data/brand-colors.js +7 -0
- package/dist/data/flags-part0.d.ts +3 -0
- package/dist/data/flags-part0.js +2 -0
- package/dist/data/flags-part1.d.ts +3 -0
- package/dist/data/flags-part1.js +2 -0
- package/dist/data/flags.d.ts +17 -0
- package/dist/data/flags.js +4 -0
- package/dist/data/gradients.d.ts +37 -0
- package/dist/data/gradients.js +64 -0
- package/dist/data/mood-primary-colors.d.ts +77 -0
- package/dist/data/mood-primary-colors.js +83 -0
- package/dist/data/nature-colors-part0.d.ts +3 -0
- package/dist/data/nature-colors-part0.js +2 -0
- package/dist/data/nature-colors.d.ts +17 -0
- package/dist/data/nature-colors.js +3 -0
- package/dist/data/palette-packs.d.ts +8 -0
- package/dist/data/palette-packs.js +23 -0
- package/dist/data/presets.d.ts +4 -0
- package/dist/data/presets.js +69 -0
- package/dist/data/primaryCandidates.d.ts +4 -0
- package/dist/data/primaryCandidates.js +575 -0
- package/dist/data/social-colors-part0.d.ts +3 -0
- package/dist/data/social-colors-part0.js +2 -0
- package/dist/data/social-colors.d.ts +17 -0
- package/dist/data/social-colors.js +3 -0
- package/dist/data/tone-presets.d.ts +40 -0
- package/dist/data/tone-presets.js +179 -0
- package/dist/data/traditional-colors-part0.d.ts +3 -0
- package/dist/data/traditional-colors-part0.js +2 -0
- package/dist/data/traditional-colors-part1.d.ts +3 -0
- package/dist/data/traditional-colors-part1.js +2 -0
- package/dist/data/traditional-colors-part2.d.ts +3 -0
- package/dist/data/traditional-colors-part2.js +2 -0
- package/dist/data/traditional-colors-part3.d.ts +3 -0
- package/dist/data/traditional-colors-part3.js +2 -0
- package/dist/data/traditional-colors-part4.d.ts +3 -0
- package/dist/data/traditional-colors-part4.js +2 -0
- package/dist/data/traditional-colors-part5.d.ts +3 -0
- package/dist/data/traditional-colors-part5.js +2 -0
- package/dist/data/traditional-colors-part6.d.ts +3 -0
- package/dist/data/traditional-colors-part6.js +2 -0
- package/dist/data/traditional-colors-part7.d.ts +3 -0
- package/dist/data/traditional-colors-part7.js +2 -0
- package/dist/data/traditional-colors-part8.d.ts +3 -0
- package/dist/data/traditional-colors-part8.js +2 -0
- package/dist/data/traditional-colors.d.ts +18 -0
- package/dist/data/traditional-colors.js +11 -0
- package/dist/export/inject-template.d.ts +10 -0
- package/dist/export/inject-template.js +99 -0
- package/dist/export/registry.d.ts +12 -0
- package/dist/export/registry.js +24 -0
- package/dist/export/shared.d.ts +13 -0
- package/dist/export/shared.js +45 -0
- package/dist/export/tailwind.d.ts +10 -0
- package/dist/export/tailwind.js +85 -0
- package/dist/export/types.d.ts +30 -0
- package/dist/export/types.js +2 -0
- package/dist/export/wordpress.d.ts +14 -0
- package/dist/export/wordpress.js +225 -0
- package/dist/gradient/render.d.ts +13 -0
- package/dist/gradient/render.js +180 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +89 -0
- package/dist/pack.d.ts +25 -0
- package/dist/pack.js +168 -0
- package/dist/share.d.ts +38 -0
- package/dist/share.js +245 -0
- package/dist/studio.d.ts +25 -0
- package/dist/studio.js +155 -0
- package/dist/theme.d.ts +5 -0
- package/dist/theme.js +110 -0
- package/dist/token/css.d.ts +3 -0
- package/dist/token/css.js +42 -0
- package/dist/token/json.d.ts +3 -0
- package/dist/token/json.js +34 -0
- package/dist/token/typography.d.ts +9 -0
- package/dist/token/typography.js +67 -0
- package/dist/types.d.ts +148 -0
- package/dist/types.js +12 -0
- package/package.json +57 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HIA-Theme-Builder contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hia/theme-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
跨 Web、Electron、Tauri 共用的纯主题领域核心。
|
|
4
|
+
|
|
5
|
+
- 无 DOM / localStorage / Electron / Tauri 依赖。
|
|
6
|
+
- 颜色换算、色阶、副色生成、渐变渲染、预设管理、Token 导出、网址取色分析集中于此。
|
|
7
|
+
- Web 只负责 DOM 渲染;桌面端复用相同 UI 并注入平台桥。
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {createThemeDraft,buildThemeFromDraft,exportThemeCss} from '@hia/theme-core';
|
|
11
|
+
const theme=buildThemeFromDraft(createThemeDraft({primary:'#0047AB',secondary:'#00A8D3'}));
|
|
12
|
+
console.log(exportThemeCss(theme));
|
|
13
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lang zh-CN V2 主题分析:把浏览器端采集的 ColorEvidence[] 在 OKLab 空间聚类、做角色感知评分,
|
|
3
|
+
* 选出主色/副色/备选并给出置信度与可解释理由。纯算法,不访问 DOM/Canvas/网络。
|
|
4
|
+
* 评分参数集中到 CONFIG,便于 fixture 调参与解释输出(指令第七、八、九节)。
|
|
5
|
+
* @lang en V2 analysis: clusters ColorEvidence[] in OKLab, role-aware scores, picks primary/secondary/alternatives
|
|
6
|
+
* with confidence and explainable reasons. Pure algorithm, no DOM/Canvas/network. Scoring params centralized
|
|
7
|
+
* in CONFIG for fixture tuning and explainable output (spec §7/§8/§9).
|
|
8
|
+
*/
|
|
9
|
+
import type { ColorEvidence, ColorSource, SemanticRole, WebsiteColorSignalsV2, WebsiteThemeAnalysisV2 } from './types-v2.js';
|
|
10
|
+
interface Cluster {
|
|
11
|
+
familyId: string;
|
|
12
|
+
repHex: string;
|
|
13
|
+
repOklch: {
|
|
14
|
+
L: number;
|
|
15
|
+
C: number;
|
|
16
|
+
h: number;
|
|
17
|
+
};
|
|
18
|
+
evidence: ColorEvidence[];
|
|
19
|
+
count: number;
|
|
20
|
+
areaMax: number;
|
|
21
|
+
aboveFoldMax: number;
|
|
22
|
+
pixelSum: number;
|
|
23
|
+
sources: ColorSource[];
|
|
24
|
+
roles: SemanticRole[];
|
|
25
|
+
tokenNames: string[];
|
|
26
|
+
contexts: string[];
|
|
27
|
+
selectors: string[];
|
|
28
|
+
frameKinds: Array<'main' | 'same-origin' | 'third-party'>;
|
|
29
|
+
hasBrandToken: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare function clusterEvidence(evidence: ColorEvidence[]): Cluster[];
|
|
32
|
+
export declare function analyzeThemeV2(signals: WebsiteColorSignalsV2): WebsiteThemeAnalysisV2;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=analyze-v2.d.ts.map
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lang zh-CN V2 主题分析:把浏览器端采集的 ColorEvidence[] 在 OKLab 空间聚类、做角色感知评分,
|
|
3
|
+
* 选出主色/副色/备选并给出置信度与可解释理由。纯算法,不访问 DOM/Canvas/网络。
|
|
4
|
+
* 评分参数集中到 CONFIG,便于 fixture 调参与解释输出(指令第七、八、九节)。
|
|
5
|
+
* @lang en V2 analysis: clusters ColorEvidence[] in OKLab, role-aware scores, picks primary/secondary/alternatives
|
|
6
|
+
* with confidence and explainable reasons. Pure algorithm, no DOM/Canvas/network. Scoring params centralized
|
|
7
|
+
* in CONFIG for fixture tuning and explainable output (spec §7/§8/§9).
|
|
8
|
+
*/
|
|
9
|
+
import { hexToOklch, oklchToRgb, rgbToHex, normalizeHex, validHex } from '../color/convert.js';
|
|
10
|
+
import { contrast } from '../color/contrast.js';
|
|
11
|
+
import { generateSecondarySet } from '../color/harmony.js';
|
|
12
|
+
// OKLab 近似聚类阈值(指令七节建议 0.025~0.04,取 0.03 作 fixture 调参锚点)。
|
|
13
|
+
// <lang zh-CN>同一品牌色的 hover/active/浅背景属不同候选但共享 familyId。</lang>
|
|
14
|
+
// <lang en>Hover/active/light-bg of one brand are distinct candidates but share familyId.</lang>
|
|
15
|
+
const CLUSTER_DIST = 0.03;
|
|
16
|
+
// 来源基准分(指令八节基线,集中配置)。
|
|
17
|
+
// <lang zh-CN>显式 token > logo/CTA > meta/manifest > header > button/link > 背景 > border > 文本。</lang>
|
|
18
|
+
// <lang en>Explicit token > logo/CTA > meta/manifest > header > button/link > bg > border > text.</lang>
|
|
19
|
+
const SOURCE_BASE = {
|
|
20
|
+
'css-token': 10,
|
|
21
|
+
'svg-fill': 9,
|
|
22
|
+
'svg-stroke': 8,
|
|
23
|
+
'computed-border': 1.5,
|
|
24
|
+
'gradient-stop': 3,
|
|
25
|
+
'meta-theme': 7.5,
|
|
26
|
+
'manifest-theme': 7.5,
|
|
27
|
+
'computed-background': 2.5,
|
|
28
|
+
'computed-text': 0.8,
|
|
29
|
+
'logo-raster': 9,
|
|
30
|
+
'image-dominant': 6,
|
|
31
|
+
'screenshot-ui': 3,
|
|
32
|
+
'screenshot-artwork': 0.75
|
|
33
|
+
};
|
|
34
|
+
// 语义乘数(指令八节基线)。
|
|
35
|
+
// <lang zh-CN>brand/primary 最高,status 强惩罚,surface/text 竞争主色时降权。</lang>
|
|
36
|
+
// <lang en>brand/primary highest; status penalized; surface/text down-weighted when competing for primary.</lang>
|
|
37
|
+
const SEMANTIC_MULT = {
|
|
38
|
+
brand: 1.8,
|
|
39
|
+
primary: 1.8,
|
|
40
|
+
cta: 1.6,
|
|
41
|
+
secondary: 1.4,
|
|
42
|
+
accent: 1.4,
|
|
43
|
+
link: 1.2,
|
|
44
|
+
icon: 1.0,
|
|
45
|
+
surface: 0.4,
|
|
46
|
+
text: 0.4,
|
|
47
|
+
border: 1.0,
|
|
48
|
+
status: 0.1,
|
|
49
|
+
unknown: 1.0
|
|
50
|
+
};
|
|
51
|
+
// 帧权重:主帧 1.0,third-party 0.2(指令八节)。
|
|
52
|
+
// <lang zh-CN>同源帧全权,跨域帧仅弱证据。</lang>
|
|
53
|
+
// <lang en>Same-origin full weight; third-party weak.</lang>
|
|
54
|
+
const FRAME_WEIGHT = {
|
|
55
|
+
'main': 1.0,
|
|
56
|
+
'same-origin': 1.0,
|
|
57
|
+
'third-party': 0.2
|
|
58
|
+
};
|
|
59
|
+
// 中性/低彩度判定:白/近黑与极低彩度不参与主色评选。
|
|
60
|
+
// <lang zh-CN>单色品牌也可能合法,故中性色不硬删除,只在评分中强降权。</lang>
|
|
61
|
+
// <lang en>Mono brands are legal; neutrals are strongly down-weighted, not deleted.</lang>
|
|
62
|
+
function isNeutral(hex) {
|
|
63
|
+
const { C, L } = hexToOklch(hex);
|
|
64
|
+
return C < 0.02 || L > 0.985 || L < 0.015;
|
|
65
|
+
}
|
|
66
|
+
// 把一组证据按 OKLab 距离聚成 cluster。
|
|
67
|
+
// <lang zh-CN>首次出现建立聚类,后续同簇合并;代表色在聚合结束后按 token 优先/面积加权确定。</lang>
|
|
68
|
+
// <lang en>First sight opens a cluster; later near colors merge; representative decided after by token-priority/area.</lang>
|
|
69
|
+
export function clusterEvidence(evidence) {
|
|
70
|
+
const clusters = [];
|
|
71
|
+
for (const e of evidence) {
|
|
72
|
+
if (!validHex(e.hex))
|
|
73
|
+
continue;
|
|
74
|
+
const ok = hexToOklch(e.hex);
|
|
75
|
+
// OKLab 欧氏距离近似(L 直接差,a/b 用 C/h 投影差)。
|
|
76
|
+
// <lang zh-CN>用 L 差 + 彩度差 + 色相环差(按 C 加权)作为近似距离。</lang>
|
|
77
|
+
// <lang en>Approx distance: L diff + chroma diff + hue-ring diff (C-weighted).</lang>
|
|
78
|
+
const dist = (c) => {
|
|
79
|
+
const dl = Math.abs(ok.L - c.repOklch.L);
|
|
80
|
+
const dc = Math.abs(ok.C - c.repOklch.C);
|
|
81
|
+
const dh = Math.min(Math.abs(ok.h - c.repOklch.h), 360 - Math.abs(ok.h - c.repOklch.h)) / 180 * Math.max(ok.C, c.repOklch.C);
|
|
82
|
+
return dl + dc + dh;
|
|
83
|
+
};
|
|
84
|
+
let target;
|
|
85
|
+
let bestD = CLUSTER_DIST;
|
|
86
|
+
for (const c of clusters) {
|
|
87
|
+
const d = dist(c);
|
|
88
|
+
if (d <= bestD) {
|
|
89
|
+
bestD = d;
|
|
90
|
+
target = c;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!target) {
|
|
94
|
+
target = {
|
|
95
|
+
familyId: 'f' + clusters.length,
|
|
96
|
+
repHex: e.hex, repOklch: ok, evidence: [],
|
|
97
|
+
count: 0, areaMax: 0, aboveFoldMax: 0, pixelSum: 0,
|
|
98
|
+
sources: [], roles: [], tokenNames: [], contexts: [], selectors: [],
|
|
99
|
+
frameKinds: [], hasBrandToken: false
|
|
100
|
+
};
|
|
101
|
+
clusters.push(target);
|
|
102
|
+
}
|
|
103
|
+
target.evidence.push(e);
|
|
104
|
+
target.count += e.count || 1;
|
|
105
|
+
target.areaMax = Math.max(target.areaMax ?? 0, e.areaRatio ?? 0);
|
|
106
|
+
target.aboveFoldMax = Math.max(target.aboveFoldMax ?? 0, e.aboveFoldRatio ?? 0);
|
|
107
|
+
target.pixelSum += e.pixelRatio ?? 0;
|
|
108
|
+
if (e.source && !target.sources.includes(e.source))
|
|
109
|
+
target.sources.push(e.source);
|
|
110
|
+
if (e.role && !target.roles.includes(e.role))
|
|
111
|
+
target.roles.push(e.role);
|
|
112
|
+
for (const t of e.tokenNames ?? [])
|
|
113
|
+
if (!target.tokenNames.includes(t))
|
|
114
|
+
target.tokenNames.push(t);
|
|
115
|
+
for (const cx of e.contexts ?? [])
|
|
116
|
+
if (!target.contexts.includes(cx))
|
|
117
|
+
target.contexts.push(cx);
|
|
118
|
+
for (const s of e.selectors ?? [])
|
|
119
|
+
if (!target.selectors.includes(s))
|
|
120
|
+
target.selectors.push(s);
|
|
121
|
+
if (e.frameKind && !target.frameKinds.includes(e.frameKind))
|
|
122
|
+
target.frameKinds.push(e.frameKind);
|
|
123
|
+
// 显式品牌 token 命中(变量名含 brand/primary/accent/secondary)。
|
|
124
|
+
// <lang zh-CN>仅当角色为 brand/primary/accent/secondary 或 token 名含关键词时标记。</lang>
|
|
125
|
+
// <lang en>Marked when role is brand/primary/accent/secondary or token name has keywords.</lang>
|
|
126
|
+
if (/brand|primary|accent|secondary/i.test((e.tokenNames ?? []).join(' ')) || ['brand', 'primary', 'accent', 'secondary'].includes(e.role)) {
|
|
127
|
+
target.hasBrandToken = true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// 确定代表色:优先取含 brand/primary token 的证据 hex;否则按面积加权均值映射回 sRGB。
|
|
131
|
+
// <lang zh-CN>保证代表色是真实出现过的色值,而非凭空均值。</lang>
|
|
132
|
+
// <lang en>Representative is a real observed value, not an invented mean.</lang>
|
|
133
|
+
for (const c of clusters) {
|
|
134
|
+
const tokenHex = c.evidence.find(e => /brand|primary|accent|secondary/i.test((e.tokenNames ?? []).join(' ')))?.hex;
|
|
135
|
+
if (tokenHex && validHex(tokenHex)) {
|
|
136
|
+
c.repHex = normalizeHex(tokenHex);
|
|
137
|
+
c.repOklch = hexToOklch(c.repHex);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// 面积加权均值(在 OKLab 空间),再映射回 sRGB 取最接近的有效 hex。
|
|
141
|
+
// <lang zh-CN>避免极端色;加权平均后再量化到有效 sRGB。</lang>
|
|
142
|
+
// <lang en>Avoid extremes; average in OKLab then quantize to valid sRGB.</lang>
|
|
143
|
+
let L = 0, a = 0, b = 0, wsum = 0;
|
|
144
|
+
for (const e of c.evidence) {
|
|
145
|
+
const w = (e.areaRatio ?? 0.01) + (e.count ?? 1) * 0.001;
|
|
146
|
+
const o = hexToOklch(e.hex);
|
|
147
|
+
const rad = o.h * Math.PI / 180;
|
|
148
|
+
L += o.L * w;
|
|
149
|
+
a += o.C * Math.cos(rad) * w;
|
|
150
|
+
b += o.C * Math.sin(rad) * w;
|
|
151
|
+
wsum += w;
|
|
152
|
+
}
|
|
153
|
+
if (wsum > 0) {
|
|
154
|
+
const [r, g, bl] = oklchToRgb(L / wsum, Math.hypot(a, b) / wsum, (Math.atan2(b, a) * 180 / Math.PI + 360) % 360);
|
|
155
|
+
c.repHex = normalizeHex(rgbToHex([Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, bl))]));
|
|
156
|
+
c.repOklch = hexToOklch(c.repHex);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return clusters;
|
|
160
|
+
}
|
|
161
|
+
// 对单个聚类打分(指令八节公式结构)。
|
|
162
|
+
// <lang zh-CN>sourceBase × semantic × frame × (1+0.35 log1p count) × (1+0.6√area+0.3√aboveFold) + 互证 - 状态惩罚。</lang>
|
|
163
|
+
// <lang en>sourceBase × semantic × frame × (1+0.35 log1p count) × (1+0.6√area+0.3√aboveFold) + corroboration - status penalty.</lang>
|
|
164
|
+
function scoreCluster(c) {
|
|
165
|
+
const reasons = [];
|
|
166
|
+
// 若聚类整体是中性色,强降权(不硬删)。
|
|
167
|
+
// <lang zh-CN>白/黑/灰作为 surface/text 角色降权,而不是硬删除。</lang>
|
|
168
|
+
// <lang en>White/black/gray down-weighted as surface/text, not hard-deleted.</lang>
|
|
169
|
+
if (isNeutral(c.repHex)) {
|
|
170
|
+
reasons.push(`中性/低彩度色 ${c.repHex},已降权`);
|
|
171
|
+
return { score: 0.05, reasons };
|
|
172
|
+
}
|
|
173
|
+
let total = 0;
|
|
174
|
+
// 零实际使用惩罚:仅 token 声明(无 computed/screenshot 来源,且面积与像素均为 0)的品牌色,
|
|
175
|
+
// 不能仅凭变量名获胜(指令八节/十四节:unused token 不得压过有实际使用的颜色)。
|
|
176
|
+
// <lang zh-CN>此时整体分数强降权,让真实出现在 DOM/截图的颜色胜出。</lang>
|
|
177
|
+
// <lang en>Strongly down-weight so colors actually observed in DOM/screenshot win.</lang>
|
|
178
|
+
const onlyDeclaredToken = c.sources.every(s => s === 'css-token')
|
|
179
|
+
&& !c.sources.some(s => s.startsWith('computed') || s.startsWith('screenshot'))
|
|
180
|
+
&& c.areaMax <= 0 && c.pixelSum <= 0;
|
|
181
|
+
const declaredPenalty = onlyDeclaredToken ? 0.15 : 1;
|
|
182
|
+
// 同色多来源/多角色分别计贡献(避免只取最高来源而漏掉互证)。
|
|
183
|
+
// <lang zh-CN>每条 evidence 独立计 sourceBase×semantic×frame,再汇总。</lang>
|
|
184
|
+
// <lang en>Each evidence contributes sourceBase×semantic×frame independently, then summed.</lang>
|
|
185
|
+
for (const e of c.evidence) {
|
|
186
|
+
const sb = SOURCE_BASE[e.source] ?? 1;
|
|
187
|
+
const sm = SEMANTIC_MULT[e.role] ?? 1;
|
|
188
|
+
const fw = FRAME_WEIGHT[e.frameKind ?? 'main'] ?? 1;
|
|
189
|
+
const sat = (1 + 0.35 * Math.log1p(e.count ?? 1)) * (1 + 0.6 * Math.sqrt(e.areaRatio ?? 0) + 0.3 * Math.sqrt(e.aboveFoldRatio ?? 0));
|
|
190
|
+
let s = sb * sm * fw * sat;
|
|
191
|
+
if (e.source === 'screenshot-artwork')
|
|
192
|
+
s *= 0.3; // artwork 显著降权
|
|
193
|
+
total += s;
|
|
194
|
+
}
|
|
195
|
+
// 跨来源互证加分(指令八节):token+DOM、DOM+screenshot-ui、logo+CTA、多区域。
|
|
196
|
+
// <lang zh-CN>满足互证条件即叠加固定加分,体现证据融合。</lang>
|
|
197
|
+
// <lang en>Corroboration adds fixed bonus when conditions met.</lang>
|
|
198
|
+
const hasToken = c.sources.includes('css-token');
|
|
199
|
+
const hasDom = c.sources.some(s => s.startsWith('computed'));
|
|
200
|
+
const hasUiShot = c.sources.includes('screenshot-ui');
|
|
201
|
+
const hasLogo = c.sources.includes('svg-fill') || c.sources.includes('logo-raster');
|
|
202
|
+
const regions = new Set(c.roles);
|
|
203
|
+
if (hasToken && hasDom) {
|
|
204
|
+
total += 3;
|
|
205
|
+
reasons.push('CSS token 与 DOM 实际使用互证');
|
|
206
|
+
}
|
|
207
|
+
if (hasDom && hasUiShot) {
|
|
208
|
+
total += 2;
|
|
209
|
+
reasons.push('DOM 与 UI-only 截图互证');
|
|
210
|
+
}
|
|
211
|
+
if (hasLogo && c.roles.includes('cta')) {
|
|
212
|
+
total += 2;
|
|
213
|
+
reasons.push('logo 与 CTA 互证');
|
|
214
|
+
}
|
|
215
|
+
if (regions.size >= 3) {
|
|
216
|
+
total += 1.5;
|
|
217
|
+
reasons.push(`在 ${regions.size} 个语义区域出现`);
|
|
218
|
+
}
|
|
219
|
+
// 状态色惩罚:status 角色且非品牌 token 时强惩罚。
|
|
220
|
+
// <lang zh-CN>状态红绿橙不能凭色相胜出。</lang>
|
|
221
|
+
// <lang en>Status red/green/orange must not win by hue.</lang>
|
|
222
|
+
if (c.roles.includes('status') && !c.hasBrandToken) {
|
|
223
|
+
total *= 0.2;
|
|
224
|
+
reasons.push('状态色(success/warning/error)已降权,不按色相胜出');
|
|
225
|
+
}
|
|
226
|
+
// 主帧多区域覆盖的稳定性加分。
|
|
227
|
+
// <lang zh-CN>主帧出现即视作稳定。</lang>
|
|
228
|
+
// <lang en>Present in main frame counts as stable.</lang>
|
|
229
|
+
if (c.frameKinds.includes('main')) {
|
|
230
|
+
total += 0.5;
|
|
231
|
+
}
|
|
232
|
+
// 零实际使用 token 整体强降权(在互证/主帧加分之后统一施加)。
|
|
233
|
+
// <lang zh-CN>仅声明未出现的品牌色,总分乘以 declaredPenalty,避免仅凭变量名胜出。</lang>
|
|
234
|
+
// <lang en>Declared-only brand token gets total×declaredPenalty so it cannot win by name alone.</lang>
|
|
235
|
+
if (declaredPenalty < 1) {
|
|
236
|
+
total *= declaredPenalty;
|
|
237
|
+
reasons.push('品牌 token 仅声明未实际使用,已强降权');
|
|
238
|
+
}
|
|
239
|
+
reasons.push(`覆盖来源 ${c.sources.join('/')},频次 ${c.count},面积 ${(c.areaMax * 100).toFixed(1)}%`);
|
|
240
|
+
return { score: Math.max(0, total), reasons };
|
|
241
|
+
}
|
|
242
|
+
// 从聚类列表选择主色(brand/primary/cta/accent 优先,非中性且分数最高)。
|
|
243
|
+
// <lang zh-CN>不让 surface/text/border/status 与其同池竞争主色。</lang>
|
|
244
|
+
// <lang en>Excludes surface/text/border/status from the primary pool.</lang>
|
|
245
|
+
function pickPrimary(clusters, scored) {
|
|
246
|
+
const pool = clusters.filter(c => {
|
|
247
|
+
const role = c.roles[0] ?? 'unknown';
|
|
248
|
+
const competing = ['surface', 'text', 'border', 'status', 'unknown'].includes(role) && !c.hasBrandToken;
|
|
249
|
+
// 中性色(黑/白/灰)默认排除主色池,但显式品牌 token 的中性色合法(单色品牌)。
|
|
250
|
+
// <lang zh-CN>单色黑白品牌不应被 isNeutral 误杀,故 brand token 例外保留。</lang>
|
|
251
|
+
// <lang en>Mono black/white brands must survive isNeutral; brand token is the exception.</lang>
|
|
252
|
+
const neutralButBrand = isNeutral(c.repHex) && c.hasBrandToken;
|
|
253
|
+
return !competing && (!isNeutral(c.repHex) || neutralButBrand);
|
|
254
|
+
});
|
|
255
|
+
if (!pool.length)
|
|
256
|
+
return null;
|
|
257
|
+
pool.sort((a, b) => (scored.get(b)?.score ?? 0) - (scored.get(a)?.score ?? 0));
|
|
258
|
+
return pool[0];
|
|
259
|
+
}
|
|
260
|
+
// 选择副色(指令九节顺序):显式 secondary/accent token 且使用 → 高置信次级 → 与主色区分 → derived fallback。
|
|
261
|
+
// <lang zh-CN>derived 必须标记 origin:'harmony-fallback'。</lang>
|
|
262
|
+
// <lang en>derived must be tagged origin:'harmony-fallback'.</lang>
|
|
263
|
+
function pickSecondary(primary, clusters, scored) {
|
|
264
|
+
if (!primary)
|
|
265
|
+
return { cluster: null, derived: false };
|
|
266
|
+
const pOk = primary.repOklch;
|
|
267
|
+
// 候选:非主色、非中性、与主色有足够区分。
|
|
268
|
+
// <lang zh-CN>区分度用色相环差或亮度差 + 对比度阈值。</lang>
|
|
269
|
+
// <lang en>Distinctness via hue-ring or lightness gap + contrast threshold.</lang>
|
|
270
|
+
const candidates = clusters.filter(c => {
|
|
271
|
+
if (c.repHex === primary.repHex || isNeutral(c.repHex))
|
|
272
|
+
return false;
|
|
273
|
+
const o = c.repOklch;
|
|
274
|
+
const dh = Math.min(Math.abs(o.h - pOk.h), 360 - Math.abs(o.h - pOk.h));
|
|
275
|
+
return (dh > 25 || Math.abs(o.L - pOk.L) > 0.12) && contrast(primary.repHex, c.repHex) > 1.6;
|
|
276
|
+
});
|
|
277
|
+
// 1) 显式 secondary/accent token 且实际使用(有 computed 来源)。
|
|
278
|
+
// <lang zh-CN>变量名含 secondary/accent 即优先。</lang>
|
|
279
|
+
// <lang en>Token name containing secondary/accent wins first.</lang>
|
|
280
|
+
const explicit = candidates.find(c => /secondary|accent/i.test((c.tokenNames ?? []).join(' ')) && c.sources.some(s => s.startsWith('computed') || s === 'css-token'));
|
|
281
|
+
if (explicit)
|
|
282
|
+
return { cluster: explicit, derived: false };
|
|
283
|
+
// 2) 高置信次级 CTA/品牌候选(分数较高)。
|
|
284
|
+
// <lang zh-CN>取候选中分数最高者,若显著则采用。</lang>
|
|
285
|
+
// <lang en>Highest-scored candidate if meaningful.</lang>
|
|
286
|
+
const ranked = candidates.filter(c => (scored.get(c)?.score ?? 0) > 0).sort((a, b) => (scored.get(b)?.score ?? 0) - (scored.get(a)?.score ?? 0));
|
|
287
|
+
if (ranked.length && (scored.get(ranked[0])?.score ?? 0) >= (scored.get(primary)?.score ?? 1) * 0.3) {
|
|
288
|
+
return { cluster: ranked[0], derived: false };
|
|
289
|
+
}
|
|
290
|
+
// 3) 无真实副色 → 由主色协调生成(derived fallback)。
|
|
291
|
+
// <lang zh-CN>明确标记非网页原始颜色。</lang>
|
|
292
|
+
// <lang en>Clearly tagged as not from the page.</lang>
|
|
293
|
+
return { cluster: null, derived: true };
|
|
294
|
+
}
|
|
295
|
+
// 把聚类转为 ScoredColorV2(含 OKLCH、置信度、reasons、evidenceSummary)。
|
|
296
|
+
// <lang zh-CN>origin 默认 observed;derived 由调用方覆盖。</lang>
|
|
297
|
+
// <lang en>origin defaults observed; derived overridden by caller.</lang>
|
|
298
|
+
function toScored(c, score, reasons, origin = 'observed') {
|
|
299
|
+
const ok = c.repOklch;
|
|
300
|
+
return {
|
|
301
|
+
hex: c.repHex,
|
|
302
|
+
oklch: { l: ok.L, c: ok.C, h: ok.h },
|
|
303
|
+
score,
|
|
304
|
+
confidence: 0, // 由 analyzeV2 统算
|
|
305
|
+
role: c.roles[0] ?? 'unknown',
|
|
306
|
+
familyId: c.familyId,
|
|
307
|
+
derived: origin === 'harmony-fallback',
|
|
308
|
+
origin,
|
|
309
|
+
reasons,
|
|
310
|
+
evidenceSummary: {
|
|
311
|
+
sources: c.sources,
|
|
312
|
+
sourceFamilies: new Set(c.sources).size,
|
|
313
|
+
count: c.count,
|
|
314
|
+
areaRatio: c.areaMax,
|
|
315
|
+
pixelRatio: c.pixelSum,
|
|
316
|
+
tokenNames: c.tokenNames,
|
|
317
|
+
contexts: c.contexts
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
// 置信度合成(指令九节结构):0.35 证据强度 + 0.25 来源多样 + 0.25 分数差 + 0.15 稳定性。
|
|
322
|
+
// <lang zh-CN>稳定化指主色在主帧出现且多区域覆盖。</lang>
|
|
323
|
+
// <lang en>Stability = primary present in main frame with multi-region coverage.</lang>
|
|
324
|
+
function calcConfidence(primary, scored, top, second) {
|
|
325
|
+
const evidenceStrength = Math.min(1, primary.count / 20 + primary.areaMax);
|
|
326
|
+
const sourceDiversity = Math.min(1, new Set(primary.sources).size / 3);
|
|
327
|
+
const scoreMargin = top > 0 ? Math.max(0, Math.min(1, (top - second) / top)) : 0;
|
|
328
|
+
const stability = (primary.frameKinds.includes('main') ? 0.6 : 0) + Math.min(0.4, primary.roles.length / 5);
|
|
329
|
+
return Math.max(0, Math.min(1, 0.35 * evidenceStrength + 0.25 * sourceDiversity + 0.25 * scoreMargin + 0.15 * stability));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* @lang zh-CN V2 分析主入口:聚类 → 评分 → 选主副色 → 置信度/理由 → alternatives。
|
|
333
|
+
* 纯函数,输入 V2 信号,输出可读分析报告;降级模式(degraded-static)封顶置信度 0.45。
|
|
334
|
+
* @lang en V2 analysis entry: cluster → score → pick primary/secondary → confidence/reasons → alternatives.
|
|
335
|
+
* Pure; degraded-static mode caps confidence at 0.45.
|
|
336
|
+
*/
|
|
337
|
+
// 内部:对一组证据评出主副色、排序与评分映射(light/dark 双模式复用,避免重复实现)。
|
|
338
|
+
// <lang zh-CN>降级封顶、主副色选择、置信度合成逻辑统一;dark 模式复用同一套评分保证跨模式一致。</lang>
|
|
339
|
+
// <lang en>Degrade cap, primary/secondary pick, confidence synthesis unified; dark mode reuses the same scoring for cross-mode consistency.</lang>
|
|
340
|
+
function analyzeColors(evidence, degraded) {
|
|
341
|
+
const cap = degraded ? 0.45 : 1;
|
|
342
|
+
const clusters = clusterEvidence(evidence);
|
|
343
|
+
const scored = new Map();
|
|
344
|
+
for (const c of clusters)
|
|
345
|
+
scored.set(c, scoreCluster(c));
|
|
346
|
+
const sorted = [...clusters].sort((a, b) => (scored.get(b)?.score ?? 0) - (scored.get(a)?.score ?? 0));
|
|
347
|
+
const primary = pickPrimary(clusters, scored);
|
|
348
|
+
const secondaryPick = pickSecondary(primary, clusters, scored);
|
|
349
|
+
const top = primary ? scored.get(primary)?.score ?? 0 : 0;
|
|
350
|
+
const second = sorted[1] ? scored.get(sorted[1])?.score ?? 0 : 0;
|
|
351
|
+
const primaryScored = primary
|
|
352
|
+
? (() => {
|
|
353
|
+
const s = scored.get(primary);
|
|
354
|
+
const sc = toScored(primary, s.score, s.reasons);
|
|
355
|
+
sc.confidence = Math.min(cap, calcConfidence(primary, scored, top, second));
|
|
356
|
+
return sc;
|
|
357
|
+
})()
|
|
358
|
+
: null;
|
|
359
|
+
let secondaryScored = null;
|
|
360
|
+
if (secondaryPick.cluster) {
|
|
361
|
+
const c = secondaryPick.cluster;
|
|
362
|
+
const s = scored.get(c);
|
|
363
|
+
const sc = toScored(c, s.score, s.reasons);
|
|
364
|
+
sc.confidence = Math.min(cap, calcConfidence(c, scored, top, second) * 0.9);
|
|
365
|
+
secondaryScored = sc;
|
|
366
|
+
}
|
|
367
|
+
else if (secondaryPick.derived && primaryScored) {
|
|
368
|
+
// 由主色协调生成副色候选集,取首项作为 derived 副色(明确标记非网页原始颜色)。
|
|
369
|
+
// <lang zh-CN>generateSecondarySet 返回按和谐方案推导的候选数组,首项即默认推荐。</lang>
|
|
370
|
+
// <lang en>generateSecondarySet returns harmony-derived candidates; first is the default pick.</lang>
|
|
371
|
+
const derived = generateSecondarySet(primaryScored.hex);
|
|
372
|
+
const dHex = derived[0]?.hex ?? primaryScored.hex;
|
|
373
|
+
const ok = hexToOklch(dHex);
|
|
374
|
+
secondaryScored = {
|
|
375
|
+
hex: dHex, oklch: { l: ok.L, c: ok.C, h: ok.h },
|
|
376
|
+
score: 0, confidence: Math.min(cap, 0.3), role: 'secondary',
|
|
377
|
+
familyId: primaryScored.familyId + '-d', derived: true, origin: 'harmony-fallback',
|
|
378
|
+
reasons: ['网页无真实副色,由主色协调生成(非网页原始颜色)'],
|
|
379
|
+
evidenceSummary: { sources: [], sourceFamilies: 0, count: 0, areaRatio: 0, pixelRatio: 0, tokenNames: [], contexts: [] }
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
return { primary, primaryScored, secondaryScored, top, second, sorted, scored };
|
|
383
|
+
}
|
|
384
|
+
export function analyzeThemeV2(signals) {
|
|
385
|
+
const baseDiag = signals.diagnostics;
|
|
386
|
+
// 降级模式封顶置信度。
|
|
387
|
+
// <lang zh-CN>静态降级(无真实渲染)证据不完整,置信度上限 0.45。</lang>
|
|
388
|
+
// <lang en>Static degraded (no real render) has incomplete evidence; confidence capped at 0.45.</lang>
|
|
389
|
+
const degraded = baseDiag.mode === 'degraded-static';
|
|
390
|
+
const cap = degraded ? 0.45 : 1;
|
|
391
|
+
// 主(light)证据评分;dark 双模式复用同一评分逻辑评出 dark 主副色。
|
|
392
|
+
// <lang zh-CN>darkMode 由适配器强制 prefers-color-scheme:dark 复采得到。</lang>
|
|
393
|
+
// <lang en>darkMode comes from adapter forcing prefers-color-scheme:dark recapture.</lang>
|
|
394
|
+
const main = analyzeColors(signals.evidence, degraded);
|
|
395
|
+
const { primary, primaryScored, secondaryScored, top, second, sorted, scored } = main;
|
|
396
|
+
const dark = signals.darkMode ? analyzeColors(signals.darkMode.evidence, degraded) : null;
|
|
397
|
+
// logo 色板:由 logo 区域裁剪量化识别的颜色(P5 logo crop)。
|
|
398
|
+
// <lang zh-CN>供 UI 在候选中标注 logo 来源。</lang>
|
|
399
|
+
// <lang en>Surfaced to UI so logo-origin candidates are tagged.</lang>
|
|
400
|
+
const logoHexes = [...new Set(signals.evidence.filter(e => e.isLogo).map(e => e.hex))];
|
|
401
|
+
// 备选:取主色之外分数最高的 8~12 个,跳过中性与状态竞争主色者。
|
|
402
|
+
// <lang zh-CN>覆盖主副色之外的可辨识候选,供操作台展示。</lang>
|
|
403
|
+
// <lang en>Up to 8~12 distinguishable candidates beyond primary/secondary for the studio.</lang>
|
|
404
|
+
const altList = [];
|
|
405
|
+
for (const c of sorted) {
|
|
406
|
+
if (altList.length >= 12)
|
|
407
|
+
break;
|
|
408
|
+
if (primary && c.repHex === primary.repHex)
|
|
409
|
+
continue;
|
|
410
|
+
if (secondaryScored && c.repHex === secondaryScored.hex)
|
|
411
|
+
continue;
|
|
412
|
+
if (isNeutral(c.repHex))
|
|
413
|
+
continue;
|
|
414
|
+
const s = scored.get(c);
|
|
415
|
+
if (s.score <= 0)
|
|
416
|
+
continue;
|
|
417
|
+
const sc = toScored(c, s.score, s.reasons);
|
|
418
|
+
sc.confidence = Math.min(cap, calcConfidence(c, scored, top, second));
|
|
419
|
+
altList.push(sc);
|
|
420
|
+
}
|
|
421
|
+
const confidence = primaryScored?.confidence ?? 0;
|
|
422
|
+
const warnings = [
|
|
423
|
+
...signals.warnings.map(w => w.message)
|
|
424
|
+
];
|
|
425
|
+
if (degraded)
|
|
426
|
+
warnings.push('页面未真实渲染,动态样式/CSS 变量/截图证据缺失,置信度已封顶 0.45');
|
|
427
|
+
return {
|
|
428
|
+
schemaVersion: 2,
|
|
429
|
+
primary: primaryScored,
|
|
430
|
+
secondary: secondaryScored,
|
|
431
|
+
// surfaces/textColors 取自对应角色聚类(前若干)。
|
|
432
|
+
// <lang zh-CN>surface/text 角色单独列出便于 UI 区分配色。</lang>
|
|
433
|
+
// <lang en>surface/text roles listed separately for UI pairing.</lang>
|
|
434
|
+
surfaces: sorted.filter(c => c.roles.includes('surface') && !isNeutral(c.repHex)).slice(0, 4).map(c => { const s = scored.get(c); const sc = toScored(c, s.score, s.reasons); sc.confidence = Math.min(cap, calcConfidence(c, scored, top, second)); return sc; }),
|
|
435
|
+
textColors: sorted.filter(c => c.roles.includes('text')).slice(0, 4).map(c => { const s = scored.get(c); const sc = toScored(c, s.score, s.reasons); sc.confidence = Math.min(cap, calcConfidence(c, scored, top, second)); return sc; }),
|
|
436
|
+
accent: (() => { const c = sorted.find(c => c.roles.includes('accent') && !isNeutral(c.repHex)); if (!c)
|
|
437
|
+
return null; const s = scored.get(c); const sc = toScored(c, s.score, s.reasons); sc.confidence = Math.min(cap, calcConfidence(c, scored, top, second)); return sc; })(),
|
|
438
|
+
alternatives: altList,
|
|
439
|
+
confidence,
|
|
440
|
+
warnings,
|
|
441
|
+
diagnostics: baseDiag,
|
|
442
|
+
darkPrimary: dark?.primaryScored ?? null,
|
|
443
|
+
darkSecondary: dark?.secondaryScored ?? null,
|
|
444
|
+
logoHexes
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
//# sourceMappingURL=analyze-v2.js.map
|