feat(UI): update logo

This commit is contained in:
Timi
2026-11-20 14:28:44 +08:00
parent 9ccdbe593a
commit 7c1dfc0e9f
47 changed files with 165 additions and 102 deletions
+55 -84
View File
@@ -1,95 +1,66 @@
/**
* 从 public/icons/icon-512.png 生成 Android 各密度的启动图标
*
* 传统图标用完整图,圆形图标裁圆,自适应前景留 10% 余量避免被系统裁切
*
* 运行:node scripts/generate-android-icons.mjs
*/
import sharp from "sharp";
const SOURCE = "public/icons/icon-512.png";
const RES = "android/app/src/main/res";
const source = "public/icons/brand.svg";
const res = "android/app/src/main/res";
const transparent = { r: 0, g: 0, b: 0, alpha: 0 };
const white = { r: 255, g: 255, b: 255, alpha: 1 };
/** 传统图标边长 */
const LEGACY = { mdpi: 48, hdpi: 72, xhdpi: 96, xxhdpi: 144, xxxhdpi: 192 };
/** 自适应前景边长,108dp 基准 */
const FOREGROUND = { mdpi: 108, hdpi: 162, xhdpi: 216, xxhdpi: 324, xxxhdpi: 432 };
/** 前景内容占画布的比例,低于安全区 66.7%,留余量 */
const FOREGROUND_RATIO = 0.9;
const { data } = await sharp(SOURCE).raw().toBuffer({ resolveWithObject: true });
const hex = `#${[data[0], data[1], data[2]].map(v => v.toString(16).padStart(2, "0")).join("")}`;
console.log(`图标底色:${hex}`);
for (const [density, size] of Object.entries(LEGACY)) {
const dir = `${RES}/mipmap-${density}`;
await sharp(SOURCE).resize(size, size, { fit: "cover" }).png().toFile(`${dir}/ic_launcher.png`);
const circle = Buffer.from(
`<svg width="${size}" height="${size}"><circle cx="${size / 2}" cy="${size / 2}" r="${size / 2}" fill="#fff"/></svg>`
);
await sharp(SOURCE)
.resize(size, size, { fit: "cover" })
.composite([{ input: circle, blend: "dest-in" }])
.png()
.toFile(`${dir}/ic_launcher_round.png`);
console.log(`${density}: 传统 ${size},圆形 ${size}`);
async function render(size, ratio, background = transparent) {
const inner = Math.round(size * ratio);
const icon = await sharp(source).resize(inner, inner).png().toBuffer();
return await sharp({ create: { width: size, height: size, channels: 4, background } })
.composite([{ input: icon, gravity: "centre" }]).png().toBuffer();
}
for (const [density, size] of Object.entries(FOREGROUND)) {
const inner = Math.round(size * FOREGROUND_RATIO);
const offset = Math.round((size - inner) / 2);
const scaled = await sharp(SOURCE).resize(inner, inner, { fit: "cover" }).png().toBuffer();
await sharp({
create: { width: size, height: size, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } }
})
.composite([{ input: scaled, top: offset, left: offset }])
.png()
.toFile(`${RES}/mipmap-${density}/ic_launcher_foreground.png`);
// 统一图形像素的颜色,通过透明度保留边缘抗锯齿。
const { data: pixels, info } = await sharp(await render(1024, 1)).raw().toBuffer({ resolveWithObject: true });
const colors = [[8, 127, 131], [157, 223, 201]];
for (let i = 0; i < pixels.length; i += 4) {
if (pixels[i + 3] === 0) continue;
const color = colors.reduce((closest, candidate) => {
const distance = candidate.reduce((sum, channel, index) => sum + (channel - pixels[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++) pixels[i + channel] = color[channel];
}
await sharp(pixels, { raw: info }).png().toFile("public/icons/brand-transparent.png");
await sharp(await render(1024, 1, white)).toFile("public/icons/brand-white.png");
for (const size of [192, 512]) {
await sharp(await render(size, 1, white)).toFile(`public/icons/icon-${size}.png`);
}
await sharp(await render(512, .8, white)).toFile("public/icons/icon-maskable-512.png");
console.log(`${density}: 前景 ${size}(内容 ${inner})`);
// 按图形实际边界居中;自适应图标的可见区域约为画布的三分之二。
const androidMark = await sharp(source).trim({ background: "#00000000", threshold: 1 }).png().toBuffer();
async function renderAndroid(size, ratio, background = transparent) {
const inner = Math.round(size * ratio);
const icon = await sharp(androidMark).resize(inner, inner, { fit: "contain" }).png().toBuffer();
return await sharp({ create: { width: size, height: size, channels: 4, background } })
.composite([{ input: icon, gravity: "centre" }]).png().toBuffer();
}
const legacy = { mdpi: 48, hdpi: 72, xhdpi: 96, xxhdpi: 144, xxxhdpi: 192 };
const foreground = { mdpi: 108, hdpi: 162, xhdpi: 216, xxhdpi: 324, xxxhdpi: 432 };
for (const [density, size] of Object.entries(legacy)) {
const dir = `${res}/mipmap-${density}`;
const icon = await renderAndroid(size, .66, white);
await sharp(icon).toFile(`${dir}/ic_launcher.png`);
const circle = Buffer.from(`<svg width="${size}" height="${size}"><circle cx="${size / 2}" cy="${size / 2}" r="${size / 2}" fill="white"/></svg>`);
await sharp(icon).composite([{ input: circle, blend: "dest-in" }]).png().toFile(`${dir}/ic_launcher_round.png`);
await sharp(await renderAndroid(foreground[density], .44)).toFile(`${dir}/ic_launcher_foreground.png`);
}
/**
* 启动图尺寸,背景铺图标底色,中间放 logo
*
* 键为 drawable 目录名,值为画布宽高
*/
const SPLASH = {
"drawable": [480, 320],
"drawable-land-mdpi": [480, 320],
"drawable-land-hdpi": [800, 480],
"drawable-land-xhdpi": [1280, 720],
"drawable-land-xxhdpi": [1600, 960],
"drawable-land-xxxhdpi": [1920, 1280],
"drawable-port-mdpi": [320, 480],
"drawable-port-hdpi": [480, 800],
"drawable-port-xhdpi": [720, 1280],
"drawable-port-xxhdpi": [960, 1600],
"drawable-port-xxxhdpi": [1280, 1920]
const splash = {
"drawable": [480, 320], "drawable-land-mdpi": [480, 320], "drawable-land-hdpi": [800, 480],
"drawable-land-xhdpi": [1280, 720], "drawable-land-xxhdpi": [1600, 960], "drawable-land-xxxhdpi": [1920, 1280],
"drawable-port-mdpi": [320, 480], "drawable-port-hdpi": [480, 800], "drawable-port-xhdpi": [720, 1280],
"drawable-port-xxhdpi": [960, 1600], "drawable-port-xxxhdpi": [1280, 1920]
};
/** logo 占短边的比例 */
const SPLASH_LOGO_RATIO = 0.28;
for (const [dir, [width, height]] of Object.entries(SPLASH)) {
const logo = Math.round(Math.min(width, height) * SPLASH_LOGO_RATIO);
const scaled = await sharp(SOURCE).resize(logo, logo, { fit: "cover" }).png().toBuffer();
await sharp({
create: { width, height, channels: 3, background: { r: data[0], g: data[1], b: data[2] } }
})
.composite([{
input: scaled,
top: Math.round((height - logo) / 2),
left: Math.round((width - logo) / 2)
}])
.png()
.toFile(`${RES}/${dir}/splash.png`);
console.log(`${dir}: ${width}x${height},logo ${logo}`);
for (const [dir, [width, height]] of Object.entries(splash)) {
const size = Math.round(Math.min(width, height) * .28);
const icon = await render(size, 1);
await sharp({ create: { width, height, channels: 4, background: white } })
.composite([{ input: icon, gravity: "centre" }]).png().toFile(`${res}/${dir}/splash.png`);
}
console.log("完成");