/
test
executable file
·51 lines (43 loc) · 1.02 KB
/
test
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
#!/usr/bin/env node
var palette = require('./')
, fs = require('fs')
, Canvas = require('canvas')
, Image = Canvas.Image
, canvas = new Canvas
, ctx = canvas.getContext('2d')
, path = process.argv[2]
, out = '/tmp/out.png';
if (!path) {
console.error('Usage: test <image>');
process.exit(1);
}
var img = new Image;
img.onload = function(){
canvas.width = img.width;
canvas.height = img.height + 50;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
paintPalette();
save();
};
img.src = path;
function paintPalette() {
var x = 0;
var colors = palette(canvas);
colors.forEach(function(color){
var r = color[0]
, g = color[1]
, b = color[2]
, val = r << 16 | g << 8 | b
, str = '#' + val.toString(16);
ctx.fillStyle = str;
ctx.fillRect(x += 31, canvas.height - 40, 30, 30);
});
}
function save() {
fs.writeFile(out, canvas.toBuffer(), function(err){
if (err) throw err;
console.log('saved %s', out);
});
}