-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.js
44 lines (41 loc) · 1.08 KB
/
transfer.js
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
var fs = require('fs');
var diff = require('color-diff');
var PNGReader = require('png.js');
var colors = require('./colors.json');
var colorNum = colors.length;
fs.readFile('test.png', function(err, buffer){
var reader = new PNGReader(buffer);
reader.parse(function(err, png){
if (err) throw err;
var width = png.getWidth();
var height = png.getHeight();
var pic = [];
for (var y = 0; y < height; y++) {
var line = [];
for (var x = 0; x < width; x++) {
var pixel = png.getPixel(x, y);
if (pixel[3] !== 0) {
var color = diff.closest({
R: pixel[0],
G: pixel[1],
B: pixel[2]
}, colors)
for (var index in colors) {
if(colors[index].R === color.R && colors[index].G === color.G && colors[index].B === color.B) {
line.push(index);
break;
}
}
} else {
line.push(-1);
}
}
pic.push(line);
}
console.log(JSON.stringify({
x: 100,
y: 100,
image: pic
}));
});
});