Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / color-mixing.js

1WE.mount('color-mixing', function (S) {
2 var C = S.C, ctx = S.ctx, P = [[.4, .4], [.6, .4], [.5, .64]], grab = -1;
3 var mode = S.select('Mix', [['light', 'Light (screens)'], ['paint', 'Ink (printing)']], 'light');
4 S.pointer({ down: function (x, y) { grab = -1; var bd = 1e9; P.forEach(function (p, i) { var d = Math.hypot(p[0] * S.W - x, p[1] * S.H - y); if (d < bd) { bd = d; grab = i; } }); }, move: function (x, y, d) { if (d && grab >= 0) { P[grab][0] = Math.max(.05, Math.min(.95, x / S.W)); P[grab][1] = Math.max(.08, Math.min(.92, y / S.H)); } }, up: function () { grab = -1; } });
5 S.loop(function () {
6 var L = mode.v === 'light', w = S.W, h = S.H, r = Math.min(w, h) * .27, cols = L ? ['#ff0000', '#00ff00', '#0000ff'] : ['#00ffff', '#ff00ff', '#ffff00'], names = L ? ['Red', 'Green', 'Blue'] : ['Cyan', 'Magenta', 'Yellow'];
7 S.clear(L ? '#000' : '#fbfaf5'); ctx.globalCompositeOperation = L ? 'lighter' : 'multiply';
8 P.forEach(function (p, i) { S.dot(p[0] * w, p[1] * h, r, cols[i]); }); ctx.globalCompositeOperation = 'source-over';
9 P.forEach(function (p, i) { S.txt(names[i], p[0] * w + (p[0] - .5) * r * 1.6, p[1] * h + (p[1] - .48) * r * 2.2, L ? '#fff' : '#111', 13, 'center', 700); });
10 var px = S.ptr.x > 0 ? S.ptr.x : w / 2, py = S.ptr.x > 0 ? S.ptr.y : h * .47, inn = P.map(function (p) { return Math.hypot(p[0] * w - px, p[1] * h - py) < r; }), key = inn.map(Number).join('');
11 var NM = L ? { '000': 'black: no light at all', '100': 'red', '010': 'green', '001': 'blue', '110': 'yellow (red + green)', '101': 'magenta (red + blue)', '011': 'cyan (green + blue)', '111': 'white: all three together' } : { '000': 'white paper', '100': 'cyan', '010': 'magenta', '001': 'yellow', '110': 'blue (cyan + magenta)', '101': 'green (cyan + yellow)', '011': 'red (magenta + yellow)', '111': 'black: all light soaked up' };
12 S.ring(px, py, 9, L ? '#fff' : '#111', 2);
13 S.say((L ? 'Screens start dark and <b>add</b> light. ' : 'Ink starts with white paper and <b>takes light away</b>. ') + 'Under the ring: <b>' + NM[key] + '</b>. Drag the circles apart and together.');
14 });
15});