Jotley / app · jotley.ai / scripts / apply-brand-palette.mjs
| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | |
| 3 | const [input, output, mode, ...extra] = process.argv.slice(2); |
| 4 | if (!input || !output || input === output || (mode && mode !== '--solid') || extra.length) { |
| 5 | throw new Error('Usage: node scripts/apply-brand-palette.mjs INPUT.png OUTPUT.png [--solid]'); |
| 6 | } |
| 7 | |
| 8 | // The app icon uses the light brand purple. Website artwork can use the download button gradient. |
| 9 | const bottom = [0x7c, 0x3a, 0xed]; |
| 10 | const top = mode === '--solid' ? bottom : [0x8b, 0x5c, 0xf6]; |
| 11 | |
| 12 | const [width, height] = execFileSync('magick', ['identify', '-format', '%w %h', input]) |
| 13 | .toString().trim().split(/\s+/).map(Number); |
| 14 | const pixels = execFileSync('magick', [input, '-alpha', 'on', '-depth', '8', 'rgba:-'], |
| 15 | { maxBuffer: width * height * 4 + 1024 }); |
| 16 | |
| 17 | // Find the original disc color and its vertical bounds. The white ring, feather, |
| 18 | // background tile and alpha are reused from each native variant. |
| 19 | const histogram = new Map(); |
| 20 | let first = height; |
| 21 | let last = 0; |
| 22 | for (let i = 0; i < pixels.length; i += 4) { |
| 23 | const [red, green, blue, alpha] = pixels.subarray(i, i + 4); |
| 24 | if (alpha < 240 || red - green < 30 || blue - green < 90 || blue <= red) continue; |
| 25 | const key = (red << 16) | (green << 8) | blue; |
| 26 | histogram.set(key, (histogram.get(key) ?? 0) + 1); |
| 27 | const row = Math.floor(i / (width * 4)); |
| 28 | first = Math.min(first, row); |
| 29 | last = Math.max(last, row); |
| 30 | } |
| 31 | if (histogram.size === 0 || first === last) throw new Error(`No violet disc found in ${input}`); |
| 32 | const [key] = [...histogram].sort((a, b) => b[1] - a[1])[0]; |
| 33 | const source = [key >> 16, (key >> 8) & 255, key & 255]; |
| 34 | const sourceChroma = source[2] - source[1]; |
| 35 | const sourceHue = (source[0] - source[1]) / sourceChroma; |
| 36 | for (let row = 0; row < height; row++) { |
| 37 | const progress = Math.max(0, Math.min(1, (row - first) / (last - first))); |
| 38 | const delta = top.map((start, index) => start + progress * (bottom[index] - start) - source[index]); |
| 39 | for (let x = 0; x < width; x++) { |
| 40 | const i = (row * width + x) * 4; |
| 41 | const chroma = pixels[i + 2] - pixels[i + 1]; |
| 42 | if (pixels[i + 3] === 0 || chroma <= 0) continue; |
| 43 | const hue = (pixels[i] - pixels[i + 1]) / chroma; |
| 44 | if (Math.abs(hue - sourceHue) > 0.14) continue; |
| 45 | const weight = Math.min(1, chroma / sourceChroma); |
| 46 | for (let channel = 0; channel < 3; channel++) { |
| 47 | pixels[i + channel] = Math.max(0, Math.min(255, Math.round(pixels[i + channel] + weight * delta[channel]))); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | execFileSync('magick', ['-size', `${width}x${height}`, '-depth', '8', 'rgba:-', '-strip', output], { input: pixels }); |