-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacecolor.js
60 lines (48 loc) · 1.82 KB
/
replacecolor.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
// Replace Color - https://github.com/klase/impact-replacecolor
// Based on Image Blender - https://github.com/deakster/impact-imageblender
ig.module(
'plugins.replacecolor'
).requires(
'impact.image'
).defines(function () {
ig.Image.inject({
onload: function( event ) {
this.parent( event );
var hashIndex = this.path.indexOf('#');
if (hashIndex > 0 && this.path.match(/#/g).length === 2) {
this.convertToCanvas();
var rRed = parseInt(this.path.substr(hashIndex + 8, 2), 16),
rGreen = parseInt(this.path.substr(hashIndex + 10, 2), 16),
rBlue = parseInt(this.path.substr(hashIndex + 12, 2), 16),
ctx = this.data.getContext("2d"),
imgData = ctx.getImageData(0, 0, this.data.width, this.data.height),
src = imgData.data,
len = src.length,
dRed = parseInt(this.path.substr(hashIndex + 1, 2), 16),
dGreen = parseInt(this.path.substr(hashIndex + 3, 2), 16),
dBlue = parseInt(this.path.substr(hashIndex + 5, 2), 16);
for (var px = 0; px < len; px += 4) {
var red = src[px],
green = src[px+1],
blue = src[px+2];
if (red === rRed && green === rGreen && blue === rBlue) {
src[px] = dRed;
src[px+1] = dGreen;
src[px+2] = dBlue;
}
}
ctx.putImageData(imgData, 0, 0);
}
},
convertToCanvas: function () {
if (this.data.getContext) {
return;
}
var orig = ig.$new('canvas');
orig.width = this.width;
orig.height = this.height;
orig.getContext('2d').drawImage( this.data, 0, 0, this.width, this.height, 0, 0, this.width, this.height );
this.data = orig;
}
});
});