Files
gao-customer-h5/scripts/generate-dark-brand.mjs
2026-11-20 14:28:44 +08:00

22 lines
1.2 KiB
JavaScript

import { readFile, writeFile } from "node:fs/promises";
import sharp from "sharp";
const light = await readFile("public/icons/brand.svg", "utf8");
const dark = light.replaceAll("#087f83", "#4dc9be").replaceAll("#9ddfc9", "#d0f5e7");
await writeFile("public/icons/brand-dark.svg", dark, "utf8");
const { data, info } = await sharp(Buffer.from(dark)).resize(1024, 1024).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const colors = [[77, 201, 190], [208, 245, 231]];
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] === 0) continue;
const color = colors.reduce((closest, candidate) => {
const distance = candidate.reduce((sum, channel, index) => sum + (channel - data[i + index]) ** 2, 0);
return distance < closest.distance ? { value: candidate, distance } : closest;
}, { value: colors[0], distance: Infinity }).value;
for (let channel = 0; channel < 3; channel++) data[i + channel] = color[channel];
}
const icon = await sharp(data, { raw: info }).png().toBuffer();
await sharp(icon).toFile("public/icons/brand-dark-transparent.png");
await sharp({ create: { width: 1024, height: 1024, channels: 4, background: "#10161f" } })
.composite([{ input: icon }]).png().toFile("public/icons/brand-dark-background.png");