Skip to content

Instantly share code, notes, and snippets.

@funmaker
Created January 21, 2020 14:10
Show Gist options
  • Save funmaker/e20016d08ecd9c1924708ba1bcb851ce to your computer and use it in GitHub Desktop.
Save funmaker/e20016d08ecd9c1924708ba1bcb851ce to your computer and use it in GitHub Desktop.
Denied
#!/usr/bin/env node
const { createCanvas, loadImage } = require('canvas')
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs')
const cp = require('child_process');
const denied = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAeCAQAAAA7+qCVAAABC0lEQVRYw+2X6w6CMBSD+/4vXRN/KNtpy4iwoVFjgpdwPkrbObA+MPFVBq9DgYLBDZ4TYXgnmL8yX6MM29FFtyEBT7z2zVH77smBSTiq0dqKwcx624NZrswbsfGM6uS6XECcCMMhUChQymxPVxeKeqSwdaPQKFOt3CijruAoDEto62D1SVGmVcTD0IZUj1czFJxVBuE2MdRXf5spPKMcFD2D6Bn1a23VmiZv5oE0Jc/4JNFCWpSrekYrg9i/Rpm91kFonuQZh4NeGR3Z2jrcdZhPU6o+mSYHw0GYFHTfYy9l8hLQD3HfQ2jQNzBiIYBEcIbv3FwEH//TuxqGR2HOStMv7Q7WbW5x2732AzuLL2wdDjdNAAAAAElFTkSuQmCC";
const approved = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAeCAQAAAA7+qCVAAABAklEQVRYw+2XUQrDMAxDdf9Le7CPrZ4lR4GSpLCNQUtL+qzK8oKoHyz8lQfvQwGDwQHfhTBxEsxfmccoE/nRRTdLwBtrvxzlszcHFuGwRMsRg5XxNoLZrswXMXmGZXIdFyALwW4ChgKmzHW5OijqEcOeU6ZaOSnDKrgLRq8rlMmKaJiQTeoqw+GkMmheUzTx5SjDHNR6Bq1n2N2uMtrMRjd1nsHkOAmROFifM2jzN82mGkQhrzkpFNTcDUr1TFhnoxRi0H30yW5yYGISps+bjzIsW2LwYpwUUspo2w+qZ5U4vTY/t/BbKaveGQFznrH+dvZz24fpu+lpu4N9m1scu9d+AVT1IXqfvOOtAAAAAElFTkSuQmCC";
const numbers = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAGCAQAAADnRD9qAAAAS0lEQVQY03WQCwoAIAxCvf+lX4Mi5j7FhpQzTQiicr9LH5EQhR/bh1ilHj0J/lMVbZkUNtJvzUW1R4mjoUYJ7KVue+R57i3U/OUPH5S7JemxnjCTAAAAAElFTkSuQmCC";
const positive = argv.positive || argv.p;
const date = (argv.date || argv.d) ? new Date(argv.date || argv.d) : new Date();
const scale = argv.scale || argv.s || 8;
(async () => {
const [ bg, text ] = await Promise.all([
loadImage(positive ? approved : denied),
loadImage(numbers),
])
const canvas = createCanvas(bg.width * scale, bg.height * scale);
const ctx = canvas.getContext('2d', { pixelFormat: 'A8' });
const color = positive ? [83, 112, 27] : [112, 27, 27];
const nh = 6;
const nw = 3;
let pos = 0;
const drawNum = n => ctx.drawImage(text, Math.floor(n) * nw, 0, nw, nh, (3 + (pos++) * 4) * scale, 3 * scale, nw * scale, nh * scale);
ctx.imageSmoothingEnabled = false;
ctx.drawImage(bg, 0, 0, canvas.width, canvas.height);
drawNum(date.getFullYear() / 1000 % 10);
drawNum(date.getFullYear() / 100 % 10);
drawNum(date.getFullYear() / 10 % 10);
drawNum(date.getFullYear() / 1 % 10);
drawNum(10);
drawNum((date.getMonth() + 1) / 10);
drawNum((date.getMonth() + 1) % 10);
drawNum(10);
drawNum(date.getDate() / 10);
drawNum(date.getDate() % 10);
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle = `#00000001`;
ctx.fillRect(0, 0, bg.width * scale, bg.height * scale);
const stream = canvas.createPNGStream({
compressionLevel: 9,
backgroundIndex: 0,
palette: new Uint8ClampedArray([
0, 0, 0, 0,
...color, 255,
]),
});
const xclip = cp.spawn("xclip", ["-selection", "clipboard", "-t", "image/png"], { stdio: ['pipe', 'inherit', 'inherit'] });
stream.pipe(xclip.stdin);
})().catch(err => {console.error(err); process.exit(-1);})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment