summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/reftest/colors/color_canvas.html
blob: 7abbc8625528464ae525e9b639ad8a1108b28bfc (plain)
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
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8>
    <title>color_canvas.html</title>
  </head>
  <!--
# color_canvas.html

* Default is a 100x100 'css' canvas-equivalent div, filled with 50% srgb red.

* We default to showing the settings pane when loaded without a query string.
  This way, someone naively opens this in a browser, they can immediately see
  all available options.

* The 'Publish' button updates the url, and so causes the settings pane to
  hide.

* Clicking on the canvas toggles the settings pane for further editing.
  -->
  <body>
    <form id=e_settings><fieldset><legend>Settings</legend>
      Width: <input id=e_width type=text value=100>
      <br>
      Height: <input id=e_height type=text value=100>
      <br>
      <fieldset><legend>Canvas Context</legend>
        Type: <select id=e_context>
          <option value=css selected>css</option>
          <option value=2d>2d</option>
          <option value=webgl>webgl</option>
        </select>
        <br>
        Options: <input id=e_options type=text value={}>
        <br>
        Colorspace: <input id=e_cspace type=text placeholder=srgb>
        <br>
        WebGL Format: <input id=e_webgl_format type=text placeholder=RGBA8>
      </fieldset>
      <br>
      Color: <input id=e_color type=text value='color(srgb 0 0.5 0)'>
      <br>
      <input id=e_publish type=button value=Publish>
      <input type=checkbox id=e_publish_omit_defaults checked><label for=e_publish_omit_defaults>Omit defaults</label>
      <hr>
    </fieldset></form>
    <div id=e_canvas_list><canvas></canvas></div>
    <script>
'use strict';

// -

function walk_nodes_depth_first(e, fn) {
  if (fn(e) === false) return; // Don't stop on `true`, or `undefined`!
  for (const c of e.childNodes) {
    walk_nodes_depth_first(c, fn);
  }
}

// -

// Click the canvas to toggle the settings pane.
e_canvas_list.addEventListener('click', () => {
  // Toggle display:none to hide/unhide.
  e_settings.hidden = !e_settings.hidden;
});

// Hide settings initially if there's a query string in the url.
if (window.location.search.startsWith('?')) {
  e_settings.hidden = true;
}

// -

// Imply .name from .id, because `new FormData` collects based on names.
walk_nodes_depth_first(e_settings, e => {
  if (e.id) {
    e.name = e.id;
    e._default_value = e.value;
  }
});

// -

const URL_PARAMS = new URLSearchParams(window.location.search);
URL_PARAMS.forEach((v,k) => {
  const e = window[k];
  if (!e) {
    if (k) {
      console.warn(`Unrecognized setting: ${k} = ${v}`);
    }
    return;
  }
  v = decode_url_v(k,v);
  e.value = v;
});

// -

