-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
80 lines (76 loc) · 1.94 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- Copyright 2018 icasdri. See https://github.com/icasdri/colorview for details. -->
<title>colorview</title>
<style>
body {
text-align: center;
}
#color {
font-size: 1.1em;
text-align: center;
height: 2.4em;
width: 14em;
margin-top: calc(50vh - 2.4em);
margin-left: auto;
margin-right: auto;
padding-left: 1.66em;
padding-right: 1.66em;
}
#warn {
font-size: 1.5em;
text-align: center;
vertical-align: middle;
margin-left: -1.4em;
padding-bottom: 0.17em;
color: red;
}
#bad, #stage {
display: none;
}
</style>
</head>
<body>
<div>
<input type='text' id='color' value='#deadbeef'>
<span id='warn'>⚠</span>
</div>
<div id='stage'></div>
<div id='bad' style='bad'></div>
<script>
var color = document.getElementById('color');
var warn = document.getElementById('warn');
var stage = document.getElementById('stage');
var bad = document.getElementById('bad');
var badsty = window.getComputedStyle(bad)['background-color'];
var handler = function() {
var s = color.value;
s = s.replace(/^0x/, '#');
s = s.replace(/^([0-9A-Fa-f]+)$/, function(m, v) {
return '#' + v;
});
if (s == '') {
s = 'white'
}
stage.setAttribute('style', 'background-color: ' + s);
};
var observer = new MutationObserver(function() {
var computed = window.getComputedStyle(stage)['background-color'];
if (computed == badsty) {
warn.setAttribute('style', 'visibility: inherit');
} else {
warn.setAttribute('style', 'visibility: hidden');
document.body.setAttribute('style', 'background-color: ' + computed);
}
});
observer.observe(stage, {
attributes: true,
attributeFilter: ['style']
});
color.addEventListener('input', handler);
handler();
</script>
</body>
</html>