-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
66 lines (63 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
function cell(r, g, b, cid, f) {
const c = `rgb(${r},${g},${b})`;
const a = r + g + b < 300 ? '#999' : '#000';
return $('<td>').css('background-color', c).prop('title', cid).click(() => f({
c, cid, a, ct: [r, g, b],
}));
}
function setupTable(t, f) {
let tr;
let i;
for (i = 16; i < 232; i++) {
if ((i - 16) % 36 === 0) {
tr = $('<tr>');
t.append(tr);
}
tr.append(cell(...getIndexedColor(i), i, f));
}
// include all the greys for the ramp
let colors = [];
for (i = 16; i < 232; i += 43) {
colors.push([i, getIndexedColor(i)]);
}
for (i = 232; i < 256; i++) {
colors.push([i, getIndexedColor(i)]);
}
colors.sort((a, b) => b[1][0] - a[1][0]);
tr = $('<tr>');
colors.forEach(c => {
tr.append(cell(...c[1], c[0], f));
});
t.append(tr);
}
function setColorByPrefix(prefix, idx) {
const c = getIndexedColor(idx);
$(`.${prefix}-label`).text(idx);
$(`.${prefix}-label-hex`).text(c.map(x => x.toString(16).padStart(2, '0')).join(''));
$(`.${prefix}-label-rgb`).text(c.join(', '));
}
$(() => {
let fgt = [0, 0, 0];
let bgt = [255, 255, 255];
setupTable($('#gbg'), ({ c, a }) => {
$('body').css({
'background-color': c,
color: a,
});
});
setupTable($('#bg'), ({ c, cid, ct }) => {
$('#ex').css('background-color', c);
setColorByPrefix('bg', cid);
bgt = ct;
$('.contrast-ratio').text(contrastRatio(bgt, fgt).toFixed(1));
});
setupTable($('#fg'), ({ c, cid, ct }) => {
$('#ex').css('color', c);
setColorByPrefix('fg', cid);
fgt = ct;
$('.contrast-ratio').text(contrastRatio(bgt, fgt).toFixed(1));
});
setColorByPrefix('fg', 16);
setColorByPrefix('bg', 231);
$('.contrast-ratio').text(contrastRatio(bgt, fgt).toFixed(1));
});