globalThis.ASSERT = (() => {
  function toPrettyString(arg) {
    if (!arg) return ''+arg;

    if (arg.call) {
      let s = arg.toString();
      const RE_TRIVIAL_LAMBDA = /\( *\) *=> *(.*)/;
      const m = RE_TRIVIAL_LAMBDA.exec(s);
      if (m) {
        s = '`' + m[1] + '`';
      }
      return s;
    }
    if (arg.constructor == Array) {
      return `[${[].join.call(arg, ', ')}]`;
    }
    return JSON.stringify(arg);
  }

  /// new AssertArg(): Construct a wrapper for args to assert functions.
  function AssertArg(dict) {
    this.label = Object.keys(dict)[0];

    this.set = function(arg) {
      this.arg = arg;
      this.value = (arg && arg.call) ? arg.call() : arg;
      this.value = toPrettyString(this.value);
    };
    this.set(dict[this.label]);

    this.toString = function() {
      let ret = `${this.label} ${toPrettyString(this.arg)}`;
      if (this.arg.call) {
        ret += ` (${this.value})`;
      }
      return ret;
    }
  }

  const eq = (a,b) => a == b;
  const neq = (a,b) => a != b;

  const CMP_BY_NAME = {
    '==': eq,
    '!=': neq,
  };

  function IS(cmp, was, expected, _console) {
    _console = _console || console;

    _console.assert(was.call, '`was.call` not defined.');
    was = new AssertArg({was});
    expected = new AssertArg({expected});

    const fn_cmp = CMP_BY_NAME[cmp] || cmp;

    _console.assert(fn_cmp(was.value, expected.value), `${toPrettyString(was.arg)} => ${was.value} not ${cmp} ${expected}`);
    if (was.value != expected.value) {
    } else if (globalThis.ASSERT && globalThis.ASSERT.verbose) {
      const maybe_cmp_str = (cmp == '==') ? '' : ` ${was.value} ${cmp}`;
      _console.log(`${toPrettyString(was.arg)} => ${maybe_cmp_str}${expected}`);
    }
  }

  // -

  const MOCK_CONSOLE = {
    _asserts: [],
    assert: function(expr, ...args) {
      if (!expr) {
        this._asserts.push(args);
      }
    },
    log: function(...args) {
      // Don't record.
    },
  };

  // -
  // Test `==`

  IS('==', () => 1, 1, MOCK_CONSOLE);
  console.assert(MOCK_CONSOLE._asserts.length == 0, MOCK_CONSOLE._asserts);
  MOCK_CONSOLE._asserts = [];

  IS('==', () => 2, 2, MOCK_CONSOLE);
  console.assert(MOCK_CONSOLE._asserts.length == 0, MOCK_CONSOLE._asserts);
  MOCK_CONSOLE._asserts = [];

  IS('==', () => 5, () => 3, MOCK_CONSOLE);
  console.assert(MOCK_CONSOLE._asserts.length == 1, MOCK_CONSOLE._asserts);
  MOCK_CONSOLE._asserts = [];

  IS('==', () => [1,2], () => [1,2], MOCK_CONSOLE);
  console.assert(MOCK_CONSOLE._asserts.length == 0, MOCK_CONSOLE._asserts);
  MOCK_CONSOLE._asserts = [];

  // -
  // Test `!=`

  IS('!=', () => [1,2,5], () => [1,2,3], MOCK_CONSOLE);
  console.assert(MOCK_CONSOLE._asserts.length == 0, MOCK_CONSOLE._asserts);
  MOCK_CONSOLE._asserts = [];

  // -

  const ret = {
    verbose: false,
    IS,
  };
  ret.EQ = (was,expected) => ret.IS('==', was, expected);
  ret.NEQ = (was,expected) => ret.IS('!=', was, expected);
  ret.EEQ = (was,expected) => ret.IS('===', was, expected);
  ret.NEEQ = (was,expected) => ret.IS('!==', was, expected);
  ret.TRUE = was => ret.EQ(was, true);
  ret.FALSE = was => ret.EQ(was, false);
  ret.NULL = was => ret.EEQ(was, null);
  return ret;
})();

// -

function parse_css_rgb(str) {
  //          rgb  (R      ,G      ,B             /A         )
  const m = /rgba?\(([^,]+),([^,]+),([^/)]+)(?: *\/([^)]+))?\)/.exec(str);
  if (!m) throw str;
  const rgba = m.slice(1,1+4).map((s,i) => {
    if (s === undefined && i == 3) {
      s = '1'; // Alpha defaults to 1.
    }
    s = s.trim();
    let v = parseFloat(s);
    if (s.endsWith('%')) {
      v /= 100;
    } else {
      if (i < 3) { // r,g,b but not a!
        v /= 255;
      }
    }
    return v;
  });
  return rgba;
}
ASSERT.EQ(() => parse_css_rgb('rgb(255,255,255)'), [1,1,1,1]);
ASSERT.EQ(() => parse_css_rgb('rgba(255,255,255)'), [1,1,1,1]);
ASSERT.EQ(() => parse_css_rgb('rgb(255,255,255)'), [1,1,1,1]);
ASSERT.EQ(() => parse_css_rgb('rgba(255,255,255)'), [1,1,1,1]);
ASSERT.EQ(() => parse_css_rgb('rgb(20,40,60)'), () => [20/255, 40/255, 60/255, 1]);
ASSERT.EQ(() => parse_css_rgb('rgb(20,40,60 / 0.5)'), () => [20/255, 40/255, 60/255, 0.5]);
ASSERT.EQ(() => parse_css_rgb('rgb(20,40,60 / 0)'), () => [20/255, 40/255, 60/255, 0]);

