-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (66 loc) · 1.73 KB
/
index.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
/**
* Removes color from src, attempting to preserve anti-aliasing.
*
* Adapted from GIMP's color-to-alpha plugin by Seth Burgess, algorithm by clahey
*
* https://git.gnome.org/browse/gimp/tree/plug-ins/common/color-to-alpha.c?h=gimp-2-8
*
* @param {number[]} src
* @param {number[]} color
* @return {number[]}
*/
function color_to_alpha (src, color) {
var alphaR, alphaG, alphaB, alphaA;
var srcR = src[0] / 255.0; // enforce floats
var srcG = src[1] / 255.0;
var srcB = src[2] / 255.0;
var srcA = src[3] / 255.0;
var colorR = color[0] / 255.0;
var colorG = color[1] / 255.0;
var colorB = color[2] / 255.0;
alphaA = srcA;
if (colorR < 0.0001) {
alphaR = srcR;
} else if (srcR > colorR) {
alphaR = (srcR - colorR) / (1.0 - colorR);
} else if (srcR < colorR) {
alphaR = (colorR - srcR) / colorR;
} else {
alphaR = 0.0;
}
if (colorG < 0.0001) {
alphaG = srcG;
} else if (srcG > colorG) {
alphaG = (srcG - colorG) / (1.0 - colorG);
} else if (srcG < colorG) {
alphaG = (colorG - srcG) / colorG;
} else {
alphaG = 0.0;
}
if (colorB < 0.0001) {
alphaB = srcB;
} else if (srcB > colorB) {
alphaB = (srcB - colorB) / (1.0 - colorB);
} else if (srcB < colorB) {
alphaB = (colorB - srcB) / colorB;
} else {
alphaB = 0.0;
}
if (alphaR > alphaG) {
if (alphaR > alphaB) {
srcA = alphaR;
} else {
srcA = alphaB;
}
} else if (alphaG > alphaB) {
srcA = alphaG;
} else {
srcA = alphaB;
}
srcR = (srcR - colorR)/srcA + colorR;
srcG = (srcG - colorG)/srcA + colorG;
srcB = (srcB - colorB)/srcA + colorB;
srcA = srcA * alphaA;
return [~~(255*srcR), ~~(255*srcG), ~~(255*srcB), ~~(255*srcA)];
};
module.exports = color_to_alpha;