-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.html
More file actions
661 lines (585 loc) · 20.3 KB
/
Copy pathui.html
File metadata and controls
661 lines (585 loc) · 20.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
padding: 16px;
background: #ffffff;
color: #000000;
overflow-y: auto;
}
h1 {
font-size: 16px;
font-weight: 600;
margin-bottom: 8px;
color: #000000;
}
.description {
font-size: 12px;
color: #666666;
margin-bottom: 20px;
line-height: 1.4;
}
.preview-container {
margin-bottom: 20px;
border: 1px solid #e5e5e5;
border-radius: 6px;
padding: 12px;
background: #fafafa;
}
.preview-container.empty {
padding: 40px 20px;
color: #888888;
font-size: 12px;
text-align: center;
}
.preview-grid {
display: flex;
gap: 12px;
align-items: flex-start;
}
.preview-column {
flex: 1;
text-align: center;
}
.preview-image {
width: 100%;
max-height: 300px;
object-fit: contain;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
background: #ffffff;
}
.preview-label {
font-size: 11px;
color: #666666;
margin-top: 8px;
font-weight: 500;
}
.option-group {
margin-bottom: 20px;
}
.dropdown-container {
position: relative;
}
select {
width: 100%;
padding: 12px 16px;
font-size: 13px;
font-weight: 400;
border: 1px solid #e5e5e5;
border-radius: 6px;
background: #ffffff;
color: #000000;
cursor: pointer;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L6 6L11 1' stroke='%23666666' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 12px center;
padding-right: 40px;
transition: all 0.2s;
}
select:hover {
border-color: #18a0fb;
background-color: #f5f5f5;
}
select:focus {
outline: none;
border-color: #18a0fb;
box-shadow: 0 0 0 2px rgba(24, 160, 251, 0.1);
}
.description-text {
font-size: 11px;
color: #888888;
margin-top: 8px;
line-height: 1.4;
}
.button {
width: 100%;
padding: 10px 16px;
font-size: 13px;
font-weight: 500;
border: none;
border-radius: 6px;
cursor: pointer;
transition: all 0.2s;
margin-bottom: 8px;
}
.button-primary {
background: #18a0fb;
color: #ffffff;
}
.button-primary:hover {
background: #0d8ce8;
}
.button-primary:active {
background: #0b7bc7;
}
.button-primary:disabled {
background: #cccccc;
cursor: not-allowed;
}
.button-secondary {
background: #f0f0f0;
color: #000000;
}
.button-secondary:hover {
background: #e0e0e0;
}
.loading {
display: none;
text-align: center;
padding: 10px;
font-size: 12px;
color: #666666;
}
.error-message {
display: none;
background: #fee;
border: 1px solid #fcc;
border-radius: 6px;
padding: 12px;
margin-bottom: 16px;
font-size: 12px;
color: #c00;
}
</style>
</head>
<body>
<h1>Color Blind Simulator</h1>
<p class="description">
Select a layer in Figma and choose a color blindness type to preview how it appears.
</p>
<div class="error-message" id="errorMessage"></div>
<div class="preview-container empty" id="previewContainer">
<div>Select a layer and click "Preview" to see the simulation</div>
</div>
<div class="loading" id="loading">Processing image...</div>
<div class="option-group">
<div class="dropdown-container">
<select id="colorBlindnessSelect">
<option value="normal">Normal Vision - No simulation</option>
<option value="protanopia">Protanopia - Red-blind (cannot see red light)</option>
<option value="deuteranopia">Deuteranopia - Green-blind (cannot see green light)</option>
<option value="tritanopia">Tritanopia - Blue-blind (cannot see blue light)</option>
<option value="protanomaly">Protanomaly - Red-weak (reduced red sensitivity)</option>
<option value="deuteranomaly">Deuteranomaly - Green-weak (reduced green sensitivity)</option>
<option value="tritanomaly">Tritanomaly - Blue-weak (reduced blue sensitivity)</option>
<option value="achromatopsia">Achromatopsia - Complete color blindness (grayscale)</option>
</select>
<div class="description-text" id="descriptionText">No simulation</div>
</div>
</div>
<button class="button button-primary" id="previewBtn">Preview</button>
<script>
// Color blindness simulation algorithms (same as in code.js)
function linearizeSRGB(c) {
if (c <= 0.04045) {
return c / 12.92;
} else {
return Math.pow((c + 0.055) / 1.055, 2.4);
}
}
function sRGBFromLinear(c) {
if (c <= 0.0031308) {
return 12.92 * c;
} else {
return 1.055 * Math.pow(c, 1.0 / 2.4) - 0.055;
}
}
function rgbToLms(r, g, b) {
var rLin = linearizeSRGB(r);
var gLin = linearizeSRGB(g);
var bLin = linearizeSRGB(b);
var l = 0.31399022 * rLin + 0.63951294 * gLin + 0.04649755 * bLin;
var m = 0.15537241 * rLin + 0.75789446 * gLin + 0.08670142 * bLin;
var s = 0.01775239 * rLin + 0.10944209 * gLin + 0.87256922 * bLin;
return [l, m, s];
}
function lmsToRgb(l, m, s) {
var rLin = 5.47221206 * l - 4.6419601 * m + 0.16963708 * s;
var gLin = -1.1252419 * l + 2.29317094 * m - 0.1678952 * s;
var bLin = 0.02980165 * l - 0.19318073 * m + 1.16364789 * s;
var r = sRGBFromLinear(rLin);
var g = sRGBFromLinear(gLin);
var b = sRGBFromLinear(bLin);
return [r, g, b];
}
function applyBrettelDichromacy(l, m, s, type) {
var l2, m2, s2;
switch (type) {
case 'protanopia':
l2 = 0.0 * l + 2.02344 * m + -2.52581 * s;
m2 = 0.0 * l + 1.0 * m + 0.0 * s;
s2 = 0.0 * l + 0.0 * m + 1.0 * s;
break;
case 'deuteranopia':
l2 = 1.0 * l + 0.0 * m + 0.0 * s;
m2 = 0.494207 * l + 0.0 * m + 1.24827 * s;
s2 = 0.0 * l + 0.0 * m + 1.0 * s;
break;
case 'tritanopia':
l2 = 1.0 * l + 0.0 * m + 0.0 * s;
m2 = 0.0 * l + 1.0 * m + 0.0 * s;
s2 = -0.395913 * l + 0.801109 * m + 0.0 * s;
break;
default:
return [l, m, s];
}
return [l2, m2, s2];
}
function applyMachadoAnomaly(l, m, s, type) {
var l2, m2, s2;
switch (type) {
case 'protanomaly':
l2 = 0.81667 * l + 0.18333 * m + 0.0 * s;
m2 = 0.0 * l + 1.0 * m + 0.0 * s;
s2 = 0.0 * l + 0.0 * m + 1.0 * s;
break;
case 'deuteranomaly':
l2 = 1.0 * l + 0.0 * m + 0.0 * s;
m2 = 0.22222 * l + 0.77778 * m + 0.0 * s;
s2 = 0.0 * l + 0.0 * m + 1.0 * s;
break;
case 'tritanomaly':
l2 = 1.0 * l + 0.0 * m + 0.0 * s;
m2 = 0.0 * l + 1.0 * m + 0.0 * s;
s2 = 0.0 * l + 0.33333 * m + 0.66667 * s;
break;
default:
return [l, m, s];
}
return [l2, m2, s2];
}
function applyColorBlindness(r, g, b, type) {
if (type === 'normal') {
return [r, g, b];
}
if (type === 'achromatopsia') {
var gray = 0.299 * r + 0.587 * g + 0.114 * b;
return [gray, gray, gray];
}
// Optimize: inline common operations to reduce function calls
var rLin = r <= 0.04045 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
var gLin = g <= 0.04045 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
var bLin = b <= 0.04045 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
var l = 0.31399022 * rLin + 0.63951294 * gLin + 0.04649755 * bLin;
var m = 0.15537241 * rLin + 0.75789446 * gLin + 0.08670142 * bLin;
var s = 0.01775239 * rLin + 0.10944209 * gLin + 0.87256922 * bLin;
var l2, m2, s2;
// Inline transformations for better performance
if (type === 'protanopia') {
l2 = 2.02344 * m - 2.52581 * s;
m2 = m;
s2 = s;
} else if (type === 'deuteranopia') {
l2 = l;
m2 = 0.494207 * l + 1.24827 * s;
s2 = s;
} else if (type === 'tritanopia') {
l2 = l;
m2 = m;
s2 = -0.395913 * l + 0.801109 * m;
} else if (type === 'protanomaly') {
l2 = 0.81667 * l + 0.18333 * m;
m2 = m;
s2 = s;
} else if (type === 'deuteranomaly') {
l2 = l;
m2 = 0.22222 * l + 0.77778 * m;
s2 = s;
} else if (type === 'tritanomaly') {
l2 = l;
m2 = m;
s2 = 0.33333 * m + 0.66667 * s;
} else {
return [r, g, b];
}
// Convert back to RGB
var rLin2 = 5.47221206 * l2 - 4.6419601 * m2 + 0.16963708 * s2;
var gLin2 = -1.1252419 * l2 + 2.29317094 * m2 - 0.1678952 * s2;
var bLin2 = 0.02980165 * l2 - 0.19318073 * m2 + 1.16364789 * s2;
var r2 = rLin2 <= 0.0031308 ? 12.92 * rLin2 : 1.055 * Math.pow(rLin2, 1.0 / 2.4) - 0.055;
var g2 = gLin2 <= 0.0031308 ? 12.92 * gLin2 : 1.055 * Math.pow(gLin2, 1.0 / 2.4) - 0.055;
var b2 = bLin2 <= 0.0031308 ? 12.92 * bLin2 : 1.055 * Math.pow(bLin2, 1.0 / 2.4) - 0.055;
return [
Math.max(0, Math.min(1, r2)),
Math.max(0, Math.min(1, g2)),
Math.max(0, Math.min(1, b2))
];
}
function loadImageToDataUrl(imageData) {
var img = new Image();
return new Promise(function(resolve, reject) {
img.onload = function() {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
resolve(canvas.toDataURL());
};
img.onerror = function() {
reject(new Error('Failed to load image'));
};
// Handle base64 string or array
if (typeof imageData === 'string') {
img.src = 'data:image/png;base64,' + imageData;
} else if (Array.isArray(imageData)) {
try {
var data = new Uint8Array(imageData);
var blob = new Blob([data], { type: 'image/png' });
var url = URL.createObjectURL(blob);
img.src = url;
} catch (error) {
reject(new Error('Failed to create image from array: ' + error.message));
}
} else {
reject(new Error('Invalid image data format: ' + typeof imageData));
}
});
}
function processImage(imageData, type) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var img = new Image();
return new Promise(function(resolve, reject) {
img.onload = function() {
try {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
var imageDataObj = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageDataObj.data;
var length = data.length;
// Optimize: cache color transformation for normal type
if (type === 'normal') {
// No processing needed for normal vision
ctx.putImageData(imageDataObj, 0, 0);
resolve(canvas.toDataURL());
return;
}
// Optimize: process all pixels at once with optimized loop
// Cache division by 255
var inv255 = 1 / 255;
for (var i = 0; i < length; i += 4) {
var r = data[i] * inv255;
var g = data[i + 1] * inv255;
var b = data[i + 2] * inv255;
var transformed = applyColorBlindness(r, g, b, type);
data[i] = Math.round(transformed[0] * 255);
data[i + 1] = Math.round(transformed[1] * 255);
data[i + 2] = Math.round(transformed[2] * 255);
// Alpha channel stays the same
}
ctx.putImageData(imageDataObj, 0, 0);
resolve(canvas.toDataURL());
} catch (error) {
reject(error);
}
};
img.onerror = function(error) {
reject(new Error('Failed to load image'));
};
// Handle base64 string or array
if (typeof imageData === 'string') {
// Base64 string
img.src = 'data:image/png;base64,' + imageData;
} else if (Array.isArray(imageData)) {
// Array - convert to Uint8Array then to blob
try {
var data = new Uint8Array(imageData);
var blob = new Blob([data], { type: 'image/png' });
var url = URL.createObjectURL(blob);
img.src = url;
} catch (error) {
reject(new Error('Failed to create image from array: ' + error.message));
}
} else {
reject(new Error('Invalid image data format: ' + typeof imageData));
}
});
}
var previewBtn = document.getElementById('previewBtn');
var previewContainer = document.getElementById('previewContainer');
var loading = document.getElementById('loading');
var errorMessage = document.getElementById('errorMessage');
var colorBlindnessSelect = document.getElementById('colorBlindnessSelect');
var descriptionText = document.getElementById('descriptionText');
var currentImageData = null;
var originalImageDataUrl = null;
var currentType = 'normal';
// Description mapping
var descriptions = {
'normal': 'No simulation',
'protanopia': 'Red-blind (cannot see red light)',
'deuteranopia': 'Green-blind (cannot see green light)',
'tritanopia': 'Blue-blind (cannot see blue light)',
'protanomaly': 'Red-weak (reduced red sensitivity)',
'deuteranomaly': 'Green-weak (reduced green sensitivity)',
'tritanomaly': 'Blue-weak (reduced blue sensitivity)',
'achromatopsia': 'Complete color blindness (grayscale)'
};
// Update description when selection changes
function updateDescription() {
var value = colorBlindnessSelect.value;
descriptionText.textContent = descriptions[value] || '';
}
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}
function hideError() {
errorMessage.style.display = 'none';
}
function updatePreview() {
if (!currentImageData) {
previewContainer.className = 'preview-container empty';
previewContainer.innerHTML = '<div>Select a layer and click "Preview" to see the simulation</div>';
return;
}
var type = colorBlindnessSelect.value || 'normal';
currentType = type;
console.log('Updating preview with type:', type);
loading.style.display = 'block';
previewContainer.className = 'preview-container';
// Load original image if not already loaded
var originalPromise = originalImageDataUrl
? Promise.resolve(originalImageDataUrl)
: loadImageToDataUrl(currentImageData).then(function(url) {
originalImageDataUrl = url;
return url;
});
// Process simulated image
var simulatedPromise = processImage(currentImageData, type);
Promise.all([originalPromise, simulatedPromise]).then(function(results) {
console.log('Preview processed successfully');
loading.style.display = 'none';
var originalUrl = results[0];
var simulatedUrl = results[1];
var typeLabel = type === 'normal' ? 'Normal Vision' : type.charAt(0).toUpperCase() + type.slice(1);
previewContainer.innerHTML =
'<div class="preview-grid">' +
'<div class="preview-column">' +
'<img src="' + originalUrl + '" class="preview-image" alt="Original">' +
'<div class="preview-label">Original</div>' +
'</div>' +
'<div class="preview-column">' +
'<img src="' + simulatedUrl + '" class="preview-image" alt="Simulated">' +
'<div class="preview-label">' + typeLabel + '</div>' +
'</div>' +
'</div>';
}).catch(function(error) {
console.error('Error processing preview:', error);
loading.style.display = 'none';
showError('Error processing image: ' + (error.message || error));
});
}
previewBtn.addEventListener('click', function() {
var type = colorBlindnessSelect.value;
if (type) {
hideError();
loading.style.display = 'block';
previewContainer.className = 'preview-container';
previewContainer.innerHTML = '<div>Loading...</div>';
parent.postMessage({
pluginMessage: {
type: 'preview-simulation',
colorBlindnessType: type
}
}, '*');
} else {
showError('Please select a color blindness type');
}
});
// Update preview when selection changes
colorBlindnessSelect.addEventListener('change', function() {
updateDescription();
var type = colorBlindnessSelect.value;
if (type) {
// If we have cached image data, update immediately
if (currentImageData) {
updatePreview();
} else {
// Otherwise, request new preview from Figma
loading.style.display = 'block';
previewContainer.className = 'preview-container';
previewContainer.innerHTML = '<div>Loading...</div>';
parent.postMessage({
pluginMessage: {
type: 'get-preview',
colorBlindnessType: type
}
}, '*');
}
}
});
// Request initial preview on load
window.addEventListener('load', function() {
updateDescription();
var type = colorBlindnessSelect.value;
if (type) {
parent.postMessage({
pluginMessage: {
type: 'get-preview',
colorBlindnessType: type
}
}, '*');
}
});
// Listen for messages from plugin
window.onmessage = function(event) {
var msg = event.data.pluginMessage;
if (!msg) {
console.log('No message received');
return;
}
console.log('Message received:', msg.type);
if (msg.type === 'image-data') {
console.log('Image data received, type:', msg.colorBlindnessType);
currentImageData = msg.imageData;
currentType = msg.colorBlindnessType;
// Reset original image data URL when new image is loaded
originalImageDataUrl = null;
hideError();
updatePreview();
} else if (msg.type === 'preview-error') {
console.error('Preview error:', msg.error);
loading.style.display = 'none';
showError(msg.error || 'An error occurred');
currentImageData = null;
} else if (msg.type === 'selection-changed') {
// Selection changed in Figma - update preview automatically
console.log('Selection changed, updating preview');
var type = colorBlindnessSelect.value;
if (type) {
loading.style.display = 'block';
previewContainer.className = 'preview-container';
previewContainer.innerHTML = '<div>Loading...</div>';
parent.postMessage({
pluginMessage: {
type: 'get-preview',
colorBlindnessType: type
}
}, '*');
}
} else if (msg.type === 'no-selection') {
// No layer selected
loading.style.display = 'none';
currentImageData = null;
originalImageDataUrl = null;
previewContainer.className = 'preview-container empty';
previewContainer.innerHTML = '<div>Select a layer to see the preview</div>';
}
};
</script>
</body>
</html>