dsh-visualkit 0.0.0-stage → 0.0.2
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/CHANGELOG.md +74 -0
- package/LICENSE +26 -0
- package/README.md +443 -2
- package/THIRD_PARTY.md +55 -0
- package/cordis.patch.yml +29 -0
- package/data/assets/README.md +38 -0
- package/data/assets/icons/brands/bilibili.svg +1 -0
- package/data/assets/icons/brands/discord.svg +1 -0
- package/data/assets/icons/brands/neteasecloudmusic.svg +1 -0
- package/data/assets/icons/brands/qq.svg +1 -0
- package/data/assets/icons/brands/sinaweibo.svg +1 -0
- package/data/assets/icons/brands/steam.svg +1 -0
- package/data/assets/icons/brands/telegram.svg +1 -0
- package/data/assets/icons/brands/tiktok.svg +1 -0
- package/data/assets/icons/brands/twitch.svg +1 -0
- package/data/assets/icons/brands/wechat.svg +1 -0
- package/data/assets/icons/brands/x.svg +1 -0
- package/data/assets/icons/brands/youtube.svg +1 -0
- package/data/assets/icons/lucide/bell.svg +1 -0
- package/data/assets/icons/lucide/calendar.svg +1 -0
- package/data/assets/icons/lucide/clock.svg +1 -0
- package/data/assets/icons/lucide/gamepad-2.svg +1 -0
- package/data/assets/icons/lucide/gift.svg +1 -0
- package/data/assets/icons/lucide/heart.svg +1 -0
- package/data/assets/icons/lucide/mic.svg +1 -0
- package/data/assets/icons/lucide/play.svg +1 -0
- package/data/assets/icons/lucide/star.svg +1 -0
- package/data/assets/icons/lucide/video.svg +1 -0
- package/data/assets/icons/phosphor/LICENSE +21 -0
- package/data/assets/icons/phosphor/atom-duotone.svg +1 -0
- package/data/assets/icons/phosphor/binary-duotone.svg +1 -0
- package/data/assets/icons/phosphor/blueprint-duotone.svg +1 -0
- package/data/assets/icons/phosphor/circuitry-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cloud-arrow-up-duotone.svg +1 -0
- package/data/assets/icons/phosphor/code-duotone.svg +1 -0
- package/data/assets/icons/phosphor/compass-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cpu-duotone.svg +1 -0
- package/data/assets/icons/phosphor/cube-duotone.svg +1 -0
- package/data/assets/icons/phosphor/database-duotone.svg +1 -0
- package/data/assets/icons/phosphor/flask-duotone.svg +1 -0
- package/data/assets/icons/phosphor/frame-corners-duotone.svg +1 -0
- package/data/assets/icons/phosphor/function-duotone.svg +1 -0
- package/data/assets/icons/phosphor/hard-drives-duotone.svg +1 -0
- package/data/assets/icons/phosphor/hexagon-duotone.svg +1 -0
- package/data/assets/icons/phosphor/infinity-duotone.svg +1 -0
- package/data/assets/icons/phosphor/lightning-duotone.svg +1 -0
- package/data/assets/icons/phosphor/magic-wand-duotone.svg +1 -0
- package/data/assets/icons/phosphor/moon-stars-duotone.svg +1 -0
- package/data/assets/icons/phosphor/paint-brush-duotone.svg +1 -0
- package/data/assets/icons/phosphor/pen-nib-duotone.svg +1 -0
- package/data/assets/icons/phosphor/planet-duotone.svg +1 -0
- package/data/assets/icons/phosphor/rocket-launch-duotone.svg +1 -0
- package/data/assets/icons/phosphor/ruler-duotone.svg +1 -0
- package/data/assets/icons/phosphor/selection-plus-duotone.svg +1 -0
- package/data/assets/icons/phosphor/shapes-duotone.svg +1 -0
- package/data/assets/icons/phosphor/sparkle-duotone.svg +1 -0
- package/data/assets/icons/phosphor/sun-horizon-duotone.svg +1 -0
- package/data/assets/icons/phosphor/terminal-window-duotone.svg +1 -0
- package/data/assets/icons/phosphor/triangle-duotone.svg +1 -0
- package/data/assets/icons/phosphor/waveform-duotone.svg +1 -0
- package/data/assets/icons/tabler/alarm.svg +1 -0
- package/data/assets/icons/tabler/badge-ad.svg +1 -0
- package/data/assets/icons/tabler/bell.svg +1 -0
- package/data/assets/icons/tabler/bolt.svg +1 -0
- package/data/assets/icons/tabler/brand-bilibili.svg +1 -0
- package/data/assets/icons/tabler/brand-discord.svg +1 -0
- package/data/assets/icons/tabler/brand-qq.svg +1 -0
- package/data/assets/icons/tabler/brand-twitch.svg +1 -0
- package/data/assets/icons/tabler/brand-wechat.svg +1 -0
- package/data/assets/icons/tabler/brand-weibo.svg +1 -0
- package/data/assets/icons/tabler/brand-youtube.svg +1 -0
- package/data/assets/icons/tabler/broadcast.svg +1 -0
- package/data/assets/icons/tabler/calendar-event.svg +1 -0
- package/data/assets/icons/tabler/calendar-week.svg +1 -0
- package/data/assets/icons/tabler/clock-hour-3.svg +1 -0
- package/data/assets/icons/tabler/coin.svg +1 -0
- package/data/assets/icons/tabler/crown.svg +1 -0
- package/data/assets/icons/tabler/currency-yen.svg +1 -0
- package/data/assets/icons/tabler/device-gamepad-2.svg +1 -0
- package/data/assets/icons/tabler/diamond.svg +1 -0
- package/data/assets/icons/tabler/flame.svg +1 -0
- package/data/assets/icons/tabler/gift.svg +1 -0
- package/data/assets/icons/tabler/headphones.svg +1 -0
- package/data/assets/icons/tabler/heart-filled.svg +1 -0
- package/data/assets/icons/tabler/heart.svg +1 -0
- package/data/assets/icons/tabler/live-view.svg +1 -0
- package/data/assets/icons/tabler/microphone.svg +1 -0
- package/data/assets/icons/tabler/mood-smile.svg +1 -0
- package/data/assets/icons/tabler/mood-tongue.svg +1 -0
- package/data/assets/icons/tabler/movie.svg +1 -0
- package/data/assets/icons/tabler/player-play.svg +1 -0
- package/data/assets/icons/tabler/playlist.svg +1 -0
- package/data/assets/icons/tabler/rocket.svg +1 -0
- package/data/assets/icons/tabler/sparkles.svg +1 -0
- package/data/assets/icons/tabler/star-filled.svg +1 -0
- package/data/assets/icons/tabler/star.svg +1 -0
- package/data/assets/icons/tabler/tag.svg +1 -0
- package/data/assets/icons/tabler/thumb-up.svg +1 -0
- package/data/assets/icons/tabler/ticket.svg +1 -0
- package/data/assets/icons/tabler/video.svg +1 -0
- package/data/assets/illustrations/openpeeps/LICENSE +8 -0
- package/data/assets/illustrations/openpeeps/peep-10.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-100.svg +32 -0
- package/data/assets/illustrations/openpeeps/peep-101.svg +25 -0
- package/data/assets/illustrations/openpeeps/peep-102.svg +28 -0
- package/data/assets/illustrations/openpeeps/peep-103.svg +32 -0
- package/data/assets/illustrations/openpeeps/peep-105.svg +25 -0
- package/data/assets/illustrations/openpeeps/peep-12.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-13.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-16.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-17.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-22.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-25.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-26.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-27.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-30.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-31.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-34.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-38.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-39.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-4.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-41.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-44.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-47.svg +26 -0
- package/data/assets/illustrations/openpeeps/peep-48.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-50.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-51.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-53.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-54.svg +26 -0
- package/data/assets/illustrations/openpeeps/peep-55.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-56.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-58.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-61.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-64.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-68.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-69.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-7.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-72.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-76.svg +31 -0
- package/data/assets/illustrations/openpeeps/peep-77.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-80.svg +27 -0
- package/data/assets/illustrations/openpeeps/peep-81.svg +29 -0
- package/data/assets/illustrations/openpeeps/peep-83.svg +29 -0
- package/data/assets/manifest.json +1 -0
- package/data/assets/parts//342/206/223/346/212/225/345/275/261.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2711.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2712.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2713.png +0 -0
- package/data/assets/parts//344/270/211/350/247/222/345/217/221/345/244/2714.png +0 -0
- package/data/assets/parts//345/217/214/351/251/254/345/260/276.png +0 -0
- package/data/assets/parts//345/217/214/351/251/254/345/260/2761.png +0 -0
- package/data/assets/parts//345/217/2211.png +0 -0
- package/data/assets/parts//345/217/2212.png +0 -0
- package/data/assets/parts//345/217/2213.png +0 -0
- package/data/assets/parts//345/217/221/345/234/210.png +0 -0
- package/data/assets/parts//345/217/221/345/244/2711.png +0 -0
- package/data/assets/parts//345/217/221/345/244/2712.png +0 -0
- package/data/assets/parts//345/217/243/346/260/264/345/267/276/345/211/215.png +0 -0
- package/data/assets/parts//345/217/243/346/260/264/345/267/276/345/220/216.png +0 -0
- package/data/assets/parts//345/217/263/344/270/212/347/234/274/347/235/221.png +0 -0
- package/data/assets/parts//345/217/263/346/211/213.png +0 -0
- package/data/assets/parts//345/217/263/346/211/2131.png +0 -0
- package/data/assets/parts//345/217/263/347/234/274/347/220/203.png +0 -0
- package/data/assets/parts//345/217/263/347/234/274/347/231/275.png +0 -0
- package/data/assets/parts//345/217/263/350/205/277.png +0 -0
- package/data/assets/parts//345/221/206/346/257/233.png +0 -0
- package/data/assets/parts//345/230/264/345/267/264.png +0 -0
- package/data/assets/parts//345/260/276/345/267/264.png +0 -0
- package/data/assets/parts//345/267/246/344/270/212/347/234/274/347/235/221.png +0 -0
- package/data/assets/parts//345/267/246/345/217/214/347/234/274/347/232/256.png +0 -0
- package/data/assets/parts//345/267/246/346/211/213.png +0 -0
- package/data/assets/parts//345/267/246/346/211/2131.png +0 -0
- package/data/assets/parts//345/267/246/347/234/274/347/220/203.png +0 -0
- package/data/assets/parts//345/267/246/347/234/274/347/231/275.png +0 -0
- package/data/assets/parts//345/267/246/350/205/277.png +0 -0
- package/data/assets/parts//346/212/225/345/275/261.png +0 -0
- package/data/assets/parts//347/234/211/346/257/233.png +0 -0
- package/data/assets/parts//347/234/274/347/275/251.png +0 -0
- package/data/assets/parts//350/200/263/346/234/2651.png +0 -0
- package/data/assets/parts//350/200/263/346/234/2652.png +0 -0
- package/data/assets/parts//350/204/221/345/243/263.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/345/211/215.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/345/220/216.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/351/273/221.png +0 -0
- package/data/assets/parts//350/241/243/346/234/215/351/273/2211.png +0 -0
- package/data/assets/parts//350/272/253/344/275/223.png +0 -0
- package/data/assets/parts//351/223/201/346/235/277.png +0 -0
- package/data/assets/parts//351/273/221/347/234/274/345/234/210.png +0 -0
- package/data/assets/parts//351/273/221/347/234/274/345/234/2102.png +0 -0
- package/data/assets/patterns/checker.svg +1 -0
- package/data/assets/patterns/circles-overlap.svg +1 -0
- package/data/assets/patterns/crosshatch.svg +1 -0
- package/data/assets/patterns/diagonal.svg +1 -0
- package/data/assets/patterns/dots.svg +1 -0
- package/data/assets/patterns/grid.svg +1 -0
- package/data/assets/patterns/halftone.svg +1 -0
- package/data/assets/patterns/plus.svg +1 -0
- package/data/assets/patterns/scales.svg +1 -0
- package/data/assets/patterns/triangles.svg +1 -0
- package/data/assets/patterns/waves.svg +1 -0
- package/data/assets/patterns/zigzag.svg +1 -0
- package/data/assets/stickers/openmoji/1F375.svg +12 -0
- package/data/assets/stickers/openmoji/1F9E8.svg +21 -0
- package/data/assets/stickers/twemoji/1f389.svg +1 -0
- package/data/assets/stickers/twemoji/1f3a4.svg +1 -0
- package/data/assets/stickers/twemoji/1f3a7.svg +1 -0
- package/data/assets/stickers/twemoji/1f3ae.svg +1 -0
- package/data/assets/stickers/twemoji/1f44b.svg +1 -0
- package/data/assets/stickers/twemoji/1f44d.svg +1 -0
- package/data/assets/stickers/twemoji/1f494.svg +1 -0
- package/data/assets/stickers/twemoji/1f4a2.svg +1 -0
- package/data/assets/stickers/twemoji/1f4a8.svg +1 -0
- package/data/assets/stickers/twemoji/1f4b5.svg +1 -0
- package/data/assets/stickers/twemoji/1f4d6.svg +1 -0
- package/data/assets/stickers/twemoji/1f525.svg +1 -0
- package/data/assets/stickers/twemoji/1f600.svg +1 -0
- package/data/assets/stickers/twemoji/1f602.svg +1 -0
- package/data/assets/stickers/twemoji/1f60d.svg +1 -0
- package/data/assets/stickers/twemoji/1f62d.svg +1 -0
- package/data/assets/stickers/twemoji/1f680.svg +1 -0
- package/data/assets/stickers/twemoji/1f979.svg +1 -0
- package/data/assets/stickers/twemoji/1f97a.svg +1 -0
- package/data/assets/stickers/twemoji/26a1.svg +1 -0
- package/data/assets/stickers/twemoji/2705.svg +1 -0
- package/data/assets/stickers/twemoji/274c.svg +1 -0
- package/data/assets/stickers/twemoji/2764.svg +1 -0
- package/data/assets/stickers/twemoji/2b50.svg +1 -0
- package/data/assets/stickers/twemoji_1f525.svg +1 -0
- package/data/assets/stickers/twemoji_1f600.svg +1 -0
- package/data/assets/stickers/twemoji_1f979.svg +1 -0
- package/data/assets/stickers/twemoji_2764.svg +1 -0
- package/data/assets/textures/grain-dark.png +0 -0
- package/data/assets/textures/grain-light.png +0 -0
- package/data/assets/textures/grain-warm.png +0 -0
- package/data/lucide-icons.json +1 -0
- package/data/packs/tabler.icons.json +1 -0
- package/icon.svg +26 -0
- package/lib/assets.js +228 -0
- package/lib/audit.js +284 -0
- package/lib/contrast-render.js +213 -0
- package/lib/css-rewrite.js +379 -0
- package/lib/detect.js +701 -0
- package/lib/diff.js +153 -0
- package/lib/emit.js +557 -0
- package/lib/html-context.js +315 -0
- package/lib/icon-render.js +107 -0
- package/lib/icondb.js +402 -0
- package/lib/image-normalize.js +140 -0
- package/lib/index.js +1289 -0
- package/lib/layouts/common.js +48 -0
- package/lib/layouts/image.js +1010 -0
- package/lib/layouts/index.js +10 -0
- package/lib/layouts/intent.js +66 -0
- package/lib/layouts/metrics-text.js +91 -0
- package/lib/layouts/png.js +75 -0
- package/lib/layouts/rules-contrast.js +575 -0
- package/lib/layouts/rules-punct.js +486 -0
- package/lib/layouts/rules-structure.js +182 -0
- package/lib/layouts/source.js +1961 -0
- package/lib/layouts/svg.js +1204 -0
- package/lib/morphcore.js +139 -0
- package/lib/preview.js +225 -0
- package/lib/render-html.js +255 -0
- package/lib/render.js +486 -0
- package/lib/rules.js +267 -0
- package/lib/shadow.js +83 -0
- package/lib/skip.js +56 -0
- package/lib/style.js +688 -0
- package/lib/svg.js +729 -0
- package/lib/taste-image.js +323 -0
- package/lib/taste.js +219 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +92 -4
- package/vendor/morphicons-adapters.iife.js +903 -0
- package/vendor/morphicons.iife.js +2441 -0
- package/vendor/resvg/LICENSE +373 -0
- package/vendor/resvg/index.d.ts +98 -0
- package/vendor/resvg/index.js +23 -0
- package/vendor/resvg/js-binding.d.ts +52 -0
- package/vendor/resvg/js-binding.js +1 -0
- package/vendor/resvg/package.json +138 -0
- package/vendor/resvg/resvgjs.win32-x64-msvc.node +0 -0
package/lib/diff.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* diff.js — 两张图的像素差异:整体差距 + 分区差距 + 热力图。
|
|
3
|
+
*
|
|
4
|
+
* **为什么值得做成工具**:视觉工作最难的不是“改”,而是“改完到底好了没有”。
|
|
5
|
+
* 靠眼睛看只能判断“像不像”,没法回答“这次改动是提升还是回退”。
|
|
6
|
+
* 实测动机:视频站动态页参照实现迭代时,每一轮都要手写一段 node 脚本算差异——
|
|
7
|
+
* 同一个需求写第三遍,就该进工具了。
|
|
8
|
+
*
|
|
9
|
+
* 输出三件东西:
|
|
10
|
+
* 1. 均像素差(0-255)—— 一个数字,能横比多轮迭代;
|
|
11
|
+
* 2. 分区差—— 告诉你差异集中在哪,而不是只能看整图;
|
|
12
|
+
* 3. 热力图 PNG—— 哪里差一眼看到,不用猜。
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
|
|
16
|
+
import { decodePng } from "./layouts/png.js";
|
|
17
|
+
import { renderSvg } from "./render.js";
|
|
18
|
+
|
|
19
|
+
export async function imageDiff({ base, compare, out, cols, rows, amplify, align }) {
|
|
20
|
+
|
|
21
|
+
const a = decodePng(readFileSync(base));
|
|
22
|
+
const b = decodePng(readFileSync(compare));
|
|
23
|
+
if (a.width !== b.width || a.height !== b.height) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`两张图尺寸不同:${a.width}×${a.height} vs ${b.width}×${b.height}。\n` +
|
|
26
|
+
"比对前先统一尺寸(可用 design_render action=render 的 width,或 composite 把图层放到同尺寸画布)。"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const W = a.width;
|
|
31
|
+
const H = a.height;
|
|
32
|
+
const NCOL = Number.isFinite(cols) ? Math.min(12, Math.max(1, Math.round(cols))) : 4;
|
|
33
|
+
const NROW = Number.isFinite(rows) ? Math.min(12, Math.max(1, Math.round(rows))) : 1;
|
|
34
|
+
const gain = Number.isFinite(amplify) ? Math.min(4, Math.max(1, amplify)) : 2.0;
|
|
35
|
+
|
|
36
|
+
const at = (img, x, y) => {
|
|
37
|
+
const i = (y * img.width + x) * img.channels;
|
|
38
|
+
return [img.data[i], img.data[i + 1], img.data[i + 2]];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const bS = b;
|
|
42
|
+
const SAMPLE = 2; // 每 2px 采样:2400×1400 的图也只要 ~84 万次,快且足够
|
|
43
|
+
let sum = 0;
|
|
44
|
+
let n = 0;
|
|
45
|
+
let max = 0;
|
|
46
|
+
let changed = 0;
|
|
47
|
+
const cells = Array.from({ length: NCOL * NROW }, () => ({ s: 0, c: 0 }));
|
|
48
|
+
const rects = [];
|
|
49
|
+
|
|
50
|
+
for (let y = 0; y < H; y += SAMPLE) {
|
|
51
|
+
for (let x = 0; x < W; x += SAMPLE) {
|
|
52
|
+
const p = at(a, x, y);
|
|
53
|
+
const q = at(b, x, y);
|
|
54
|
+
const d = (Math.abs(p[0] - q[0]) + Math.abs(p[1] - q[1]) + Math.abs(p[2] - q[2])) / 3;
|
|
55
|
+
sum += d;
|
|
56
|
+
n += 1;
|
|
57
|
+
if (d > max) max = d;
|
|
58
|
+
if (d > 24) changed += 1;
|
|
59
|
+
const ci = Math.min(NCOL - 1, Math.floor((x / W) * NCOL));
|
|
60
|
+
const ri = Math.min(NROW - 1, Math.floor((y / H) * NROW));
|
|
61
|
+
const cell = cells[ri * NCOL + ci];
|
|
62
|
+
cell.s += d;
|
|
63
|
+
cell.c += 1;
|
|
64
|
+
if (d > 24) {
|
|
65
|
+
const g = Math.min(255, Math.round(d * gain * 1.6));
|
|
66
|
+
rects.push(`<rect x="${x}" y="${y}" width="${SAMPLE}" height="${SAMPLE}" fill="rgb(${g},${Math.round(g * 0.24)},0)" opacity="0.85"/>`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 只保留一位小数:注意不要写成 round1(x)/10——除号会重新引入浮点噪声
|
|
72
|
+
// (实测输出过 13.719999999999999%,把百分比直接四舍五入才干净)
|
|
73
|
+
const round1 = (v) => Math.round(v * 10) / 10;
|
|
74
|
+
const pct = (v) => Math.round(v * 10) / 10;
|
|
75
|
+
const regions = cells.map((cell, i) => ({
|
|
76
|
+
row: Math.floor(i / NCOL),
|
|
77
|
+
col: i % NCOL,
|
|
78
|
+
meanDelta: cell.c === 0 ? 0 : round1(cell.s / cell.c),
|
|
79
|
+
share: cell.c === 0 ? 0 : pct(((cell.s / cell.c) / 255) * 100)
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
const result = {
|
|
83
|
+
width: W,
|
|
84
|
+
height: H,
|
|
85
|
+
meanDelta: round1(sum / n),
|
|
86
|
+
meanPct: pct(((sum / n) / 255) * 100),
|
|
87
|
+
maxDelta: round1(max),
|
|
88
|
+
changedPct: pct((changed / n) * 100),
|
|
89
|
+
cols: NCOL,
|
|
90
|
+
rows: NROW,
|
|
91
|
+
regions
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/* 偏移搜索:重影的元凶往往是“整体平移了几像素”,而不是“哪里排错了”。
|
|
95
|
+
* 实测:参照稿行距已经对齐到 48 vs 48,但热力图满是重影——文字每行出现两次。
|
|
96
|
+
* 这不是排错了,是整块内容比原图高/低了几像素。不把这个相位报出来,
|
|
97
|
+
* “差 13.6%”这个数字就只会让人到处乱改。 */
|
|
98
|
+
if (align === true) {
|
|
99
|
+
const STEP = 8;
|
|
100
|
+
const sampleAt = (img, x, y) => {
|
|
101
|
+
const i = (y * img.width + x) * img.channels;
|
|
102
|
+
return [img.data[i], img.data[i + 1], img.data[i + 2]];
|
|
103
|
+
};
|
|
104
|
+
const cost = (dx, dy) => {
|
|
105
|
+
let acc = 0;
|
|
106
|
+
let cnt = 0;
|
|
107
|
+
for (let y = 0; y < H; y += STEP) {
|
|
108
|
+
const sy = y + dy;
|
|
109
|
+
if (sy < 0 || sy >= H) continue;
|
|
110
|
+
for (let x = 0; x < W; x += STEP) {
|
|
111
|
+
const sx = x + dx;
|
|
112
|
+
if (sx < 0 || sx >= W) continue;
|
|
113
|
+
const p1 = sampleAt(a, x, y);
|
|
114
|
+
const p2 = sampleAt(bS, sx, sy);
|
|
115
|
+
acc += (Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]) + Math.abs(p1[2] - p2[2])) / 3;
|
|
116
|
+
cnt += 1;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return cnt === 0 ? Infinity : acc / cnt;
|
|
120
|
+
};
|
|
121
|
+
let best = { dx: 0, dy: 0, delta: cost(0, 0) };
|
|
122
|
+
for (let dy = -12; dy <= 12; dy += 2) {
|
|
123
|
+
for (let dx = -6; dx <= 6; dx += 2) {
|
|
124
|
+
if (dx === 0 && dy === 0) continue;
|
|
125
|
+
const d = cost(dx, dy);
|
|
126
|
+
if (d < best.delta) best = { dx, dy, delta: d };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
result.align = {
|
|
130
|
+
bestDx: best.dx,
|
|
131
|
+
bestDy: best.dy,
|
|
132
|
+
deltaAtBest: round1(best.delta),
|
|
133
|
+
deltaAtZero: round1(cost(0, 0)),
|
|
134
|
+
// 提升幅度:越大说明“错位”越是主因
|
|
135
|
+
gainPct: round1(((cost(0, 0) - best.delta) / Math.max(1, cost(0, 0))) * 100)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 热力图(保留原图轮廓作底,叠差异块)——只有给了 out 才写,没给就只返回数字
|
|
140
|
+
if (typeof out === "string" && out.trim() !== "") {
|
|
141
|
+
const b64 = readFileSync(base).toString("base64");
|
|
142
|
+
const svg =
|
|
143
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}">` +
|
|
144
|
+
`<image href="data:image/png;base64,${b64}" width="${W}" height="${H}" opacity="0.28"/>` +
|
|
145
|
+
`<rect width="${W}" height="${H}" fill="#05070A" opacity="0.55"/>` +
|
|
146
|
+
rects.join("") +
|
|
147
|
+
`</svg>`;
|
|
148
|
+
const written = await renderSvg({ svg, png: out, width: W });
|
|
149
|
+
result.heatmap = written.path;
|
|
150
|
+
result.heatmapBytes = written.bytes;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|