-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd.js
executable file
·60 lines (47 loc) · 1.85 KB
/
cmd.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
/*jshint strict:false */
var getPixels = require('get-pixels');
var fs = require('fs');
var rgb2hex = require('rgb2hex');
var image = process.argv[2];
var output = process.argv[3];
if (!output) output = __dirname + '/output.svg';
var svgStream = fs.createWriteStream(output);
var ProgressBar = require('progress');
getPixels(image, function(err, pixels) {
if (err) return console.log('Bad pixels path');
console.log('processing ' + image + ' -> SVG');
var bar = new ProgressBar(':percent', { total: (pixels.data.buffer.byteLength / 4) });
var shape = pixels.shape.slice();
var width = shape[0];
var height = shape[1];
svgStream.write('<?xml version="1.0" encoding="utf-8" ?>\n'+
'<svg baseProfile="full" ' +
'version="1.1" ' +
'height="'+height+'px" ' +
'width="'+width+'px" ' +
'xmlns="http://www.w3.org/2000/svg" ' +
'xmlns:ev="http://www.w3.org/2001/xml-events" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink">\n');
var c = 0;
for (var i = 0; i < height; i += 1) {
for (var j = 0; j < width; j += 1) {
var r = pixels.data[c];
var g = pixels.data[c+1];
var b = pixels.data[c+2];
var a = pixels.data[c+3];
// If colour data is found, draw a rect.
if (r !== 0 && g !== 0 && b !== 0) {
var rgbStr = 'rgb('+r+','+g+','+b+')';
var hexObj = rgb2hex(rgbStr);
var rectStr = '<rect fill="'+ hexObj.hex +'" height="1px" width="1px" x="' + j + '" y="'+ i +'" />\n';
svgStream.write(rectStr);
}
bar.tick();
c += 4;
}
}
svgStream.end('</svg>', function () {
console.log('done!');
});
});