// -

function parse_css_color(str) {
  //         color (  srgb     R        G        B           /A         )
  const m = /color\( *([^ ]+) +([^ ]+) +([^ ]+) +([^/)]+)(?:\/([^)]+))?\)/.exec(str);
  if (!m) {
    return ['srgb', ...parse_css_rgb(str)];
  }

  const cspace = m[1].trim();
  let has_extreme_colors = false;
  const rgba = m.slice(2, 2+4).map((s,i) => {
    if (s === undefined && i == 3) {
      s = '1'; // Alpha defaults to 1.
    }
    s = s.trim();
    let v = parseFloat(s);
    if (s.endsWith('%')) {
      v /= 100;
    }
    if (v < 0 || v > 1) {
      has_extreme_colors = true;
    }
    return v;
  });
  if (has_extreme_colors) {
    console.warn(`parse_css_color('${str}') has colors outside [0.0,1.0]: ${JSON.stringify(rgba)}`);
  }
  return [cspace, ...rgba];
}
ASSERT.EQ(() => parse_css_color('rgb(255,255,255)'), ['srgb',1,1,1,1]);
ASSERT.EQ(() => parse_css_color('rgb(20,40,60 / 0.5)'), () => ['srgb', 20/255, 40/255, 60/255, 0.5]);
ASSERT.EQ(() => parse_css_color('color(srgb 1 0 1 /0.3)'), ['srgb',1,0,1,0.3]);
ASSERT.EQ(() => parse_css_color('color(display-p3 1 0% 100%/ 30%)'), ['display-p3',1,0,1,0.3]);

// -

class CssColor {
  constructor(cspace, r,g,b,a=1) {
    this.cspace = cspace;
    this.rgba = [this.r, this.g, this.b, this.a] = [r,g,b,a];
    this.rgb = this.rgba.slice(0,3);
    this.tuple = [this.cspace, ...this.rgba];
  }

  toString() {
    return `color(${this.cspace} ${this.rgb.join(' ')} / ${this.a})`;
  }
};
CssColor.parse = function(str) {
  return new CssColor(...parse_css_color(str));
}

{
  let STR;
  // Test round-trip.
  STR = 'color(display-p3 1 0 1 / 0.3)';
  ASSERT.EQ(() => CssColor.parse(STR).toString(), STR);

  // Test round-trip normalization
  ASSERT.EQ(() => CssColor.parse('color( display-p3  1   0  1/30% )').toString(), 'color(display-p3 1 0 1 / 0.3)');
}

// -

function redraw() {
  while (e_canvas_list.firstChild) {
    e_canvas_list.removeChild(e_canvas_list.firstChild);
  }

  const c = make_canvas(e_color.value.trim());
  c.style.border = '4px solid black';
  e_canvas_list.appendChild(c);
}

