Added palette generator

This commit is contained in:
sora 2023-06-17 02:36:34 +02:00
parent f0f056038b
commit 1563bb0815

35
palette-gen.js Normal file
View File

@ -0,0 +1,35 @@
#!/bin/nodejs
const componentToHex = (c) => ("0" + c.toString(16)).slice(-2);
const rgbToHex = (c) => "#" + componentToHex(c.r) + componentToHex(c.g) + componentToHex(c.b);
const hexToRgb = (hex) => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16)
} : null;
};
const parseScheme = (str) => str.split(",").map(e=>hexToRgb(e))
const getTint = (c, w) => Math.round(c + (255 - c) * 1 * w);
const getShade = (c, w) => Math.round(c * w);
const tint = (c, w) => ({r:getTint(c.r,w), g:getTint(c.g,w), b:getTint(c.b,w)});
const shade = (c, w) => ({r:getShade(c.r,w), g:getShade(c.g,w), b:getShade(c.b,w)});
const tints = (c, w) => Array.from({ length: 100 / w }, (_, i) => tint(c, (i + 1) * w/100));
const shades = (c, w) => Array.from({ length: 100 / w }, (_, i) => shade(c, (i + 1) * w/100));
const genPalette = (c, w=10) => [...tints(c, w).reverse(), Object.assign(c), ...shades(c, w).reverse()];
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
console.log(parseScheme(line).map(e=>genPalette(e).map(c=>rgbToHex(c))));
});