Update 'palette-gen.js'

This commit is contained in:
sora 2023-06-17 02:42:25 +02:00 committed by soraefir
parent 1563bb0815
commit a1676fc2a0
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
25 changed files with 49 additions and 16 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
palette.in
palette.out
gifpalette.out
build/

23
Makefile Normal file
View File

@ -0,0 +1,23 @@
SRCDIR:= img
DSTDIR:= build
SOURCES := $(shell find ${SRCDIR} -type f -print)
FILES := $(patsubst $(SRCDIR)/%,$(DSTDIR)/%,$(SOURCES))
all: $(FILES)
palette.out: palette.in
node palette-gen.js
builddir:
@mkdir -p build
@mkdir -p build/gif
build/%: img/% builddir palette.out
repalette $< $@ -p $$(cat palette.out) --dither $(DITHER)
build/%.gif: img/%.gif builddir palette.out
gifsicle --use-colormap gifpalette.out < $< > $@
clean:
@rm -rf palette.in palette.out build

View File

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

View File

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

View File

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

View File

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View File

Before

Width:  |  Height:  |  Size: 2.5 MiB

After

Width:  |  Height:  |  Size: 2.5 MiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

View File

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 959 KiB

After

Width:  |  Height:  |  Size: 959 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

View File

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

View File

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View File

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

Before

Width:  |  Height:  |  Size: 2.9 MiB

After

Width:  |  Height:  |  Size: 2.9 MiB

View File

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

BIN
img/gif/pond.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

View File

@ -1,7 +1,7 @@
#!/bin/nodejs #!/bin/nodejs
const componentToHex = (c) => ("0" + c.toString(16)).slice(-2); const componentToHex = (c) => ("0" + c.toString(16)).slice(-2);
const rgbToHex = (c) => "#" + componentToHex(c.r) + componentToHex(c.g) + componentToHex(c.b); const rgbToHex = (c) => componentToHex(c.r) + componentToHex(c.g) + componentToHex(c.b);
const hexToRgb = (hex) => { const hexToRgb = (hex) => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? { return result ? {
@ -9,7 +9,7 @@ const hexToRgb = (hex) => {
} : null; } : null;
}; };
const parseScheme = (str) => str.split(",").map(e=>hexToRgb(e)) const parseScheme = (str) => str.replace(/\s+/g, '').split(",").map(e=>hexToRgb(e))
const getTint = (c, w) => Math.round(c + (255 - c) * 1 * w); const getTint = (c, w) => Math.round(c + (255 - c) * 1 * w);
const getShade = (c, w) => Math.round(c * w); const getShade = (c, w) => Math.round(c * w);
@ -17,19 +17,25 @@ 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 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 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 tints = (c, w) => Array.from({ length: w }, (_, i) => tint(c, (i + 1)/w));
const shades = (c, w) => Array.from({ length: 100 / w }, (_, i) => shade(c, (i + 1) * w/100)); const shades = (c, w) => Array.from({ length: w }, (_, i) => shade(c, (i )/w));
const genPalette = (c, w=10) => [...tints(c, w).reverse(), Object.assign(c), ...shades(c, w).reverse()]; const genPalette = (c, w=3) => [...tints(c, w).reverse(), Object.assign(c), ...shades(c, w).reverse()];
const readline = require('readline'); const fs = require("fs");
const rl = readline.createInterface({
input: process.stdin, fs.readFile("palette.in", "utf-8", (_, buf) => {
output: process.stdout, const colors = parseScheme(buf)
terminal: false .map(e=> genPalette(e, process.env.PALETTE_SIZE).filter(v=>(v.r+v.g+v.b>0)&&(v.r+v.g+v.b<765)))
const hexstr = colors.map(cs=>cs.map(c=>rgbToHex(c)).join(',')).join(',')
const gifstr = colors.map(cs=>cs.map(c=>`${c.r} ${c.g} ${c.b}`).join('\n')).join('\n')
fs.writeFile("palette.out", hexstr, (err) => {
if (err) console.log(err);
});
fs.writeFile("gifpalette.out", gifstr, (err) => {
if (err) console.log(err);
}); });
rl.on('line', (line) => {
console.log(parseScheme(line).map(e=>genPalette(e).map(c=>rgbToHex(c))));
}); });