function fill_canvas_rect(context /*: CanvasRenderingContext | WebGLRenderingContext*/, css_color, rect=null) {
  rect = rect || {left: 0, top: 0, w: context.canvas.width, h: context.canvas.height};

  const is_c2d = ('fillRect' in context);
  if (is_c2d) {
    const c2d = context;
    c2d.fillStyle = css_color;
    c2d.fillRect(rect.left, rect.top, rect.w, rect.h);
    return;
  }

  const is_webgl = ('drawArrays' in context);
  if (is_webgl) {
    const gl = context;
    console.assert(context.canvas.width == gl.drawingBufferWidth, context.canvas.width, '!=', gl.drawingBufferWidth);
    console.assert(context.canvas.height == gl.drawingBufferHeight, context.canvas.height, '!=', gl.drawingBufferHeight);

    gl.enable(gl.SCISSOR_TEST);
    gl.disable(gl.DEPTH_TEST);
    const bottom = rect.top + rect.h; // in y-down c2d coords
    gl.scissor(rect.left, context.canvas.height - bottom, rect.w, rect.h);

    const canvas_cspace = context.drawingBufferColorSpace || 'srgb';
    if (css_color.cspace != canvas_cspace) {
      console.warn(`Ignoring mismatched color vs webgl canvas cspace: ${css_color.cspace} vs ${canvas_cspace}`);
    }
    gl.clearColor(...css_color.rgba);
    gl.clear(gl.COLOR_BUFFER_BIT);
    return;
  }

  console.error('Unhandled context kind:', context);
}

window.e_canvas = null;

function make_canvas(css_color) {
  css_color = CssColor.parse(css_color);

  // `e_width` and e_friends are elements (by id) that we added to the raw HTML above.
  // `e_width` is an old shorthand for `window.e_width || document.getElementById('e_width')`.
  const W = parseInt(e_width.value);
  const H = parseInt(e_height.value);
  if (e_context.value == 'css') {
    e_canvas = document.createElement('div');
    e_canvas.style.width = `${W}px`;
    e_canvas.style.height = `${H}px`;
    e_canvas.style.backgroundColor = css_color;
    return e_canvas;
  }
  e_canvas = document.createElement('canvas');
  e_canvas.width = W;
  e_canvas.height = H;

  let requested_options = JSON.parse(e_options.value);
  requested_options.colorSpace = e_cspace.value || undefined;

  const context = e_canvas.getContext(e_context.value, requested_options);
  if (requested_options.colorSpace) {
    if (!context.drawingBufferColorSpace) {
      console.warn(`${context.constructor.name}.drawingBufferColorSpace not supported by browser.`);
    } else {
      context.drawingBufferColorSpace = requested_options.colorSpace;
    }
  }

  if (e_webgl_format.value) {
    if (!context.drawingBufferStorage) {
      console.warn(`${context.constructor.name}.drawingBufferStorage not supported by browser.`);
    } else {
      context.drawingBufferStorage(W, H, context[e_webgl_format.value]);
    }
  }

  let actual_options;
  if (!context.getContextAttributes) {
    console.warn(`${canvas.constructor.name}.getContextAttributes not supported by browser.`);
    actual_options = requested_options;
  } else {
    actual_options = context.getContextAttributes();
  }

  // -

  fill_canvas_rect(context, css_color);

  return e_canvas;
}

e_settings.addEventListener('change', async () => {
  redraw();
  const e_updated = document.createElement('i');
  e_updated.textContent = '(Updated!)';
  document.body.appendChild(e_updated);
  await new Promise(go => setTimeout(go, 1000));
  document.body.removeChild(e_updated);
});
redraw();

// -

function encode_url_v(k,v) {
  if (k == 'e_color') {
    v = v.replaceAll(' ', ',');
  }
  console.assert(!v.includes(' '), v);
  return v
}
function decode_url_v(k,v) {
  console.assert(!v.includes(' '), v);
  if (k == 'e_color') {
    v = v.replaceAll(',', ' ');
  }
  return v
}
ASSERT.EQ(() => decode_url_v('e_color', encode_url_v('e_color', 'color(srgb 1 0 0)')), 'color(srgb 1 0 0)')

e_publish.addEventListener('click', () => {
  const fd = new FormData(e_settings);
  let settings = [];
  for (let [k,v] of fd) {
    const e = window[k];
    if (e_publish_omit_defaults.checked && v == e._default_value) continue;

    v = encode_url_v(k,v);
    settings.push(`${k}=${v}`);
  }
  settings = settings.join('&');
  if (!settings) {
    settings = '='; // Empty key-value pair is 'publish with default settings'
  }
  window.location.search = '?' + settings;
});
    </script>
  </body>
</html>