-
Notifications
You must be signed in to change notification settings - Fork 1
/
btn-render.js
114 lines (96 loc) · 2.36 KB
/
btn-render.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
const sharp = require('sharp');
const paths = require('material-design-icons-svg/paths');
const icons = require('material-design-icons-svg')(paths);
var parseColor = require('parse-color');
function Color (c) {
var p = parseColor(c).rgb;
if (!p) p = parseColor("white").rgb;
return {
r: p[0],
g: p[1],
b: p[2]
};
}
var imgDesc = function (obj) {
return {
width: obj.info.width,
height: obj.info.height,
channels: obj.info.channels
};
}
var getIcon = function(id, large) {
var svg = icons.getSVG(id);
if (!svg) svg = icons.getSVG("help");
var b = Buffer(svg);
var d = (large) ? 200 : 150;
return sharp(b, {density: d})
.raw()
.toBuffer({resolveWithObject: true});
}
var getText = function(text) {
var svg = Buffer('<svg width="72" height="72"><text x="35" y="64" font-family="sans-serif" font-size="13" fill="white" text-anchor="middle">' + text + '</text></svg>');
return sharp(svg, {})
.raw()
.toBuffer({resolveWithObject: true});
}
var getComposite = function(bg, fg, cutout) {
return sharp(bg.data, { raw: imgDesc(bg) })
.overlayWith(fg.data, {
gravity: "north",
cutout: cutout,
raw: imgDesc(fg)
})
.raw()
.toBuffer({resolveWithObject: true});
}
var getBackground = function(color) {
return sharp({
create: {
width: 72,
height: 72,
channels: 3,
background: color
}
})
.raw()
.toBuffer({resolveWithObject: true});
}
var flatten = function (img, cb) {
return sharp(img.data, {raw: imgDesc(img)})
.flatten()
.raw()
.toBuffer({resolveWithObject: true});
//.png().toFile("./examples/foo.png");
}
module.exports = {
Load: function (color, id, text) {
var self = this;
var bg = getBackground(Color(color));
var icon = getIcon(id, text == "");
var text = getText(text);
var prom = [];
prom[0] = Promise.all([bg, icon, text])
.then(
function (x) {
return Promise.all([getComposite(x[0], x[1], true), text]);
}
)
.then(
function (x) {
return getComposite(x[0], x[1], false);
}
).then(flatten);
prom[1] = Promise.all([bg, icon])
.then(
function (x) {
return Promise.all([getComposite(x[0], x[1], false), text]);
}
)
.then(
function (x) {
return getComposite(x[0], x[1], false);
}
).then(flatten);
return Promise.all(prom);
}
};