Files
work-leave-service/png-map.mjs
T
wanghu 25147ec74e
build / build-push (push) Successful in 50s
commit
2026-10-10 15:42:52 +08:00

35 lines
1.5 KiB
JavaScript

import fs from "node:fs";
const W = Number(process.argv[3] || 110), H = Number(process.argv[4] || 46);
const meta = JSON.parse(fs.readFileSync(process.argv[2] + ".json", "utf8"));
const px = fs.readFileSync(process.argv[2]);
const { w, h, ch } = meta;
const at = (x, y) => { const o = (y * w + x) * ch; return [px[o], px[o+1], px[o+2]]; };
const D = (a,b) => Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1])+Math.abs(a[2]-b[2]);
const CLASSES = [
[" ", [255,255,255]], ["b", [235,247,255]], ["B", [87,122,251]],
["-", [227,234,246]], ["=", [236,237,243]], ["#", [81,90,110]],
["R", [250,81,81]], ["g", [69,185,114]], ["o", [232,165,76]],
["f", [246,248,249]], [".", [239,244,255]],
];
const cls = (c) => { let best = ".", bd = 1e9;
for (const [ch2, t] of CLASSES) { const d = D(c, t); if (d < bd) { bd = d; best = ch2; } }
return bd < 40 ? best : "?"; };
let out = "";
for (let gy = 0; gy < H; gy++) {
let line = "";
for (let gx = 0; gx < W; gx++) {
const x0 = Math.floor(gx * w / W), x1 = Math.max(x0+1, Math.floor((gx+1) * w / W));
const y0 = Math.floor(gy * h / H), y1 = Math.max(y0+1, Math.floor((gy+1) * h / H));
const cnt = new Map();
for (let y = y0; y < y1; y += 2) for (let x = x0; x < x1; x += 2) {
const k = cls(at(x, y)); cnt.set(k, (cnt.get(k)||0)+1);
}
let best = " ", bc = -1;
for (const [k, v] of cnt) if (v > bc) { bc = v; best = k; }
line += best;
}
out += String(gy).padStart(2) + "|" + line + "\n";
}
console.log(`图 ${w}x${h} → ${W}x${H} 字符`);
console.log(out);