From 1563bb081516b45467e166003515eb8ed1103aa9 Mon Sep 17 00:00:00 2001 From: sora Date: Sat, 17 Jun 2023 02:36:34 +0200 Subject: [PATCH] Added palette generator --- palette-gen.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 palette-gen.js diff --git a/palette-gen.js b/palette-gen.js new file mode 100644 index 0000000..6524d41 --- /dev/null +++ b/palette-gen.js @@ -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)))); +});