-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlospec-cli.js
More file actions
executable file
·99 lines (78 loc) · 2.49 KB
/
lospec-cli.js
File metadata and controls
executable file
·99 lines (78 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
import fs from "fs";
import readline from "readline";
import { PNG } from "pngjs";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask(question) {
return new Promise(resolve => rl.question(question, answer => resolve(answer.trim())));
}
function hexToRgb(hex) {
return {
r: parseInt(hex.slice(1, 3), 16),
g: parseInt(hex.slice(3, 5), 16),
b: parseInt(hex.slice(5, 7), 16)
};
}
async function main() {
const paletteName = await ask("Palette name (eg: endesga-64): ");
if (!paletteName) {
console.error("Palette name is required.");
rl.close();
process.exit(1);
}
const columnsInput = await ask("Columns [8]: ");
const cellSizeInput = await ask("Cell size [32]: ");
rl.close();
const perRow = Number(columnsInput) || 8;
const cellSize = Number(cellSizeInput) || 32;
const url = `https://lospec.com/palette-list/${paletteName}`;
const response = await fetch(url);
if (!response.ok) {
console.error(`Failed to fetch palette: ${response.status} ${response.statusText}`);
process.exit(1);
}
const html = await response.text();
const paletteMatch = html.match(/<div class="palette">([\s\S]*?)<\/div>\s*<\/div>/i);
if (!paletteMatch) {
console.error("Could not find palette block.");
process.exit(1);
}
const paletteHtml = paletteMatch[1];
const colors = [
...paletteHtml.matchAll(/<div class="color"[^>]*style="[^"]*background:\s*(#[0-9a-fA-F]{6})/gi)
].map(match => match[1].toLowerCase());
if (!colors.length) {
console.error("No colors found in palette block.");
process.exit(1);
}
const rows = Math.ceil(colors.length / perRow);
const png = new PNG({
width: perRow * cellSize,
height: rows * cellSize
});
for (let i = 0; i < colors.length; i++) {
const { r, g, b } = hexToRgb(colors[i]);
const startX = (i % perRow) * cellSize;
const startY = Math.floor(i / perRow) * cellSize;
for (let y = startY; y < startY + cellSize; y++) {
for (let x = startX; x < startX + cellSize; x++) {
const idx = (png.width * y + x) * 4; // PNG buffer has 4 channels.
png.data[idx] = r;
png.data[idx + 1] = g;
png.data[idx + 2] = b;
png.data[idx + 3] = 255;
}
}
}
const outputFile = `${paletteName}.png`;
fs.writeFileSync(outputFile, PNG.sync.write(png));
console.log(`Palette saved to ${outputFile}`);
}
main().catch(err => {
rl.close();
console.error(err);
process.exit(1);
});