import sharp from "sharp"; 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 }; 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(); } // 统一图形像素的颜色,通过透明度保留边缘抗锯齿。 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"); // 按图形实际边界居中;自适应图标的可见区域约为画布的三分之二。 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(``); 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`); } 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] }; 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`); }