-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsoftproof.html
More file actions
745 lines (672 loc) · 33.5 KB
/
Copy pathsoftproof.html
File metadata and controls
745 lines (672 loc) · 33.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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<!doctype html>
<!--
Copyright (c) 2026 Glenn Wilton, O2 Creative Limited
Released under the MIT License (see ./LICENSE).
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jsColorEngine · Soft Proof Demo</title>
<link rel="stylesheet" href="styles/styles.css">
<style>
/* ── Floating colour-picker tooltip ─────────────────────────────── */
#pickerTooltip {
position: fixed;
z-index: 100;
pointer-events: none;
display: none;
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.5);
font-family: var(--mono);
font-size: 11px;
line-height: 1.45;
padding: 0;
overflow: hidden;
min-width: 210px;
}
#pickerTooltip.is-visible { display: block; }
.tip-swatches {
display: flex;
height: 28px;
border-radius: 7px 7px 0 0;
overflow: hidden;
}
.tip-sw-left, .tip-sw-right { flex: 1; }
.tip-body {
padding: 8px 10px;
display: grid;
grid-template-columns: auto 1fr;
gap: 2px 10px;
}
.tip-lbl {
color: var(--text-muted);
text-transform: uppercase;
font-size: 9px;
letter-spacing: 0.06em;
align-self: center;
}
.tip-val {
color: var(--text);
white-space: nowrap;
}
.tip-val.de-ok { color: var(--accent-2); }
.tip-val.de-warn { color: var(--warn); }
.tip-val.de-bad { color: var(--error); }
.tip-sep {
grid-column: 1 / -1;
border-top: 1px solid var(--border-soft);
margin: 3px 0;
}
canvas.hoverable { cursor: crosshair; }
.timing-inline {
font-family: var(--mono);
font-size: 12px;
color: var(--accent-2);
padding-bottom: 8px;
white-space: nowrap;
}
.timing-inline.is-busy { color: var(--accent); }
</style>
<script src="lib/devtools-warn.js"></script>
<script src="browser/jsColorEngineWeb.js"></script>
</head>
<body>
<header class="topbar">
<div class="topbar-head">
<div class="brand">
<span class="brand-mark">jsColorEngine</span>
<span class="brand-tag">samples</span>
</div>
<nav class="nav" aria-label="Site">
<a href="index.html">Home</a>
<a href="samples.html" class="is-active">Samples</a>
<a href="bench/">Bench</a>
</nav>
</div>
<nav class="nav nav--secondary" aria-label="Sample demos">
<a href="samples.html">Samples</a>
<a href="live-video-softproof.html">Live Video</a>
<a href="softproof.html" class="is-active">Soft Proof</a>
<a href="softproof-vs-lcms.html">jsCE vs lcms</a>
<a href="lut-cmyk-to-rgb.html">CMYK → RGB LUT</a>
<a href="lut-tiff-builder.html">LUT TIFF Builder</a>
<a href="colour-calculator.html">Colour Calculator</a>
</nav>
</header>
<main>
<!-- INTRO CARD ============================================================ -->
<section class="card">
<h2>Soft proof + CMYK separations</h2>
<p class="lead">
Load an image, pick a press profile and rendering intent, see the
simulated print output alongside the four
C / M / Y / K ink plates.
Hover any canvas for a floating colour picker with source sRGB, absolute
Lab, CMYK ink percentages, CMYK round-trip Lab, and both
ΔE 00 (perceptual) and ΔE 76 (geometric).
The split swatch at the top shows source RGB (left) vs soft-proof
RGB (right) so you can see the gamut compression at a glance.
</p>
<p class="muted">
Source: <a href="ICCImage/iccimage.js"><code>iccimage.js</code></a>
· API reference: <a href="ICCImage/ICCImage.md"><code>ICCImage.md</code></a>
· License: MIT
· Lab uses relative colorimetric (D50-adapted)
</p>
</section>
<!-- CONTROLS ============================================================== -->
<section class="card">
<div class="controls">
<label>
CMYK Profile
<select id="profileSelect">
<option value="profiles/CoatedGRACoL2006.icc" selected>GRACoL 2006 Coated (US)</option>
<option value="profiles/ISOcoated_v2_eci.icc">ISO Coated v2 (ECI)</option>
<option value="profiles/eciCMYK_v2.icc">eciCMYK v2 (Generic Exchange)</option>
</select>
</label>
<label>
Intent + BPC
<select id="intentSelect">
<option value="perceptual" selected>Perceptual</option>
<!-- <option value="perceptual-bpc">Perceptual + BPC</option>-->
<option value="relative">Relative</option>
<option value="relative-bpc">Relative + BPC</option>
<option value="saturation">Saturation</option>
<option value="saturation-bpc">Saturation + BPC</option>
<option value="absolute">Absolute</option>
</select>
</label>
<label>
Source Image
<select id="imageSelect">
<optgroup label="Portrait (1200×800)">
<option value="https://picsum.photos/id/23/800/1200">Forks</option>
<option value="https://picsum.photos/id/355/800/1200">Cameras</option>
<option value="https://picsum.photos/id/376/800/1200">New York</option>
<option value="https://picsum.photos/id/454/800/1200">Holding Camera</option>
<option value="https://picsum.photos/id/766/800/1200">Coffee Beans</option>
<option value="https://picsum.photos/id/838/800/1200">Woman and Child</option>
<option value="https://picsum.photos/id/870/800/1200">Lighthouse</option>
<option value="https://picsum.photos/id/981/800/1200">Stars</option>
<option value="https://picsum.photos/id/631/800/1200">Leaf</option>
</optgroup>
<optgroup label="Landscape (1200×800)">
<option value="https://picsum.photos/id/1069/1200/800">Jellyfish</option>
<option value="https://picsum.photos/id/1015/1200/800">Mountain lake</option>
<option value="https://picsum.photos/id/237/1200/800">Black Dog</option>
<option value="https://picsum.photos/id/287/1200/800">Landscape</option>
<option value="https://picsum.photos/id/292/1200/800">Vegetables</option>
<option value="https://picsum.photos/id/323/1200/800">Coastline</option>
<option value="https://picsum.photos/id/502/1200/800">Forest</option>
<option value="https://picsum.photos/id/871/1200/800">Cove</option>
<option value="https://picsum.photos/id/553/1200/800">Autumn</option>
<option value="https://picsum.photos/id/1074/1200/800">Lioness</option>
</optgroup>
</select>
</label>
<label>
Gamut warning
<select id="gamutSelect">
<option value="none" selected>Off</option>
<option value="color-2">Colour ΔE 2 (tight)</option>
<option value="color-5">Colour ΔE 5 (typical)</option>
<option value="color-10">Colour ΔE 10 (loose)</option>
<option value="colorMap">Colour heatmap</option>
<option value="map">Raw ΔE map</option>
</select>
</label>
<button class="btn-primary" id="runBtn">Run soft proof</button>
<span class="timing-inline" id="timingInline"></span>
</div>
</section>
<!-- SCREEN vs PROOF ======================================================= -->
<section class="card">
<h2>Original vs soft proof</h2>
<div class="demo-grid">
<div>
<h3>Original (sRGB)</h3>
<canvas id="screenCanvas" class="hoverable"></canvas>
<div class="info-grid" id="screenInfo" style="margin-top:10px"></div>
</div>
<div>
<h3>Soft proof <span class="muted">— simulated press output</span></h3>
<canvas id="proofCanvas" class="hoverable"></canvas>
<div class="info-grid" id="proofInfo" style="margin-top:10px"></div>
</div>
</div>
</section>
<!-- SEPARATIONS =========================================================== -->
<section class="card">
<h2>CMYK separations</h2>
<p class="lead">
Each plate below is the actual ink-channel data from the
<code>toSeparation()</code> output, rendered via
<code>renderChannelAs()</code>.
</p>
<div class="channels-grid" id="channelContainer"></div>
</section>
<!-- TIMING BREAKDOWN ====================================================== -->
<section class="card">
<h2>Performance breakdown</h2>
<div class="info-grid">
<div class="info-cell"><span class="lbl">Profile load</span><span class="val" id="t-profile">—</span></div>
<div class="info-cell"><span class="lbl">Image pixels</span><span class="val" id="t-pixels">—</span></div>
<div class="info-cell"><span class="lbl">Cache</span><span class="val" id="t-cache">—</span></div>
</div>
<h3 style="margin:16px 0 6px; font-size:13px; color:var(--text-dim); text-transform:uppercase; letter-spacing:0.06em">
Cold run <span style="text-transform:none; letter-spacing:0">(LUT build + transform)</span>
</h3>
<div class="info-grid">
<div class="info-cell"><span class="lbl">Proof (src→cmyk→sRGB)</span><span class="val" id="tc-proof">—</span></div>
<div class="info-cell"><span class="lbl">Separation (src→cmyk)</span><span class="val" id="tc-sep">—</span></div>
<div class="info-cell"><span class="lbl">Canvas render</span><span class="val" id="tc-render">—</span></div>
<div class="info-cell"><span class="lbl">Total</span><span class="val" id="tc-total">—</span></div>
</div>
<h3 style="margin:16px 0 6px; font-size:13px; color:var(--text-dim); text-transform:uppercase; letter-spacing:0.06em">
Warm run <span style="text-transform:none; letter-spacing:0">(cached LUT, transform only)</span>
</h3>
<div class="info-grid">
<div class="info-cell"><span class="lbl">Proof</span><span class="val" id="tw-proof">—</span></div>
<div class="info-cell"><span class="lbl">Proof MPx/s</span><span class="val" id="tw-proof-mpx">—</span></div>
<div class="info-cell"><span class="lbl">Separation</span><span class="val" id="tw-sep">—</span></div>
<div class="info-cell"><span class="lbl">Sep MPx/s</span><span class="val" id="tw-sep-mpx">—</span></div>
<div class="info-cell"><span class="lbl">Canvas render</span><span class="val" id="tw-render">—</span></div>
<div class="info-cell"><span class="lbl">Total</span><span class="val" id="tw-total">—</span></div>
</div>
<p class="muted" style="margin-top:10px">
Cold = first run (LUT build + transform). Warm = second run
(cached LUT, transform only). Transform and canvas render are
timed separately. MPx/s is for the warm transform pass only.
</p>
</section>
</main>
<!-- FLOATING TOOLTIP ====================================================== -->
<div id="pickerTooltip">
<div class="tip-swatches">
<div class="tip-sw-left" id="tipSwatchRGB"></div>
<div class="tip-sw-right" id="tipSwatchCMYK"></div>
</div>
<div class="tip-body" id="tipBody"></div>
</div>
<footer class="footer">
<span>© 2026 Glenn Wilton, O2 Creative Limited · <a href="LICENSE">MIT License</a></span>
<span>
<a href="https://github.com/glennwilton/jsColorEngine">GitHub</a>
·
<a href="bench/">Bench</a>
</span>
</footer>
<script type="module">
// ── ICCImage ──────────────────────────────────────────────────────────
//
// ICCImage is a small immutable image wrapper around jsColorEngine.
// It owns the workflow: "I have an image → I want to proof / separate /
// inspect it." Every conversion returns a NEW ICCImage — the source is
// never mutated.
//
// Key concepts:
//
// ICCImage.fromHTMLImage(el) — create from an <img> / canvas / bitmap.
// Pixels are extracted via a 2D canvas
// (always 8-bit RGBA sRGB).
//
// img.toProof(profile, opts) — soft-proof: src → CMYK → sRGB.
// Returns an sRGB-tagged ICCImage whose
// pixels simulate what the image would
// look like printed on the given profile.
//
// img.toSeparation(profile) — convert to the profile's native space
// (typically CMYK). The returned ICCImage
// carries real ink-channel data — its
// raw buffer IS the separation.
//
// img.toCanvas(canvas) — blit to a <canvas>. If the image is
// already sRGB uint8, blits direct. If
// not, it auto-builds (and caches) a
// [terminal → sRGB] transform first.
//
// img.renderChannelAs('C') — extract one ink channel (e.g. Cyan)
// and render it as a tinted RGBA ICCImage.
//
// img.pixel(x, y) — single-pixel readout via the accuracy
// path (no LUT). Returns { lab, srgb,
// device } — useful for colour pickers.
//
// img.info — metadata: dimensions, colour space,
// profile name, chain history, cache size.
//
// ChannelSets.CMYK provides channel definitions (index, name, short,
// tint colour) for iterating ink plates.
//
// TransformCache is shared between an ICCImage and all its derivatives
// (toProof / toSeparation / etc.), so the expensive LUT build only
// happens once per unique (chain + intent + BPC) combination.
//
// See iccimage.js header and ICCImage.md for the full API.
import { ICCImage, ChannelSets } from './ICCImage/iccimage.js';
// jsColorEngine core — Profile for loading ICC files, eIntent for
// rendering intents, convert for deltaE colour-difference formulas.
const { Profile, eIntent, convert } = window.jsColorEngine;
// ── DOM references ────────────────────────────────────────────────────
const imgEl = document.createElement('img');
imgEl.crossOrigin = 'anonymous';
const runBtn = document.getElementById('runBtn');
const profileSel = document.getElementById('profileSelect');
const intentSel = document.getElementById('intentSelect');
const imageSel = document.getElementById('imageSelect');
const gamutSel = document.getElementById('gamutSelect');
const screenCv = document.getElementById('screenCanvas');
const proofCv = document.getElementById('proofCanvas');
const screenInfo = document.getElementById('screenInfo');
const proofInfo = document.getElementById('proofInfo');
const channelHost = document.getElementById('channelContainer');
const timingInline = document.getElementById('timingInline');
const tooltip = document.getElementById('pickerTooltip');
const tipSwatchRGB = document.getElementById('tipSwatchRGB');
const tipSwatchCMYK = document.getElementById('tipSwatchCMYK');
const tipBody = document.getElementById('tipBody');
// ── Application state ─────────────────────────────────────────────────
//
// src — the source ICCImage (sRGB, loaded from the selected URL)
// sep — the CMYK separation ICCImage (from src.toSeparation)
// proof — the soft-proof ICCImage (from src.toProof — sRGB-tagged)
//
// All three are ICCImage instances. They share the same TransformCache,
// so switching intent or profile only rebuilds the LUTs that changed.
let src = null;
let sep = null;
let proof = null;
const profileCache = new Map();
// ── Intent + BPC parsing ──────────────────────────────────────────────
//
// The dropdown value encodes both the rendering intent and whether
// black-point compensation (BPC) is enabled, e.g. 'relative-bpc'.
function parseIntent() {
const v = intentSel.value;
const bpc = v.endsWith('-bpc');
const base = v.replace('-bpc', '');
const intent = eIntent[base] ?? eIntent.perceptual;
return { intent, bpc };
}
// ── Helpers ───────────────────────────────────────────────────────────
async function loadProfile(name) {
if (profileCache.has(name)) return { profile: profileCache.get(name), ms: 0 };
const t0 = performance.now();
const p = new Profile();
await p.loadPromise(name);
const ms = performance.now() - t0;
profileCache.set(name, p);
return { profile: p, ms };
}
function dumpInfo(host, img) {
const i = img.info;
host.innerHTML = `
<div class="info-cell"><span class="lbl">Size</span><span class="val">${i.width}\u00d7${i.height}</span></div>
<div class="info-cell"><span class="lbl">Colour space</span><span class="val">${i.colorSpace} ${i.bitDepth}-bit</span></div>
<div class="info-cell"><span class="lbl">Profile</span><span class="val">${i.profile}</span></div>
<div class="info-cell"><span class="lbl">Chain</span><span class="val">${i.chain}</span></div>
`;
}
function fmtMs(ms) { return ms < 1 ? '<1 ms' : ms.toFixed(0) + ' ms'; }
function fmtMpx(pixels, ms) { return ((pixels / (ms / 1000)) / 1e6).toFixed(1); }
function $(id) { return document.getElementById(id); }
async function loadImage(url) {
return new Promise((resolve, reject) => {
imgEl.onload = () => resolve(imgEl);
imgEl.onerror = () => reject(new Error('Failed to load image: ' + url));
imgEl.src = url;
});
}
// ── Run pass ──────────────────────────────────────────────────────────
//
// A single "pass" does three things:
// 1. Soft-proof — src.toProof() → sRGB image simulating print
// 2. Separation — src.toSeparation() → CMYK image with real ink data
// 3. Render — blit both to canvas + render C/M/Y/K plate previews
//
// This is called twice per build(): once "cold" (LUT build + transform)
// and once "warm" (cached LUT, transform only) to show the performance
// difference.
function parseGamut() {
const v = gamutSel.value;
if (v === 'none') return {};
if (v === 'map') return { lutGamutMode: 'map' };
if (v === 'colorMap') return { lutGamutMode: 'colorMap' };
const limit = parseInt(v.split('-')[1], 10);
return { lutGamutMode: 'color', lutGamutLimit: limit };
}
async function runPass(proofProfile) {
const { intent, bpc } = parseIntent();
const gamutOpts = parseGamut();
const t = {};
// SOFT-PROOF: src (sRGB) → CMYK (press profile) → sRGB
//
// toProof() builds a multi-stage Transform internally:
// [srcProfile, intent, proofProfile, eIntent.relative, *sRGB]
//
// On first call this bakes the 3D CLUT (~50–100 ms). On subsequent
// calls with the same profile/intent the cached Transform is reused
// and only the pixel transform runs (~5–15 ms for 1 MP).
//
// The returned ICCImage is sRGB-tagged — its pixels represent what
// the image would look like after printing and viewing on screen.
let t0 = performance.now();
const localProof = await src.toProof(proofProfile, {
intent,
BPC: bpc,
...gamutOpts,
});
t.proofMs = performance.now() - t0;
// SEPARATION: src (sRGB) → CMYK (press profile)
//
// toSeparation() builds a [srcProfile, intent, proofProfile]
// Transform. The returned ICCImage's raw data IS the CMYK ink
// values — each pixel has 4 channels (C, M, Y, K) in 0–255 range.
//
// We can then call renderChannelAs('C') etc. to visualise
// individual ink plates.
t0 = performance.now();
const localSep = await src.toSeparation(proofProfile, {
intent, BPC: bpc,
});
t.sepMs = performance.now() - t0;
// RENDER: blit source, proof, and channel plates to canvases
t0 = performance.now();
// toCanvas() auto-detects that src is already sRGB uint8 → blits
// directly without any colour transform.
await src.toCanvas(screenCv);
// localProof is also sRGB uint8 (toProof returns sRGB-tagged) →
// direct blit, no extra conversion.
await localProof.toCanvas(proofCv);
// img.info returns metadata including the chain history, which
// shows the full conversion path (e.g. "sRGB → Perceptual →
// GRACoL 2006 → Relative → sRGB").
dumpInfo(screenInfo, src);
dumpInfo(proofInfo, localProof);
// CHANNEL PLATES: render each CMYK ink as a tinted preview.
//
// ChannelSets.CMYK defines the four channels:
// [{ index: 0, name: 'cyan', short: 'C', tint: [0,174,239] },
// { index: 1, name: 'magenta', short: 'M', tint: [236,0,140] },
// ...]
//
// renderChannelAs('C') extracts the Cyan channel from the CMYK
// separation and produces an sRGB RGBA ICCImage where the channel
// value drives the alpha and the tint colour fills RGB. This makes
// each plate visually meaningful (cyan ink shown as cyan, etc.).
channelHost.innerHTML = '';
for (const def of ChannelSets.CMYK) {
const cv = document.createElement('canvas');
cv.className = 'hoverable';
const tinted = localSep.renderChannelAs(def.short);
await tinted.toCanvas(cv);
const wrapper = document.createElement('div');
const swatch = `<span class="swatch" style="background:rgb(${def.tint.join(',')})"></span>`;
wrapper.innerHTML = `<h3>${swatch}${def.name} (${def.short})</h3>`;
wrapper.appendChild(cv);
channelHost.appendChild(wrapper);
}
t.renderMs = performance.now() - t0;
t.totalMs = t.proofMs + t.sepMs + t.renderMs;
return { t, proof: localProof, sep: localSep };
}
// ── Build ─────────────────────────────────────────────────────────────
//
// The main workflow:
// 1. Load the source image → ICCImage.fromHTMLImage()
// 2. Load the CMYK profile → Profile.loadPromise()
// 3. Cold run — first time: LUT build + transform (measures total cost)
// 4. Warm run — second time: cached LUT, transform only (measures throughput)
//
// The cold/warm split demonstrates the TransformCache in action: the
// first call pays the LUT build cost, the second call reuses it.
async function build() {
runBtn.disabled = true;
runBtn.textContent = 'Working\u2026';
timingInline.textContent = '';
timingInline.className = 'timing-inline is-busy';
try {
// Step 1: Create ICCImage from browser <img>.
// fromHTMLImage() draws the image onto a 2D canvas to extract
// RGBA pixels, then wraps them in an ICCImage tagged with *sRGB.
// maxPixels caps the resolution via bilinear downscale to keep
// the demo responsive.
if (!src) {
await loadImage(imageSel.value);
src = await ICCImage.fromHTMLImage(imgEl, { maxPixels: 4_000_000 });
}
const pixels = src.info.width * src.info.height;
$('t-pixels').textContent = (pixels / 1e6).toFixed(2) + ' MP';
// Step 2: Load the CMYK press profile (.icc file via HTTP).
// Cached after first load so switching intent doesn't reload.
const { profile: proofProfile, ms: profileMs } = await loadProfile(profileSel.value);
$('t-profile').textContent = profileMs > 0 ? fmtMs(profileMs) : 'cached';
// Step 3: Cold run — includes LUT build time.
const cold = await runPass(proofProfile);
sep = cold.sep;
proof = cold.proof;
$('tc-proof').textContent = fmtMs(cold.t.proofMs);
$('tc-sep').textContent = fmtMs(cold.t.sepMs);
$('tc-render').textContent = fmtMs(cold.t.renderMs);
$('tc-total').textContent = fmtMs(cold.t.totalMs);
$('t-cache').textContent = src.info.cacheSize + ' transforms';
// Wait two animation frames to let the browser paint the cold
// results before starting the warm run.
await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
// Step 4: Warm run — LUT is cached, only the pixel transform runs.
// This represents the steady-state performance you'd see in a
// real application.
const warm = await runPass(proofProfile);
sep = warm.sep;
proof = warm.proof;
$('tw-proof').textContent = fmtMs(warm.t.proofMs);
$('tw-proof-mpx').textContent = fmtMpx(pixels, warm.t.proofMs) + ' MPx/s';
$('tw-sep').textContent = fmtMs(warm.t.sepMs);
$('tw-sep-mpx').textContent = fmtMpx(pixels, warm.t.sepMs) + ' MPx/s';
$('tw-render').textContent = fmtMs(warm.t.renderMs);
$('tw-total').textContent = fmtMs(warm.t.totalMs);
const avgMs = ((warm.t.proofMs + warm.t.sepMs) / 2);
const avgMpx = fmtMpx(pixels, avgMs);
timingInline.textContent = `${fmtMs(warm.t.proofMs + warm.t.sepMs)} \u00b7 ~${avgMpx} MPx/s avg`;
timingInline.className = 'timing-inline';
} catch (e) {
console.error(e);
$('tc-proof').textContent = 'error: ' + e.message;
timingInline.textContent = 'error';
timingInline.className = 'timing-inline';
} finally {
runBtn.disabled = false;
runBtn.textContent = 'Run soft proof';
}
}
// ── Colour picker (floating tooltip) ──────────────────────────────────
//
// Hovering any canvas shows a floating tooltip with per-pixel colour
// data. This demonstrates ICCImage.pixel(x, y) — the accuracy-path
// single-pixel readout that returns { lab, srgb, device }.
//
// The tooltip shows:
// - Source sRGB values + hex
// - CIE L*a*b* (D50, relative colorimetric)
// - CMYK ink percentages (from the separation ICCImage's raw buffer)
// - CMYK round-trip Lab (from sep.pixel() — re-converts CMYK→Lab)
// - deltaE 2000 and deltaE 76 between source Lab and CMYK round-trip Lab
// - Split swatch: source RGB (left) vs soft-proof RGB (right)
function canvasToImageXY(canvas, e) {
const rect = canvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) * (canvas.width / rect.width));
const y = Math.floor((e.clientY - rect.top) * (canvas.height / rect.height));
return { x, y };
}
// Read CMYK ink values directly from the separation's raw buffer.
// No colour transform needed — the data IS the separation.
function readCMYK(x, y) {
if (!sep || !sep.raw) return null;
const r = sep.raw;
if (x < 0 || x >= r.width || y < 0 || y >= r.height) return null;
const stride = r.bytesPerPixel;
const idx = (y * r.width + x) * stride;
const d = r.data;
const norm = r.bitDepth === 8 ? 1 / 255 : r.bitDepth === 16 ? 1 / 65535 : 1;
return {
C: d[idx] * norm * 100,
M: d[idx + 1] * norm * 100,
Y: d[idx + 2] * norm * 100,
K: d[idx + 3] * norm * 100,
};
}
// Read the soft-proof RGB value for the swatch display.
function proofRGB(x, y) {
if (!proof || !proof.raw) return null;
const r = proof.raw;
if (x < 0 || x >= r.width || y < 0 || y >= r.height) return null;
const stride = r.bytesPerPixel;
const idx = (y * r.width + x) * stride;
const d = r.data;
return `rgb(${d[idx]},${d[idx + 1]},${d[idx + 2]})`;
}
function deClass(de) {
if (de < 1) return 'de-ok';
if (de < 3) return 'de-warn';
return 'de-bad';
}
function showTooltip(e, x, y) {
if (!src || !src.raw) return;
const r = src.raw;
if (x < 0 || x >= r.width || y < 0 || y >= r.height) { hideTooltip(); return; }
// pixel() uses the accuracy path (no LUT) — it builds two
// dataFormat:'object' Transforms on first call (pixelToLab and
// pixelToSRGB), then reuses them. Returns lab, srgb, and device
// values for the pixel.
const px = src.pixel(x, y);
const cmyk = readCMYK(x, y);
// For deltaE: convert the CMYK separation's ink values back to Lab
// via sep.pixel(), then compare against the source pixel's Lab.
let cmykLab = null;
let dE = null;
if (sep && sep.raw && x < sep.raw.width && y < sep.raw.height) {
cmykLab = sep.pixel(x, y).lab;
dE = convert.deltaE2000(px.lab, cmykLab);
}
const { R, G, B, hex } = px.srgb;
const { L, a, b } = px.lab;
tipSwatchRGB.style.background = hex;
tipSwatchCMYK.style.background = proofRGB(x, y) || hex;
let rows = `
<span class="tip-lbl">XY</span> <span class="tip-val">${x}, ${y}</span>
<span class="tip-lbl">sRGB</span> <span class="tip-val">${R} ${G} ${B} ${hex}</span>
<span class="tip-lbl">Lab</span> <span class="tip-val">${L.toFixed(1)} ${a.toFixed(1)} ${b.toFixed(1)}</span>`;
if (cmyk) {
rows += `
<div class="tip-sep"></div>
<span class="tip-lbl">CMYK</span> <span class="tip-val">${cmyk.C.toFixed(1)} ${cmyk.M.toFixed(1)} ${cmyk.Y.toFixed(1)} ${cmyk.K.toFixed(1)}</span>`;
}
if (cmykLab) {
rows += `
<span class="tip-lbl">CMYK Lab</span> <span class="tip-val">${cmykLab.L.toFixed(1)} ${cmykLab.a.toFixed(1)} ${cmykLab.b.toFixed(1)}</span>`;
}
if (dE !== null) {
const dE76 = convert.deltaE1976(px.lab, cmykLab);
rows += `
<span class="tip-lbl">ΔE 00</span> <span class="tip-val ${deClass(dE)}">${dE.toFixed(2)}</span>
<span class="tip-lbl">ΔE 76</span> <span class="tip-val ${deClass(dE76)}">${dE76.toFixed(2)}</span>`;
}
tipBody.innerHTML = rows;
const pad = 16;
let tx = e.clientX + pad;
let ty = e.clientY + pad;
const tw = tooltip.offsetWidth || 220;
const th = tooltip.offsetHeight || 160;
if (tx + tw > window.innerWidth - 8) tx = e.clientX - tw - pad;
if (ty + th > window.innerHeight - 8) ty = e.clientY - th - pad;
tooltip.style.left = tx + 'px';
tooltip.style.top = ty + 'px';
tooltip.classList.add('is-visible');
}
function hideTooltip() {
tooltip.classList.remove('is-visible');
}
document.addEventListener('mousemove', (e) => {
const cv = e.target.closest('canvas.hoverable');
if (!cv || !src) { hideTooltip(); return; }
const { x, y } = canvasToImageXY(cv, e);
showTooltip(e, x, y);
});
// ── Event wiring ──────────────────────────────────────────────────────
//
// Changing the profile, intent, or gamut mode triggers a full rebuild.
// Changing the image clears the cached source and derivatives first,
// since the new image needs a fresh ICCImage.fromHTMLImage() call.
runBtn.addEventListener('click', build);
profileSel.addEventListener('change', build);
intentSel.addEventListener('change', build);
gamutSel.addEventListener('change', build);
imageSel.addEventListener('change', () => { src = null; sep = null; proof = null; build(); });
build();
</script>
</body>
</html>