-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfilter.js
162 lines (153 loc) · 3.23 KB
/
filter.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { mapper, mapTransferLinear } from './map.js';
import converter from './converter.js';
import prepare from './_prepare.js';
import { getMode } from './modes.js';
const minzero = v => Math.max(v, 0);
const clamp = v => Math.max(Math.min(v, 1), 0);
const lerp = (a, b, t) =>
a === undefined || b === undefined ? undefined : a + t * (b - a);
const matrixSepia = amount => {
let a = 1 - clamp(amount);
return [
0.393 + 0.607 * a,
0.769 - 0.769 * a,
0.189 - 0.189 * a,
0,
0.349 - 0.349 * a,
0.686 + 0.314 * a,
0.168 - 0.168 * a,
0,
0.272 - 0.272 * a,
0.534 - 0.534 * a,
0.131 + 0.869 * a,
0,
0,
0,
0,
1
];
};
const matrixSaturate = sat => {
let s = minzero(sat);
return [
0.213 + 0.787 * s,
0.715 - 0.715 * s,
0.072 - 0.072 * s,
0,
0.213 - 0.213 * s,
0.715 + 0.285 * s,
0.072 - 0.072 * s,
0,
0.213 - 0.213 * s,
0.715 - 0.715 * s,
0.072 + 0.928 * s,
0,
0,
0,
0,
1
];
};
const matrixGrayscale = amount => {
let a = 1 - clamp(amount);
return [
0.2126 + 0.7874 * a,
0.7152 - 0.7152 * a,
0.0722 - 0.0722 * a,
0,
0.2126 - 0.2126 * a,
0.7152 + 0.2848 * a,
0.0722 - 0.0722 * a,
0,
0.2126 - 0.2126 * a,
0.7152 - 0.7152 * a,
0.0722 + 0.9278 * a,
0,
0,
0,
0,
1
];
};
const matrixHueRotate = degrees => {
let rad = (Math.PI * degrees) / 180;
let c = Math.cos(rad);
let s = Math.sin(rad);
return [
0.213 + c * 0.787 - s * 0.213,
0.715 - c * 0.715 - s * 0.715,
0.072 - c * 0.072 + s * 0.928,
0,
0.213 - c * 0.213 + s * 0.143,
0.715 + c * 0.285 + s * 0.14,
0.072 - c * 0.072 - s * 0.283,
0,
0.213 - c * 0.213 - s * 0.787,
0.715 - c * 0.715 + s * 0.715,
0.072 + c * 0.928 + s * 0.072,
0,
0,
0,
0,
1
];
};
const matrix = (values, mode, preserve_mode = false) => {
let conv = converter(mode);
let channels = getMode(mode).channels;
return color => {
let c = conv(color);
if (!c) {
return undefined;
}
let res = { mode };
let ch;
let count = channels.length;
for (let i = 0; i < values.length; i++) {
ch = channels[Math.floor(i / count)];
if (c[ch] === undefined) {
continue;
}
res[ch] =
(res[ch] || 0) + values[i] * (c[channels[i % count]] || 0);
}
if (!preserve_mode) {
return res;
}
let prep = prepare(color);
return prep && res.mode !== prep.mode ? converter(prep.mode)(res) : res;
};
};
const filterBrightness = (amt = 1, mode = 'rgb') => {
let a = minzero(amt);
return mapper(mapTransferLinear(a), mode, true);
};
const filterContrast = (amt = 1, mode = 'rgb') => {
let a = minzero(amt);
return mapper(mapTransferLinear(a, (1 - a) / 2), mode, true);
};
const filterSepia = (amt = 1, mode = 'rgb') =>
matrix(matrixSepia(amt), mode, true);
const filterSaturate = (amt = 1, mode = 'rgb') =>
matrix(matrixSaturate(amt), mode, true);
const filterGrayscale = (amt = 1, mode = 'rgb') =>
matrix(matrixGrayscale(amt), mode, true);
const filterInvert = (amt = 1, mode = 'rgb') => {
let a = clamp(amt);
return mapper(
(v, ch) => (ch === 'alpha' ? v : lerp(a, 1 - a, v)),
mode,
true
);
};
const filterHueRotate = (deg = 0, mode = 'rgb') =>
matrix(matrixHueRotate(deg), mode, true);
export {
filterBrightness,
filterContrast,
filterSepia,
filterSaturate,
filterGrayscale,
filterInvert,
filterHueRotate
};