-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mjs
61 lines (52 loc) · 1.8 KB
/
test.mjs
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
import Sprite from './sprite.mjs'
import fs from 'fs';
const seed = null;
const monochrome = false;
const result = Sprite.generate(seed, monochrome);
// Output as data: URL
const url = result.asDataUri(false);
console.log(url);
// Output to bmp file
const filename = 'output.bmp';
const bmpData = result.magnify().asBitmapData(false);
fs.writeFileSync(filename, Buffer.from(bmpData), 'binary');
console.log(`Written: ${filename}`);
// Output to terminal (uses RGB ANSI sequences)
const colorData = result.createColorData(result.colors);
console.log();
outputImageTerminal(colorData, result.width, result.height);
console.log();
outputImageTerminalSmall(colorData, result.width, result.height);
console.log();
function outputImageTerminal(color, width, height) {
const t = '██'; // '██'; // '██', '▓▓', '▒▒', '░░'
for (let y = 0; y < result.height; y++) {
const lineParts = [];
for (let x = 0; x < result.width; x++) {
const c = colorData[y * result.width + x];
lineParts.push(`\x1B[38;2;${c[0]};${c[1]};${c[2]}m${t}`);
}
lineParts.push('\x1B[0m');
console.log(lineParts.join(''));
}
}
function outputImageTerminalSmall(color, width, height) {
for (let y = 0; y < result.height; y += 2) {
const lineParts = [];
for (let x = 0; x < result.width; x++) {
let c;
// Background color
if (y + 1 < result.height) {
c = colorData[(y + 1) * result.width + x];
lineParts.push(`\x1B[48;2;${c[0]};${c[1]};${c[2]}m`);
} else {
lineParts.push('\x1B[0m'); // reset (for background)
}
// Upper/foreground color and character
c = colorData[y * result.width + x];
lineParts.push(`\x1B[38;2;${c[0]};${c[1]};${c[2]}m▀`);
}
lineParts.push('\x1B[0m');
console.log(lineParts.join(''));
}
}