-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
82 lines (57 loc) · 1.73 KB
/
main.js
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
(function() {
"use strict";
function toHex(text) {
var s = unescape(encodeURIComponent(text)); // convert utf8 to latin1
var h = [],
c = '';
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i).toString(16);
if (c.length < 2) c = '0' + c;
h.push(c);
}
return h;
}
function fillGrid(colors) {
var grid = document.getElementById('color-grid'),
div = null;
grid.innerHTML = '<h2>Color Grid</h2>';
for (var i = 0; i < colors.length; i++) {
div = document.createElement('div');
div.className = 'color-grid__item';
div.style.backgroundColor = colors[i];
grid.appendChild(div);
}
}
function generate() {
var elem = document.getElementById('original-text');
var original = elem.value.trim();
elem.value = original;
if (0 === original.length) {
alert('Please enter some text.');
return;
}
var hex = toHex(original);
var colors = [];
var cur = '';
for (var i = 0; i < hex.length; i++) {
cur += hex[i];
if (0 === (i + 1) % 3) {
colors.push('#' + cur);
cur = '';
}
}
if (cur.length < 6) {
while (cur.length < 6) cur += '00';
colors.push('#' + cur);
}
fillGrid(colors);
}
function domReady(evt) {
document.getElementById('generate').onclick = function(e) {
e.preventDefault();
generate();
return false;
}
}
window.addEventListener('DOMContentLoaded', domReady);
})();