-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (82 loc) · 1.79 KB
/
index.js
File metadata and controls
101 lines (82 loc) · 1.79 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
100
101
var getPixels = require("get-pixels");
const rgbHex = require('rgb-hex');
var fs = require('fs');
var image = process.argv[2]; //Path to image, can be JPG/PNG
console.log("SRC Image: " + image);
var ignore_white = true;
if(typeof(process.argv[3]) !== "undefined"){
if (process.argv[3] == "full")
ignore_white = false;
}
var colors = { //Replace this color palette with the palette of your target canvas
0: '#ffffff',
1: '#e4e4e4',
2: '#888888',
3: '#222222',
4: '#ffa7d1',
5: '#e50000',
6: '#e59500',
7: '#a06a42',
8: '#e5d900',
9: '#94e044',
10: '#02be01',
11: '#00d3dd',
12: '#0083c7',
13: '#0000ea',
14: '#cf6ee4',
15: '#820080',
16: '#ffe9dc',
17: '#e0a899',
18: '#dfa290',
19: '#c99789',
20: '#636363',
21: '#3c3c3c',
22: '#acacac',
23: '#ce4a34',
24: '#a51c12',
25: '#622121',
26: '#ed905f',
27: '#f6c292',
28: '#f2a27e',
29: '#fbf2d4',
};
var nearestColor = require('nearest-color').from(colors);
getPixels(image, function(err, pixels) {
if(err) {
console.log("Bad image path")
return
}
processImage(pixels);
});
let initx = 0;
let inity = 0;
let nice = [];
function processImage(img){
let px = img.shape[0];
let py = img.shape[1];
console.log("Image is: " + x + "x" + y);
for (var x = 0; x < px; x++) {
for (var y = 0; y < py; y++) {
let pixel = [];
for (var k = 2; k >= 0; k--) {
pixel[k] = img.get(x,y,k);
}
let hex = rgbHex(pixel[0],pixel[1],pixel[2]);
let pcolor = nearestColor('#'+hex);
let fp = {
color: pcolor.name,
x: initx + x,
y: inity + y
};
console.log(fp);
if((pcolor.name !== "0" && ignore_white) || !ignore_white)
nice.push(fp);
}
}
fs.writeFile("output.txt", JSON.stringify(nice), function(err) {
if(err) {
return console.log(err);
}
console.log("Finsihed.");
});
}