-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
678 lines (595 loc) · 23.2 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
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
<!DOCTYPE html>
<html>
<head>
<title>Color coding designer</title>
<meta name="description" content="Small Javascript application supporting color coding design for a set of labels">
<meta name="keywords"
content="color, colour, coding, data, design, label, category, science, csv, hsl, rgb, hex, spectrum, rainbow, visualization">
<style>
body {
width: 100%;
height: 100%;
margin: 8px 0px;
position: absolute;
z-index: -4;
}
#vis-area {
z-index: -3;
position: relative;
margin: auto;
width: 90%;
height: 50%;
min-width: 360px;
max-height: 400px;
background-color: black;
border: 1px solid black;
box-shadow: 0px 0px 80px black;
}
#vis-area.drop-possible {
border: 1px solid yellow;
box-shadow: 0px 0px 80px teal;
}
#color-space {
width: 100%;
height: 100%;
border: 0px;
padding: 0px;
margin: 0px;
z-index: -1;
position: absolute;
}
#color-space-svg {
padding: 0px;
margin: 0px;
}
.label {
z-index: 0;
position: absolute;
background: rgba(255, 255, 255, 0.45);
border: 1px solid rgba(255, 255, 255, 0.45);
color: black;
font-family: monospace;
padding: 3px;
border-radius: 8px;
transform-origin: 0 0;
transform: rotate(45deg);
cursor: grab;
}
.label span {
display: block;
max-width: 12em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.marker {
border: 2px solid white;
border-radius: 8px;
width: 8px;
height: 8px;
background-color: none;
position: absolute;
top: -7px;
left: -7px;
}
.dragged {
cursor: grabbing !important;
}
.active {
border-color: lime;
box-shadow: 0px 0px 16px lime !important;
}
.hover {
box-shadow: 0px 0px 16px grey;
}
.selected {
border: 1px solid white;
box-shadow: 0px 0px 8px white;
}
.doomed {
color: red;
border-color: red;
box-shadow: 0px 0px 16px red !important;
}
#label-list {
position: relative;
width: 100%;
margin: 0.5em auto 0em auto;
text-align: center;
}
#control-bar {
width: 65%;
min-width: 320px;
clear: both;
margin: auto;
padding: 0.5em 2%;
border-radius: 1em;
background-color: lightgrey;
}
#control-bar input {
width: 23%;
display: inline-block;
margin: auto;
font-family: monospace;
}
#control-bar input[type=file] {
display: none !important;
}
#label-list .label-form {
position: relative;
display: inline-block;
width: auto;
min-width: 320px;
border-radius: 0.5em;
padding: 1px 3px;
margin: 5px 2px;
background-color: lightgrey;
}
input {
height: 2.5em;
vertical-align: middle;
}
.label-form input[type="text"] {
border: 0px;
border-radius: 0.5em;
margin: 0px 1.5em 0px 0.5em;
background-color: white;
color: black;
font-family: monospace;
padding: 2px 0.75em;
width: 12em;
}
#footer {
text-align: center;
}
</style>
<script type="text/javascript">
// https://stackoverflow.com/questions/1740700/how-to-get-hex-color-value-rather-than-rgb-value
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
function clientPos(e) {
if (e.type.startsWith('touch')) {
let touch = e.changedTouches[0];
return { 'x': Math.round(touch.clientX), 'y': Math.round(touch.clientY) };
} else {
return { 'x': e.clientX, 'y': e.clientY };
}
}
// https://www.w3schools.com/howto/howto_js_draggable.asp
function setUpDraggableLabel(el, labelId) {
var posX = 0, posY = 0, startX = 0, startY = 0, clickTime = 0;
var dragged = false, distance = 0;
var label = labels.get(labelId);
el.onmousedown = dragMouseDown;
el.ontouchstart = dragMouseDown;
el.onmouseenter = function (e) { label.form.classList.add("hover"); };
el.onmouseleave = function (e) { label.form.classList.remove("hover"); };
el.onclick = function (e) { giveFocusIfShortClick(); }
// Must be a short click, or click without dragging.
// It is implemented that way to avoid automated scrolling
// down to the focused field which might be an issue with many labels added
function giveFocusIfShortClick() {
if ((Date.now() - clickTime) < 400 && distance < 5 || !dragged) {
label.form.getElementsByClassName("label-name")[0].focus();
}
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
document.activeElement.blur();
clickTime = Date.now();
dragged = false;
distance = 0;
let pos = clientPos(e);
startX = pos.x;
startY = pos.y;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
document.ontouchend = closeDragElement;
document.ontouchmove = elementDrag;
el.classList.add("dragged");
label.form.classList.add("active");
}
function elementDrag(e) {
e = e || window.event;
dragged = true;
let pos = clientPos(e);
posX = startX - pos.x;
posY = startY - pos.y;
distance += Math.abs(posX) + Math.abs(posY);
startX = pos.x;
startY = pos.y;
el.style.top = clamp((el.offsetTop - posY) * 100 / el.parentElement.clientHeight, -5, 120) + "%";
el.style.left = clamp((el.offsetLeft - posX) * 100 / el.parentElement.clientWidth, -5, 105) + "%";
if (elementOffParent(el)) {
label.form.classList.add("doomed");
el.classList.add("doomed");
} else {
el.classList.remove("doomed");
label.form.classList.remove("doomed");
}
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
document.ontouchend = null;
document.ontouchmove = null;
label.form.classList.remove("active");
el.classList.remove("dragged");
giveFocusIfShortClick();
if (elementOffParent(el)) {
deleteLabel(labelId);
}
}
}
function clamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
function elementOffParent(el) {
return el.offsetLeft < -5 || el.offsetLeft > (el.parentElement.clientWidth + 4)
|| el.offsetTop < -5 || el.offsetTop > (el.parentElement.clientHeight + 4);
}
// https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
// https://stackoverflow.com/questions/39118528/rgb-to-hsl-conversion
function rgbToHsl(r, g, b) {
var R = r / 255;
var G = g / 255;
var B = b / 255;
var min = Math.min(R, G, B);
var max = Math.max(R, G, B);
var lum = (min + max) / 2;
var sat = 0;
var hue = 0;
if (min != max) {
if (lum <= 0.5) {
sat = (max - min) / (max + min);
} else {
sat = (max - min) / (2 - max - min);
}
var C = max - min;
if (R == max) {
hue = (G - B) / C;
} else if (G == max) {
hue = 2.0 + (B - R) / C;
} else if (B == max) {
hue = 4.0 + (R - G) / C;
}
hue *= 60;
}
if (hue < 0) {
hue += 360;
}
var r = Object();
r.lum = lum * 100;
r.sat = sat * 100;
r.hue = hue;
return r;
}
function synchronizedValues(label) {
var dummy = document.createElement("div");
var inputText = label.form.querySelector("input[type=text]");
inputText.addEventListener("input", textChanged);
inputText.addEventListener("focus", function (e) { label.div.classList.add('selected'); });
inputText.addEventListener("blur", function (e) { label.div.classList.remove('selected'); });
var inputColor = label.form.querySelector("input[type=color]");
inputColor.addEventListener("input", colorSet);
var inputDelete = label.form.querySelector("input[type=button][value=Delete]");
inputDelete.onclick = deleteClicked;
var observer = new MutationObserver(function (mutations) {
labelMoved();
});
observer.observe(label.div, { attributes: true, attributeFilter: ['style'] });
labelMoved();
function textChanged(e) {
let s = label.div.getElementsByTagName("span")[0];
s.textContent = e.target.value;
s.setAttribute("title", e.target.value);
}
function labelMoved() {
var hue = 360 * (label.div.offsetLeft / label.div.parentElement.clientWidth);
var lum = 5 + 90 * (label.div.offsetTop / label.div.parentElement.clientHeight);
var hsl = "hsl(" + hue + ", 100%, " + lum + "%)";
dummy.style.color = hsl;
inputColor.value = rgb2hex(dummy.style.color);
}
function colorSet(e) {
label.setHexColor(e.target.value);
e.preventDefault();
}
function deleteClicked(e) {
deleteLabel(label.id);
e.preventDefault();
}
}
function makeSvg(tag) {
return document.createElementNS('http://www.w3.org/2000/svg', tag);
}
function makeGradient(hue) {
const checkpoints = 5;
const start = 5;
const stop = 95;
const w = stop - start;
const step = 100 / (checkpoints - 1);
const lumStep = w / (checkpoints - 1);
var g = makeSvg('linearGradient');
g.setAttribute("id", "g" + hue);
g.setAttribute("x1", "0%");
g.setAttribute("x2", "0%");
g.setAttribute("y1", "0%");
g.setAttribute("y2", "100%");
for (var i = 0; i < checkpoints; ++i) {
var percent = (i * step) + "%";
var lum = (start + i * lumStep) + "%";
var s = makeSvg('stop');
s.setAttribute("offset", percent);
s.setAttribute("style", "stop-color:hsl(" + hue + ", 100%, " + lum + ");stop-opacity:1");
g.appendChild(s);
}
return g;
}
function makeRect(hue) {
var r = makeSvg('rect');
var w = 100 / 360;
r.setAttribute("x", hue * w + "%");
r.setAttribute("y", 0 + "%");
r.setAttribute("width", w * 1.5 + "%");
r.setAttribute("height", "100%");
r.setAttribute("fill", "url(#g" + hue + ")");
return r;
}
var idSequence = 0;
var labels = new Map();
function makeLabelDiv(text, position) {
var marker = document.createElement("div");
marker.classList.add("marker");
var s0 = document.createElement("span");
s0.textContent = text;
s0.setAttribute("title", text);
var d0 = document.createElement("div");
d0.classList.add("label");
d0.appendChild(marker);
d0.append(s0);
if (position !== undefined && position !== null) {
d0.style.top = position.y;
d0.style.left = position.x;
} else {
d0.style.top = (60 - Math.random() * 20) + "%";
d0.style.left = (60 - Math.random() * 20) + "%";
}
return d0;
}
function makeLabelForm(id, text) {
var fs = document.createElement("fieldset");
fs.classList.add("label-form");
var inColor = document.createElement("input");
inColor.setAttribute("type", "color");
inColor.setAttribute("name", "col-" + id);
fs.appendChild(inColor);
var inText = document.createElement("input");
inText.setAttribute("type", "text");
inText.setAttribute("name", "txt-" + id);
inText.setAttribute("value", text);
inText.classList.add("label-name");
fs.appendChild(inText);
var inDelete = document.createElement("input");
inDelete.setAttribute("type", "button");
inDelete.setAttribute("value", "Delete");
inDelete.setAttribute("name", "del-" + id);
fs.appendChild(inDelete);
return fs;
}
function addLabel(text, color, position) {
var id = idSequence++;
var placeholder = (text === undefined || text === null) ? 'Label#' + id : text;
var label = Object();
label.id = id;
label.div = makeLabelDiv(placeholder, position);
label.form = makeLabelForm(id, placeholder);
label.setHexColor = function (hex) {
var r = parseInt(hex.substring(1, 3), 16);
var g = parseInt(hex.substring(3, 5), 16);
var b = parseInt(hex.substring(5, 7), 16);
var hsl = rgbToHsl(r, g, b);
label.div.style.left = (100 * hsl.hue / 360) + "%";
label.div.style.top = (100 * (Math.min(Math.max(hsl.lum, 5), 95) - 5) / 90) + "%";
};
labels.set(id, label);
document.getElementById("vis-area").appendChild(label.div);
document.getElementById("label-list").appendChild(label.form);
setUpDraggableLabel(label.div, id);
synchronizedValues(label);
label.form.getElementsByClassName("label-name")[0].select();
if (color !== undefined && color !== null && color[0] == "#") {
label.setHexColor(color);
}
return label;
}
function deleteLabel(id) {
var label = labels.get(id);
label.div.remove();
label.form.remove();
labels.delete(label.id);
}
function sortByHue() {
var references = Array();
for (var label of labels.values()) {
label.form.remove();
references.push({ 'id': label.id, 'x': label.div.offsetLeft, 'y': label.div.offsetTop });
}
references.sort(function (lhs, rhs) {
if (lhs.x == rhs.x && lhs.y == rhs.y)
return 0;
if (lhs.x == rhs.x)
return lhs.y < rhs.y ? -1 : 1;
return lhs.x < rhs.x ? -1 : 1;
});
for (var ref of references) {
document.getElementById("label-list").appendChild(labels.get(ref.id).form);
}
}
function createCsv() {
const escapeCsv = function (text) {
if (!(text.includes(',') || text.includes('"')))
return text;
return '"' + text.replaceAll('"', '""') + '"';
};
var content = 'sep=,\n';
content += 'label,color\n';
for (var el of document.getElementsByClassName("label-form")) {
content += escapeCsv(el.querySelector("input[type=text]").value);
content += ',';
content += el.querySelector("input[type=color]").value;
content += '\n';
}
const file = new Blob([content], { type: 'text/csv' });
const link = document.createElement("a");
link.href = URL.createObjectURL(file);
link.download = "color_codes.csv";
link.click();
URL.revokeObjectURL(link.href);
}
function readCsv(content) {
var possibleHeaderRow = 0;
var labelIndex = 0, colorIndex = 1;
var sep = ',';
var line, fields;
const lines = content.split("\n");
for (var i = 0; i < lines.length; ++i) {
line = lines[i];
if (i == 0 && line.startsWith('sep=') && line.length >= 5) {
sep = line[4];
++possibleHeaderRow;
continue;
}
fields = line.split(sep);
if (fields.length < 2) {
continue;
}
if (i == possibleHeaderRow) {
var fu = fields.map(s => s.toUpperCase().trim());
var li = fu.indexOf("LABEL");
var ci = fu.indexOf("COLOR");
if (li >= 0 && ci >= 0) {
labelIndex = li;
colorIndex = ci;
continue;
}
}
addLabel(fields[labelIndex], fields[colorIndex]);
}
}
function setUpVisualizationAreaAsDroppable() {
var depth = 0;
let space = document.getElementById("vis-area");
space.addEventListener("dragenter", dragEnter);
space.addEventListener("dragleave", dragLeave);
space.addEventListener("dragover", dragOver);
space.addEventListener("drop", drop);
// Every element inside receives these...
// So it is a lot of enters and leaves
function dragEnter(e) {
if (depth == 0) {
space.classList.add("drop-possible");
}
++depth;
e.stopPropagation();
e.preventDefault();
}
function dragLeave(e) {
--depth;
if (depth == 0) {
space.classList.remove("drop-possible");
}
e.stopPropagation();
e.preventDefault();
}
function dragOver(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
depth = 0;
space.classList.remove("drop-possible");
handleInputFiles(e.dataTransfer.files);
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/File_API/Using_files_from_web_applications
function handleInputFiles(files) {
if (files.length == 0) {
return;
}
const file = files[0];
const reader = new FileReader();
reader.onload = (e) => { readCsv(e.target.result); };
reader.readAsText(file);
}
function buildColorSpace() {
var svg = document.getElementById('color-space-svg');
var defs = svg.getElementsByTagName("defs")[0];
for (var i = 0; i < 360; ++i) {
defs.appendChild(makeGradient(i));
}
for (var i = 0; i < 360; ++i) {
svg.appendChild(makeRect(i));
}
}
window.addEventListener("load", function () {
buildColorSpace();
const visArea = document.getElementById("vis-area");
visArea.ondblclick = function (e) {
let pos = clientPos(e);
pos.x = 100 * ((pos.x - visArea.offsetLeft) / visArea.clientWidth) + "%";
pos.y = 100 * ((pos.y - visArea.offsetTop) / visArea.clientHeight) + "%";
addLabel(null, null, pos);
};
document.getElementById("add-button").onclick = function (e) { addLabel(); };
document.getElementById("sort-button").onclick = function (e) { sortByHue(); };
document.getElementById("download-button").onclick = function (e) { createCsv(); };
document.getElementById("upload-button").onclick = function (e) {
document.getElementById("upload").click();
};
document.getElementById("upload").addEventListener("input", function (e) {
handleInputFiles(e.target.files);
e.target.value = "";
e.preventDefault();
}, false);
setUpVisualizationAreaAsDroppable();
const urlParams = new URLSearchParams(window.location.search);
const isTest = urlParams.get('test');
if (isTest !== undefined && isTest !== null) {
addLabel();
addLabel();
addLabel();
}
});
</script>
</head>
<body>
<div id="vis-area">
<div id="color-space">
<svg id="color-space-svg" width='100%' height='100%' viewBox="0 0 720 200" preserveAspectRatio="none">
<defs></defs>
</svg>
</div>
</div>
<form id="label-list" onsubmit="return false;">
<input type="submit" disabled style="display: none" aria-hidden="true" />
<fieldset id="control-bar">
<input type="button" name="add-button" id="add-button" value="Add" />
<input type="button" name="sort-button" id="sort-button" value="Sort" />
<input type="button" name="upload-button" id="upload-button" value="Load" />
<input type="file" name="upload" id="upload" accept=".csv" />
<input type="button" name="download-button" id="download-button" value="Save" />
</fieldset>
</form>
<hr>
<p id="footer">View <a href="https://github.com/Bajron/ColorCodingDesigner">the code and instruction</a></p>
</body>
</html>