|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import sharp from "sharp"; |
| 5 | + |
| 6 | +const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 7 | +const sourcePath = path.join( |
| 8 | + projectRoot, |
| 9 | + "assets-src/hero/compbio-research-atlas-light-master.png", |
| 10 | +); |
| 11 | +const outputDirectory = path.join(projectRoot, "public/hero"); |
| 12 | +const outputWidths = [800, 1600]; |
| 13 | + |
| 14 | +const isGoldPixel = (red, green, blue) => |
| 15 | + red > 125 |
| 16 | + && red > blue * 1.18 |
| 17 | + && green > blue * 1.08 |
| 18 | + && red > green * 1.015; |
| 19 | + |
| 20 | +const clearSafeMargin = async (input) => { |
| 21 | + const { data, info } = await sharp(input) |
| 22 | + .ensureAlpha() |
| 23 | + .raw() |
| 24 | + .toBuffer({ resolveWithObject: true }); |
| 25 | + |
| 26 | + for (let y = 0; y < info.height; y += 1) { |
| 27 | + for (let x = 0; x < info.width; x += 1) { |
| 28 | + if (x >= 100 && x < info.width - 100 && y >= 100 && y < info.height - 120) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + const index = (y * info.width + x) * 4; |
| 33 | + data[index] = 0; |
| 34 | + data[index + 1] = 0; |
| 35 | + data[index + 2] = 0; |
| 36 | + data[index + 3] = 0; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return sharp(data, { |
| 41 | + raw: { |
| 42 | + width: info.width, |
| 43 | + height: info.height, |
| 44 | + channels: 4, |
| 45 | + }, |
| 46 | + }) |
| 47 | + .png() |
| 48 | + .toBuffer(); |
| 49 | +}; |
| 50 | + |
| 51 | +const buildDarkVariant = async (input) => { |
| 52 | + const { data, info } = await sharp(input) |
| 53 | + .ensureAlpha() |
| 54 | + .raw() |
| 55 | + .toBuffer({ resolveWithObject: true }); |
| 56 | + |
| 57 | + for (let index = 0; index < data.length; index += 4) { |
| 58 | + const red = data[index]; |
| 59 | + const green = data[index + 1]; |
| 60 | + const blue = data[index + 2]; |
| 61 | + const alpha = data[index + 3]; |
| 62 | + |
| 63 | + if (alpha === 0) continue; |
| 64 | + |
| 65 | + if (isGoldPixel(red, green, blue)) { |
| 66 | + data[index] = Math.min(255, Math.round(red * 1.02)); |
| 67 | + data[index + 1] = Math.min(255, Math.round(green * 1.03)); |
| 68 | + data[index + 2] = Math.min(255, Math.round(blue * 0.96)); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + const luminance = Math.max( |
| 73 | + 0, |
| 74 | + Math.min(1, (0.2126 * red + 0.7152 * green + 0.0722 * blue) / 255), |
| 75 | + ); |
| 76 | + const pearl = [247, 243, 232]; |
| 77 | + const iceBlue = [139, 164, 194]; |
| 78 | + |
| 79 | + data[index] = Math.round(pearl[0] * (1 - luminance) + iceBlue[0] * luminance); |
| 80 | + data[index + 1] = Math.round( |
| 81 | + pearl[1] * (1 - luminance) + iceBlue[1] * luminance, |
| 82 | + ); |
| 83 | + data[index + 2] = Math.round( |
| 84 | + pearl[2] * (1 - luminance) + iceBlue[2] * luminance, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return sharp(data, { |
| 89 | + raw: { |
| 90 | + width: info.width, |
| 91 | + height: info.height, |
| 92 | + channels: 4, |
| 93 | + }, |
| 94 | + }) |
| 95 | + .png() |
| 96 | + .toBuffer(); |
| 97 | +}; |
| 98 | + |
| 99 | +const writeVariant = async (variant, input, width) => { |
| 100 | + const height = Math.round(width * 0.75); |
| 101 | + const outputPath = path.join( |
| 102 | + outputDirectory, |
| 103 | + `compbio-research-atlas-${variant}-${width}.webp`, |
| 104 | + ); |
| 105 | + |
| 106 | + await sharp(input) |
| 107 | + .resize(width, height, { |
| 108 | + fit: "fill", |
| 109 | + kernel: sharp.kernel.lanczos3, |
| 110 | + withoutEnlargement: true, |
| 111 | + }) |
| 112 | + .webp({ |
| 113 | + quality: 90, |
| 114 | + alphaQuality: 100, |
| 115 | + effort: 6, |
| 116 | + smartSubsample: true, |
| 117 | + }) |
| 118 | + .toFile(outputPath); |
| 119 | + |
| 120 | + console.log(`Wrote ${path.relative(projectRoot, outputPath)}`); |
| 121 | +}; |
| 122 | + |
| 123 | +const sourceMaster = await sharp(sourcePath).png().toBuffer(); |
| 124 | +const metadata = await sharp(sourceMaster).metadata(); |
| 125 | + |
| 126 | +if ( |
| 127 | + metadata.width !== 1600 |
| 128 | + || metadata.height !== 1200 |
| 129 | + || metadata.hasAlpha !== true |
| 130 | +) { |
| 131 | + throw new Error( |
| 132 | + "Hero master must be a 1600x1200 PNG with a transparent alpha channel.", |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +await mkdir(outputDirectory, { recursive: true }); |
| 137 | + |
| 138 | +const source = await clearSafeMargin(sourceMaster); |
| 139 | +const variants = { |
| 140 | + light: source, |
| 141 | + dark: await buildDarkVariant(source), |
| 142 | +}; |
| 143 | + |
| 144 | +for (const [variant, input] of Object.entries(variants)) { |
| 145 | + for (const width of outputWidths) { |
| 146 | + await writeVariant(variant, input, width); |
| 147 | + } |
| 148 | +} |
0 commit comments