Permalink
Newer
100644
84 lines (65 sloc)
2.3 KB
7
function changeColors(iconData, fillColor, strokeColor) {
8
var re;
9
10
if (fillColor) {
11
re = /(<!ENTITY fill_color ")(.*)(">)/;
12
iconData = iconData.replace(re, "$1" + fillColor + "$3");
13
}
14
15
if (strokeColor) {
16
re = /(<!ENTITY stroke_color ")(.*)(">)/;
17
iconData = iconData.replace(re, "$1" + strokeColor + "$3");
18
}
19
20
return iconData;
21
}
22
25
var dataHeader = "data:image/svg+xml,";
26
33
var fillColor = iconInfo.fillColor;
34
var strokeColor = iconInfo.strokeColor;
36
// If source is already a data uri, read it instead of doing
37
// the XMLHttpRequest
38
if (source.substring(0, 4) == 'data') {
39
var iconData = unescape(source.slice(dataHeader.length));
40
var newData = changeColors(iconData, fillColor, strokeColor);
41
callback(dataHeader + escape(newData));
42
return;
43
}
44
46
47
client.onload = function () {
48
var iconData = this.responseText;
49
var newData = changeColors(iconData, fillColor, strokeColor);
50
callback(dataHeader + escape(newData));
57
function getBackgroundURL(elem) {
58
var style = elem.currentStyle || window.getComputedStyle(elem, '');
59
// Remove prefix 'url(' and suffix ')' before return
60
return style.backgroundImage.slice(4, -1);
61
}
62
63
function setBackgroundURL(elem, url) {
64
elem.style.backgroundImage = "url('" + url + "')";
65
}
66
67
icon.colorize = function (elem, colors, callback) {
68
var iconInfo = {
69
"uri": getBackgroundURL(elem),
70
"strokeColor": colors.stroke,
71
"fillColor": colors.fill