-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
348 lines (300 loc) · 11.5 KB
/
background.js
File metadata and controls
348 lines (300 loc) · 11.5 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Background script for Colorize
// Color Vision Deficiency Matrix used for daltonizing images
var CVDMatrix = {
"Protanopia": [
0.0, 2.02344, -2.52581,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
],
"Deuteranopia": [
1.0, 0.0, 0.0,
0.494207, 0.0, 1.24827,
0.0, 0.0, 1.0
],
"Tritanopia": [
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
-0.395913, 0.801109, 0.0
]
};
// called when normal radio button is checked
function normalImgs() {
// refreshes page and sets all images back to original source
}
// called when protanopia radio button is checked
// daltonizes images to corrected for protanopia color blindness using the CVD Matrix
function proImgs() {
const imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
if (!imgs[i].alt.endsWith("Protanopia")) {
if (imgs[i].width > 0 && imgs[i].height > 0) {
imgs[i].crossOrigin = "Anonymous";
var canvas = document.createElement('canvas');
canvas.width = imgs[i].width;
canvas.height = imgs[i].height;
var ctx = canvas.getContext('2d');
ctx.drawImage(imgs[i], 0, 0, canvas.width, canvas.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
var cvd = CVDMatrix["Protanopia"],
cvd_a = cvd[0],
cvd_b = cvd[1],
cvd_c = cvd[2],
cvd_d = cvd[3],
cvd_e = cvd[4],
cvd_f = cvd[5],
cvd_g = cvd[6],
cvd_h = cvd[7],
cvd_i = cvd[8];
var L, M, S, l, m, s, R, G, B, RR, GG, BB;
if (!data.every(x => x === 0)) {
for(var j = 0, length = data.length; j < length; j += 4) {
var r = data[j],
g = data[j + 1],
b = data[j + 2];
// RGB to LMS matrix conversion
L = (17.8824 * r) + (43.5161 * g) + (4.11935 * b);
M = (3.45565 * r) + (27.1554 * g) + (3.86714 * b);
S = (0.0299566 * r) + (0.184309 * g) + (1.46709 * b);
// Simulate color blindness
l = (cvd_a * L) + (cvd_b * M) + (cvd_c * S);
m = (cvd_d * L) + (cvd_e * M) + (cvd_f * S);
s = (cvd_g * L) + (cvd_h * M) + (cvd_i * S);
// LMS to RGB matrix conversion
R = (0.0809444479 * l) + (-0.130504409 * m) + (0.116721066 * s);
G = (-0.0102485335 * l) + (0.0540193266 * m) + (-0.113614708 * s);
B = (-0.000365296938 * l) + (-0.00412161469 * m) + (0.693511405 * s);
// Isolate invisible colors to color vision deficiency (calculate error matrix)
R = r - R;
G = g - G;
B = b - B;
// Shift colors towards visible spectrum (apply error modifications)
RR = (0.0 * R) + (0.0 * G) + (0.0 * B);
GG = (0.7 * R) + (1.0 * G) + (0.0 * B);
BB = (0.7 * R) + (0.0 * G) + (1.0 * B);
// Add compensation to original values
R = RR + r;
G = GG + g;
B = BB + b;
// Clamp values
R = Math.max(0, Math.min(255, R))
G = Math.max(0, Math.min(255, G))
B = Math.max(0, Math.min(255, B))
// Record color
data[j] = R >> 0;
data[j + 1] = G >> 0;
data[j + 2] = B >> 0;
}
ctx.putImageData(imageData, 0, 0);
var dataUrl = canvas.toDataURL();
imgs[i].src = dataUrl;
imgs[i].setAttribute("alt", imgs[i].alt + " - Corrected for Protanopia")
}
}
}
}
}
// called when deuteranopia radio button is checked
// daltonizes images to corrected for deuteranopia color blindness using the CVD Matrix
function deuImgs(){
const imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
if (!imgs[i].alt.endsWith("Deuteranopia")) {
if (imgs[i].width > 0 && imgs[i].height > 0) {
imgs[i].crossOrigin = "Anonymous";
var canvas = document.createElement('canvas');
canvas.width = imgs[i].width;
canvas.height = imgs[i].height;
var ctx = canvas.getContext('2d');
ctx.drawImage(imgs[i], 0, 0, canvas.width, canvas.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
var cvd = CVDMatrix["Deuteranopia"],
cvd_a = cvd[0],
cvd_b = cvd[1],
cvd_c = cvd[2],
cvd_d = cvd[3],
cvd_e = cvd[4],
cvd_f = cvd[5],
cvd_g = cvd[6],
cvd_h = cvd[7],
cvd_i = cvd[8];
var L, M, S, l, m, s, R, G, B, RR, GG, BB;
if (!data.every(x => x === 0)) {
for(var j = 0, length = data.length; j < length; j += 4) {
var r = data[j],
g = data[j + 1],
b = data[j + 2];
// RGB to LMS matrix conversion
L = (17.8824 * r) + (43.5161 * g) + (4.11935 * b);
M = (3.45565 * r) + (27.1554 * g) + (3.86714 * b);
S = (0.0299566 * r) + (0.184309 * g) + (1.46709 * b);
// Simulate color blindness
l = (cvd_a * L) + (cvd_b * M) + (cvd_c * S);
m = (cvd_d * L) + (cvd_e * M) + (cvd_f * S);
s = (cvd_g * L) + (cvd_h * M) + (cvd_i * S);
// LMS to RGB matrix conversion
R = (0.0809444479 * l) + (-0.130504409 * m) + (0.116721066 * s);
G = (-0.0102485335 * l) + (0.0540193266 * m) + (-0.113614708 * s);
B = (-0.000365296938 * l) + (-0.00412161469 * m) + (0.693511405 * s);
// Isolate invisible colors to color vision deficiency (calculate error matrix)
R = r - R;
G = g - G;
B = b - B;
// Shift colors towards visible spectrum (apply error modifications)
RR = (0.0 * R) + (0.0 * G) + (0.0 * B);
GG = (0.7 * R) + (1.0 * G) + (0.0 * B);
BB = (0.7 * R) + (0.0 * G) + (1.0 * B);
// Add compensation to original values
R = RR + r;
G = GG + g;
B = BB + b;
// Clamp values
R = Math.max(0, Math.min(255, R))
G = Math.max(0, Math.min(255, G))
B = Math.max(0, Math.min(255, B))
// Record color
data[j] = R >> 0;
data[j + 1] = G >> 0;
data[j + 2] = B >> 0;
}
// Record data
ctx.putImageData(imageData, 0, 0);
var dataUrl = canvas.toDataURL();
imgs[i].src = dataUrl;
imgs[i].setAttribute("alt", imgs[i].alt + " - Corrected for Deuteranopia")
}
}
}
}
}
// called when tritanopia radio button is checked
// daltonizes images to corrected for tritanopia color blindness using the CVD Matrix
function triImgs(){
const imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
if (!imgs[i].alt.endsWith("Tritanopia")) {
if (imgs[i].width > 0 && imgs[i].height > 0) {
imgs[i].crossOrigin = "Anonymous";
var canvas = document.createElement('canvas');
canvas.width = imgs[i].width;
canvas.height = imgs[i].height;
var ctx = canvas.getContext('2d');
ctx.drawImage(imgs[i], 0, 0, canvas.width, canvas.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
var cvd = CVDMatrix["Tritanopia"],
cvd_a = cvd[0],
cvd_b = cvd[1],
cvd_c = cvd[2],
cvd_d = cvd[3],
cvd_e = cvd[4],
cvd_f = cvd[5],
cvd_g = cvd[6],
cvd_h = cvd[7],
cvd_i = cvd[8];
var L, M, S, l, m, s, R, G, B, RR, GG, BB;
if (!data.every(x => x === 0)) {
for(var j = 0, length = data.length; j < length; j += 4) {
var r = data[j],
g = data[j + 1],
b = data[j + 2];
// RGB to LMS matrix conversion
L = (17.8824 * r) + (43.5161 * g) + (4.11935 * b);
M = (3.45565 * r) + (27.1554 * g) + (3.86714 * b);
S = (0.0299566 * r) + (0.184309 * g) + (1.46709 * b);
// Simulate color blindness
l = (cvd_a * L) + (cvd_b * M) + (cvd_c * S);
m = (cvd_d * L) + (cvd_e * M) + (cvd_f * S);
s = (cvd_g * L) + (cvd_h * M) + (cvd_i * S);
// LMS to RGB matrix conversion
R = (0.0809444479 * l) + (-0.130504409 * m) + (0.116721066 * s);
G = (-0.0102485335 * l) + (0.0540193266 * m) + (-0.113614708 * s);
B = (-0.000365296938 * l) + (-0.00412161469 * m) + (0.693511405 * s);
// Isolate invisible colors to color vision deficiency (calculate error matrix)
R = r - R;
G = g - G;
B = b - B;
// Shift colors towards visible spectrum (apply error modifications)
RR = (0.0 * R) + (0.0 * G) + (0.0 * B);
GG = (0.7 * R) + (1.0 * G) + (0.0 * B);
BB = (0.7 * R) + (0.0 * G) + (1.0 * B);
// Add compensation to original values
R = RR + r;
G = GG + g;
B = BB + b;
// Clamp values
R = Math.max(0, Math.min(255, R))
G = Math.max(0, Math.min(255, G))
B = Math.max(0, Math.min(255, B))
// Record color
data[j] = R >> 0;
data[j + 1] = G >> 0;
data[j + 2] = B >> 0;
}
ctx.putImageData(imageData, 0, 0);
var dataUrl = canvas.toDataURL();
imgs[i].src = dataUrl;
imgs[i].setAttribute("alt", imgs[i].alt + " - Corrected for Tritanopia")
}
}
}
}
}
function satImgs(satValue){
const imgs = document.getElementsByTagName("img");
for (var i = 0; i< imgs.length; i++){
imgs[i].style.filter = "saturate(" + String(satValue) + "%)";
}
}
chrome.runtime.onMessage.addListener(gotMessage);
chrome.runtime.onMessage.addListener(gotRefresh);
chrome.runtime.onMessage.addListener(gotSaturation);
var vision;
function gotMessage(message, sender, sendResponse) {
vision = message.mode;
if (vision == "normal"){
normalImgs();
}
else if (vision == "pro") {
proImgs();
}
else if (vision == "deu") {
deuImgs();
}
else if (vision == "tri") {
triImgs();
}
}
function gotRefresh(message, sender, sendResponse) {
if (message.yes == "refresh") {
location.reload();
}
}
var saturation = 100;
function gotSaturation(message, sender, sendResponse) {
if (message.saturation != undefined) {
saturation = message.saturation;
}
satImgs(message.saturation);
}
function checkVision() {
if (vision == "normal"){
normalImgs();
}
else if (vision == "pro") {
proImgs();
}
else if (vision == "deu") {
deuImgs();
}
else if (vision == "tri") {
triImgs();
}
}
function checkSaturation() {
satImgs(saturation)
}
setInterval(checkVision, 100)
addEventListener('scroll', checkVision);
addEventListener('scroll', checkSaturation);