-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
552 lines (551 loc) · 17.6 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<title>Munsell Color Palette</title>
<script type="text/javascript" src="munsell-floats.js"></script>
<script type="text/javascript" src="munsell-interpolate.js"></script>
<script type="text/javascript" src="glue5.js"></script>
<script type="text/javascript" src="color.js"></script>
<script type="text/javascript">
<!--
function srgb_coords(triple) {
var ans = [0, 0, 0];
for (var i in ans) {
ans[i] = Math.round(triple[i] * 255);
}
return ans;
}
function colorname(triple) {
return Color.cssrgb(srgb_coords(triple));
}
function color_charted(triple) {
for (var i in triple) {
if (isNaN(triple[i])) { return false; }
}
return true;
}
function color_valid(triple) {
var srgb = srgb_coords(triple);
for (var i in srgb) {
if (srgb[i] < 0 || srgb[i] > 255) { return false; }
}
return true;
}
function chroma_map(k) {
return k / 26;
}
function lightness_map(j) {
if (j < 5) { return j * 0.02; }
else { return (j - 4) / 10; }
}
function munsell_name(i, j, k) {
var hue = ((i % 4 + 1) * 2.5).toFixed(1) +
['R', 'YR', 'Y', 'GY', 'G', 'BG', 'B', 'PB', 'P', 'RP'][
Math.floor(i/4)
];
var lightness = (lightness_map(j) * 10).toFixed(2);
var chroma = (k * 2).toFixed(1);
return hue + ' ' + lightness + '/' + chroma;
}
function hex_from_name(name) {
var r = /([0-9.]+)[\s]*([A-Z]{1,2})[\s]*([0-9.]+)\/[\s]*([0-9.]+)/;
var hues = ['R', 'YR', 'Y', 'GY', 'G', 'BG', 'B', 'PB', 'P', 'RP']
var result = r.exec(name);
var hue = parseFloat(result[1]);
if (hues.indexOf(result[2]) != -1) {
hue += 10 * hues.indexOf(result[2]);
}
var value = parseFloat(result[3]);
var chroma = parseFloat(result[4]);
var index = munsell_to_index(hue, value, chroma);
var rgb = Munsell.interpolate(index[0], index[1], index[2]);
var ans = '';
for (var i in rgb) {
rgb[i] = Math.max(0, Math.min(Math.round(rgb[i] * 255), 255));
rgb[i] = rgb[i].toString(16);
while (rgb[i].length < 2) {
rgb[i] = '0' + rgb[i];
}
ans += rgb[i];
}
return ans;
}
function drawWedge(ctx, r1, r2, angle1, angle2) {
ctx.beginPath();
ctx.moveTo(r1 * Math.cos(angle2), r1 * Math.sin(angle2));
ctx.arc(0, 0, r1, angle2, angle1, true);
ctx.lineTo(r2 * Math.cos(angle1), r2 * Math.sin(angle1));
ctx.arc(0, 0, r2, angle1, angle2, false);
ctx.closePath();
}
function drawCrossHairs(ctx, x, y, r1, r2) {
ctx.beginPath();
ctx.moveTo(x - r1, y);
ctx.lineTo(x - r2, y);
ctx.moveTo(x + r1, y);
ctx.lineTo(x + r2, y);
ctx.moveTo(x, y - r1);
ctx.lineTo(x, y - r2);
ctx.moveTo(x, y + r1);
ctx.lineTo(x, y + r2);
}
function drawPolarCrossHairs(ctx, r, angle, r1, r2) {
return drawCrossHairs(ctx, r * Math.cos(angle), r * Math.sin(angle), r1, r2);
}
function draw_selections(ctx, radius, discrete) {
var angle = Math.PI / 20;
var i = window.current_color[0];
var j = window.current_color[1];
var k = window.current_color[2];
// Draw HC
ctx.save();
ctx.translate(3*radius, radius);
ctx.scale(radius, -radius);
ctx.lineWidth = 0.0003 * (k+10);
ctx.strokeStyle = 'rgb(255,255,255)';
if (discrete) {
drawWedge(ctx, chroma_map(k), chroma_map(k+1), i*angle, (i+1)*angle);
} else {
drawPolarCrossHairs(ctx, chroma_map(k), i*angle, 0.01, 0.05);
}
ctx.stroke();
ctx.restore();
// Draw HL
ctx.save();
ctx.translate(5*radius, radius);
ctx.scale(radius, -radius);
ctx.lineWidth = 0.01 * (0.1 + lightness_map(j));
ctx.strokeStyle = 'rgb(255,255,255)';
if (discrete) {
drawWedge(ctx, lightness_map(j-1), lightness_map(j),
i*angle, (i+1)*angle);
} else {
drawPolarCrossHairs(ctx, lightness_map(j), i*angle, 0.01, 0.05);
}
ctx.stroke();
ctx.restore();
// Draw LC
ctx.save();
ctx.translate(0, 2*radius);
ctx.scale(2*radius, -2*radius);
ctx.lineWidth = 0.005;
ctx.strokeStyle = 'rgb(255,255,255)';
if (discrete) {
ctx.strokeRect(chroma_map(k), lightness_map(j-1),
chroma_map(k+1) - chroma_map(k),
lightness_map(j) - lightness_map(j-1));
} else {
drawCrossHairs(ctx, chroma_map(k), lightness_map(j), 0.005, 0.025);
ctx.stroke();
}
ctx.restore();
}
function draw_hc(ctx, radius, j) {
var angle = Math.PI / 20;
ctx.save();
ctx.translate(3*radius, radius);
ctx.scale(radius, -radius);
for (var i = 0; i < 40; i++) {
for (var k = 0; k < 26; k++) {
var color = Munsell.interpolate(i, j, k);
if (!color_charted(color)) { color = [0,0,0]; }
if (!(color_valid(color) || window.options.oog)) { continue; }
ctx.fillStyle = colorname(color);
drawWedge(ctx, chroma_map(k), chroma_map(k+1), i*angle, (i+1)*angle);
ctx.fill();
}
}
ctx.restore();
}
function draw_hl(ctx, radius, k) {
var angle = Math.PI / 20;
ctx.save();
ctx.translate(5*radius, radius);
ctx.scale(radius, -radius);
for (var i = 0; i < 40; i++) {
for (var j = 1; j < 15; j++) {
var color = Munsell.interpolate(i, j, k);
if (!color_charted(color)) { color = [0,0,0]; }
if (!(color_valid(color) || window.options.oog)) { continue; }
ctx.fillStyle = colorname(color);
drawWedge(ctx, lightness_map(j-1), lightness_map(j),
i*angle, (i+1)*angle);
ctx.fill();
}
}
ctx.restore();
}
function draw_lc(ctx, radius, i) {
ctx.save();
ctx.translate(0, 2*radius);
ctx.scale(2*radius, -2*radius);
for (var j = 1; j < 15; j++) {
for (var k = 0; k < 26; k++) {
var color = Munsell.interpolate(i, j, k);
if (!color_charted(color)) { color = [0,0,0]; }
if (!(color_valid(color) || window.options.oog)) { continue; }
ctx.fillStyle = colorname(color);
ctx.fillRect(chroma_map(k), lightness_map(j-1),
chroma_map(k+1) - chroma_map(k),
lightness_map(j) - lightness_map(j-1));
}
}
ctx.restore();
}
function draw() {
var m = document.getElementById('main');
m.width = m.width;
var c = m.getContext('2d');
var radius = m.height / 2;
draw_lc(c, radius, window.current_color[0]);
draw_hc(c, radius, window.current_color[1]);
draw_hl(c, radius, window.current_color[2]);
draw_selections(c, radius, window.options.discrete);
show_color_in_elt(window.current_color,
document.getElementById('selected'),
document.getElementById('selected_box'));
}
function draw_continuous() {
var m = document.getElementById('main');
m.width = m.width;
var c = m.getContext('2d');
var radius = m.height / 2;
var w = m.width;
var h = m.height;
var d = c.getImageData(0, 0, w, h);
for (var x = 0; x < w; x++) {
for (var y = 0; y < h; y++) {
var i = (x + y * w) * 4;
var color = get_color_index(x, y);
d.data[i] = d.data[i + 1] = d.data[i + 2] = d.data[i + 3] = 0;
if (color) {
color = Munsell.interpolate(color[0], color[1], color[2]);
if (color_valid(color) || window.options.oog) {
for (var j = 0; j < 3; j+= 1) {
d.data[i + j] = Math.max(0,
Math.min(Math.round(color[j] * 255),
255));
}
d.data[i + 3] = 255;
}
}
}
}
d = c.putImageData(d, 0, 0);
draw_selections(c, radius, false);
show_color_in_elt(window.current_color,
document.getElementById('selected'),
document.getElementById('selected_box'));
}
function munsell_to_index(h, l, c) {
var i, j, k;
// We start i = 0 as 2.5R
i = ((h * 0.4) + 39) % 40;
if (l < 1) {
j = l * 5;
} else {
j = l + 4;
}
k = c * 0.5;
return [i, j, k];
}
function get_color_index(x, y) {
var radius = document.getElementById('main').height / 2;
var ans;
if (x < 2 * radius) {
x = x/(2 * radius);
y = 1 - y/(2 * radius);
ans = munsell_to_index(0, y * 10, x * 52);
ans[0] = window.current_color[0];
} else if ( x < 4 * radius ) {
x = x/radius - 3;
y = 1 - y/radius;
// Add 2.5 because 2.5R is the zero angle
ans = munsell_to_index(Math.atan2(y, x) * 50 / Math.PI + 2.5,
0, Math.sqrt(x*x + y*y) * 52);
ans[1] = window.current_color[1];
} else {
x = x/radius - 5;
y = 1 - y/radius;
// Add 2.5 because 2.5R is the zero angle
ans = munsell_to_index(Math.atan2(y, x) * 50 / Math.PI + 2.5,
Math.sqrt(x*x + y*y) * 10, 0);
ans[2] = window.current_color[2];
}
if (ans[0] < 0 || ans[1] < 0 || ans[2] < 0) {
return;
}
if (ans[0] >= 40 || ans[1] > 14 || ans[2] >= 26) {
return;
}
return ans;
}
function show_color_in_elt(color, elt, showbox) {
if (!color) { return; }
var ans = munsell_name(color[0], color[1], color[2]);
var rgb = Munsell.interpolate(color[0], color[1], color[2]);
var rgbname = colorname(rgb);
if (color_charted(rgb)) {
if (color_valid(rgb)) {
ans += ' ' + rgbname;
} else {
ans += ' <span class="warning">' + rgbname + '</span>';
}
showbox.style.background = rgbname;
} else {
ans += ' <span class="error">(Uncharted)</span>';
showbox.style.background = 'transparent';
}
elt.innerHTML = ans;
}
function fetch_input_color() {
var h1 = document.getElementById('goto_hue1');
var h2 = document.getElementById('goto_hue2');
var h = parseFloat(h1.value) + parseFloat(h2.value);
var l = parseFloat(document.getElementById('goto_lightness').value);
var c = parseFloat(document.getElementById('goto_chroma').value);
return [h, l, c];
}
window.options = new Object();
window.options.refresh = function () {
this.discrete = document.getElementById('discrete_check').checked;
this.oog = document.getElementById('oog_check').checked;
};
window.onload = function () {
// Set up
var width = document.body.clientWidth;
var m = document.getElementById('main');
m.width = width;
m.height = Math.floor(width / 3);
window.options.refresh();
// Pick initial color and draw once
window.current_color = [30, 8, 4]; // h, l, c
draw();
// Having drawn once, set up event handlers
// Select color at the mouse position
m.onmousemove = function (e) {
if (!e) { e = window.event; }
var xy = mouse_coords(e);
var color = get_color_index(xy[0] - m.offsetLeft, xy[1] - m.offsetTop);
if (color) {
window.options.refresh();
if (window.options.discrete) {
// Why is the lightness index discretized with ceiling?
// It's because the tile view starts with lightness > 0.
// Contrast this to chroma, which starts at 0 in tile mode.
// This is also why when we draw wedges, we use j and j-1
// and k+1 and k. Fun, endless confusion!
// Fortunately, the correct offsets and everything can
// be found more or less by trial and error.
color[0] = Math.floor(color[0]);
color[1] = Math.ceil(color[1]);
if (color[1] == 0) { color[1] = 1; }
color[2] = Math.floor(color[2]);
}
if (window.mouse_is_down) {
if (window.current_color != color) {
window.current_color = color;
draw();
}
}
show_color_in_elt(color,
document.getElementById('mouseover'),
document.getElementById('mouseover_box'));
}
};
// Redraw the canvas upon resize.
window.onresize = function () {
var width = document.body.clientWidth;
m.width = width;
m.height = Math.floor(width / 3);
draw();
};
// Handle clicks on the canvas
m.onmousedown = function (e) {
window.mouse_is_down = true;
m.onmousemove(e);
return false;
};
m.onselectstart = function () { return false; };
window.onmouseup = function () { window.mouse_is_down = false; };
m.onmouseout = function () { window.mouse_is_down = false; };
// Handle the continuous-render button
document.getElementById('continuous_button').onclick = function () {
var button = this;
function draw_continuous_finish() {
window.options.refresh();
draw_continuous();
button.disabled = false;
}
button.disabled = true;
setTimeout(draw_continuous_finish, 25);
};
// Handle the color-input form
document.getElementById('goto_form').onsubmit = function () {
var hlc = fetch_input_color();
var color = munsell_to_index(hlc[0], hlc[1], hlc[2]);
if (color) {
window.current_color = color;
draw();
}
return false;
};
};
//-->
</script>
<style type="text/css">
body{
background: #000;
color: #ccc;
text-align: left;
font-family: sans-serif;
}
canvas {
display: block;
margin-left: auto;
margin-right: auto;
margin-bottom: 0.5em;
background: #777;
border: 1px solid #333;
}
span#mouseover_box, span#selected_box {
display: inline-block;
overflow: hidden;
width: 1em;
height: 1em;
border: 1px dotted #fff;
}
ul#indicators_list {
float: right;
text-align: right;
list-style-type: none;
margin: 0em 0em 0em 1em;
padding-left: 0em;
}
ul#options_list {
float: left;
text-align: left;
list-style-type: none;
margin: 0em;
padding-left: 0em;
}
li {
margin: 0.6em 0em;
}
ul#indicators_list li, ul#options_list li {
margin: 0.2em 0em;
}
span.warning {
color: #fc0;
}
span.error {
color: #f66;
}
h1 {
padding: 0.5em;
clear: both;
text-align: center;
}
a:link { color: #6cf; }
a:visited { color: #c6f; }
a:hover { color: #cff; }
a:active { color: #ffc; }
input, select, option {
font-family: sans-serif;
color: #fff;
background: #333;
border: 1px solid #999;
}
input[type="submit"], input[type="button"] {
background: #666;
border: 2px outset #999;
}
input[disabled] {
color: #999;
border: 2px solid #666;
}
input:active {
background: #999;
border: 2px inset #999;
}
input[type="number"] {
width: 7ex;
}
div#mainbox {
max-width: 50em;
margin: 1em auto;
}
</style>
</head>
<body>
<canvas id="main" width="300" height="300">
<img src="thumb.png" alt="Preview" />
This demo requires HTML5 Canvas support.
</canvas>
<ul id="options_list">
<li title="Snap selection to the color of the nearest tile.">
<input type="checkbox" id="discrete_check" checked />
<label for="discrete_check">Select discrete</label>
</li>
<li title="Plot approximations to colors that are out of the sRGB gamut.">
<input type="checkbox" id="oog_check" />
<label for="oog_check">Plot out-of-range colors</label>
</li>
<li title="Plot the color charts with interpolation between tiles.">
<input type="button" id="continuous_button" value="Render interpolated" />
</li>
</ul>
<ul id="indicators_list">
<li>Selected:
<span id="selected">None</span>
<span id="selected_box"> </span>
</li>
<li>Mouse over:
<span id="mouseover">None</span>
<span id="mouseover_box"> </span>
</li>
<li><form id="goto_form" action="#">
<input type="submit" id="goto_button" value="Go to" />
<input type="number" id="goto_hue2" title="Hue"
min="0" max="10" step="0.1" value="7.5" />
<select id="goto_hue1" title="Hue">
<option value="0">R</option>
<option value="10">YR</option>
<option value="20">Y</option>
<option value="30">GY</option>
<option value="40">G</option>
<option value="50">BG</option>
<option value="60">B</option>
<option value="70" selected>PB</option>
<option value="80">P</option>
<option value="90">RP</option>
</select>
<input type="number" id="goto_lightness" title="Value"
min="0" max="10" step="0.1" value="4.0" />/<input
type="number" id="goto_chroma" title="Chroma"
min="0" max="50" step="0.5" value="8.0" />
</form></li>
</ul>
<div id="mainbox">
<h1>Munsell Color Palette</h1>
<p>The above grids can be used to choose colors from the
<a href="http://en.wikipedia.org/wiki/Munsell_color_system">Munsell color system</a> and display them in <a href="http://en.wikipedia.org/wiki/SRGB">sRGB</a> coordinates. The grids are (in order) value/chroma, chroma/hue, and value/hue.</p>
<h2>Limitations</h2>
<ul>
<li>The xyY-to-sRGB formula (in the <a href="fetch.py">python script</a> used to process the renotation data) is from Wikipedia. Unless you're willing to verify the formula independently, put no more trust in the correctness of this page than you'd put in the correctness of Wikipedia.</li>
<li>The data set used to plot these colors contains no grayscales. Black is assumed to be (0,0,0) in sRGB coordinates, and all other grays are interpolated by averaging hue-opposites with the smallest chroma coordinate.</li>
<li>Interpolation is piecewise linear as a map from Munsell (hue, value, chroma) coordinates to (r, g, b) coordinates. (One could argue that it would be more appropriate to interpolate to coordinates in, say, <a href="http://en.wikipedia.org/wiki/CIELUV">CIELUV</a> before converting formulaically to sRGB.)</li>
<li>There is no support for zooming. As a workaround until I come up with a way to fix this, I suggest using the input boxes on the right side of the page for fine-grained navigation.</li>
<li>Tested only in Firefox 8.0.1 and Chromium 15.0.874.121.</li>
</ul>
<h2>Sources</h2>
<ul>
<li><a href="http://en.wikipedia.org/wiki/SRGB#Specification_of_the_transformation">The xyY-to-sRGB formula</a> is from Wikipedia.</li>
<li>The data set used to plot these colors is the <a href="http://www.cis.rit.edu/research/mcsl2/online/munsell.php">Munsell Renotation Data</a>, from the Munsell Color Science Laboratory at the Rochester Institute of Technology.</li>
<li>Mouse coordinates are computed using code cribbed <a href="http://www.quirksmode.org/js/events_properties.html">from quirksmode.js</a>. (<a href="http://www.quirksmode.org/about/copyright.html">License</a>)</li>
</ul>
<h2>License</h2>
<p>The code is available under the <a href="https://opensource.org/licenses/MIT">MIT License</a>. This does not cover the Munsell Renotation Data (munsell-floats.js).</p>
</div>
</body>
</html>