Files
NetMesh/electron/bridges/windowManager/backgroundColor.cjs
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

117 lines
3.4 KiB
JavaScript

const path = require("node:path");
const fs = require("node:fs");
const frontendBackgroundColorCache = new Map();
function hslToHex(h, s, l) {
const hue = ((h % 360) + 360) % 360;
const sat = Math.max(0, Math.min(100, s)) / 100;
const light = Math.max(0, Math.min(100, l)) / 100;
const c = (1 - Math.abs(2 * light - 1)) * sat;
const x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
const m = light - c / 2;
let r1 = 0;
let g1 = 0;
let b1 = 0;
if (hue < 60) {
r1 = c; g1 = x; b1 = 0;
} else if (hue < 120) {
r1 = x; g1 = c; b1 = 0;
} else if (hue < 180) {
r1 = 0; g1 = c; b1 = x;
} else if (hue < 240) {
r1 = 0; g1 = x; b1 = c;
} else if (hue < 300) {
r1 = x; g1 = 0; b1 = c;
} else {
r1 = c; g1 = 0; b1 = x;
}
const toHex = (n) => Math.round((n + m) * 255).toString(16).padStart(2, "0");
return `#${toHex(r1)}${toHex(g1)}${toHex(b1)}`;
}
function normalizeBackgroundColor(value) {
if (!value) return null;
const raw = String(value).trim();
if (!raw) return null;
if (raw.startsWith("#")) return raw;
const parts = raw.split(/\s+/).filter(Boolean);
if (parts.length < 3) return null;
const h = Number(parts[0]);
const s = Number(String(parts[1]).replace("%", ""));
const l = Number(String(parts[2]).replace("%", ""));
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) return null;
return hslToHex(h, s, l);
}
function parseBackgroundFromIndexHtml(indexHtml, theme) {
if (!indexHtml) return null;
const block =
theme === "dark"
? indexHtml.match(/\.dark\s*\{[\s\S]*?\}/)
: indexHtml.match(/:root\s*\{[\s\S]*?\}/);
const within = block?.[0] || indexHtml;
const m = within.match(/--background:\s*([^;]+);/);
const raw = m?.[1]?.trim();
if (!raw) return null;
const parts = raw.split(/\s+/).filter(Boolean);
if (parts.length < 3) return null;
const h = Number(parts[0]);
const s = Number(String(parts[1]).replace("%", ""));
const l = Number(String(parts[2]).replace("%", ""));
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) return null;
return hslToHex(h, s, l);
}
function resolveIndexHtmlPath(electronDir) {
const dist = path.join(electronDir, "../dist/index.html");
const root = path.join(electronDir, "../index.html");
if (fs.existsSync(dist)) return dist;
if (fs.existsSync(root)) return root;
return dist;
}
function trimFrontendBackgroundColorCache() {
if (frontendBackgroundColorCache.size <= 8) return;
const firstKey = frontendBackgroundColorCache.keys().next().value;
if (firstKey) frontendBackgroundColorCache.delete(firstKey);
}
function resolveFrontendBackgroundColor(electronDir, theme) {
try {
const htmlPath = resolveIndexHtmlPath(electronDir);
if (!htmlPath || !fs.existsSync(htmlPath)) return null;
const stat = fs.statSync(htmlPath);
const cacheKey = `${htmlPath}:${stat.mtimeMs}:${theme || "default"}`;
if (frontendBackgroundColorCache.has(cacheKey)) {
return frontendBackgroundColorCache.get(cacheKey);
}
const indexHtml = fs.readFileSync(htmlPath, "utf8");
const color = parseBackgroundFromIndexHtml(indexHtml, theme);
frontendBackgroundColorCache.set(cacheKey, color);
trimFrontendBackgroundColorCache();
return color;
} catch {
return null;
}
}
module.exports = {
hslToHex,
normalizeBackgroundColor,
parseBackgroundFromIndexHtml,
resolveIndexHtmlPath,
resolveFrontendBackgroundColor,
};