-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
239 lines (233 loc) · 8.88 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
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<meta content="initial-scale=1, width=device-width" name="viewport">
<title>Contrast checker</title>
<style>
:root {
box-sizing: border-box;
font-size: 100%;
}
:root * {
box-sizing: inherit;
}
body {
background-color: #FFF;
color: #212121;
font-family: "IBM Plex Sans", Roboto, sans-serif;
line-height: 1.5rem;
margin: 1.5rem;
}
h1 {
font-size: 2.25rem;
font-weight: 600;
line-height: 3rem;
margin: 0;
max-width: 45rem;
}
h2 {
font-size: 1.5rem;
font-weight: 600;
line-height: 2.25rem;
margin: 3rem 0 1.5rem;
max-width: 45rem;
}
h3 {
font-size: 0.875rem;
font-weight: 600;
line-height: 1.5rem;
margin: 0;
text-align: center;
}
textarea {
border: 2px solid #757575;
border-radius: 0.125rem;
color: #212121;
font: inherit;
height: 3rem;
margin: 0 0 1.5rem;
max-width: 45rem;
padding: calc(0.75rem - 2px);
width: 100%;
}
button {
background-color: #757575;
border: none;
border-radius: 0.125rem;
color: #FFF;
font: inherit;
margin: 1.5rem 0 0;
padding: 0.75rem 1.5rem;
}
label {
display: block;
font-weight: 600;
}
select {
font: inherit;
padding: 0.75rem;
}
p {
max-width: 45rem;
}
.swatches {
display: grid;
grid-auto-rows: min-content;
grid-column-gap: 1.5rem;
grid-row-gap: 1.5rem;
grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
list-style: none;
margin: 3rem 0;
padding: 0;
}
.swatches li {
background-color: #F5F5F5;
}
.swatches dl {
display: flex;
flex-wrap: wrap;
font-size: 0.875rem;
margin: 0;
padding: 0.75rem;
}
.swatches dt,
.swatches dd {
margin: 0;
width: 50%;
}
.swatches dd {
text-align: right;
}
.swatches span {
display: block;
margin: 0 0 0.75rem;
padding: 0.75rem 0;
text-align: center;
}
.fail {
color: #D50000;
font-weight: 600;
text-transform: uppercase;
}
.pass {
color: #2E7D32;
font-weight: 600;
text-transform: uppercase;
}
</style>
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans:400,600&display=swap" rel="stylesheet">
</head>
<body>
<h1>Contrast checker</h1>
<p>Check the contrast ratio of many colours at once.</p>
<label for="foregrounds">Foreground colours (as HEX values)</label>
<textarea id="foregrounds"></textarea>
<label for="backgrounds">Background colours (as HEX values)</label>
<textarea id="backgrounds"></textarea>
<label for="all">
<input id="all" name="threshold" type="radio" value="all" checked>
All combinations
</label>
<label for="aa-ui">
<input id="aa-ui" name="threshold" type="radio" value="aa-ui">
Contrast ratio of 3.1:1 or more (AA UI)
</label>
<label for="aa-text">
<input id="aa-text" name="threshold" type="radio" value="aa-text">
Contrast ratio of 4.5:1 or more (AA text)
</label>
<label for="aaa-text">
<input id="aaa-text" name="threshold" type="radio" value="aaa-text">
Contrast ratio of 7:1 or more (AAA text)
</label>
<button onclick="generateswatches()">Check contrast</button>
<ul class="swatches" id="swatches"></ul>
<script>
function generateswatches() {
var foregrounds = document.querySelector("#foregrounds").value;
var backgrounds = document.querySelector("#backgrounds").value;
var foregrounds = foregrounds.match(/#[0-9|A-F|a-f]{3,6}/g);
var backgrounds = backgrounds.match(/#[0-9|A-F|a-f]{3,6}/g);
var foregroundColors = removeDups(foregrounds);
var backgroundColors = removeDups(backgrounds);
var foregroundColors = foregroundColors.sort();
var backgroundColors = backgroundColors.sort();
var swatches = "";
backgroundColors.forEach(function(backgroundColor) {
var backgroundLuminance = getLuminance(backgroundColor);
foregroundColors.forEach(function(foregroundColor) {
foregroundLuminance = getLuminance(foregroundColor);
var contrastRatio = (Math.max(foregroundLuminance, backgroundLuminance) + 0.05) / (Math.min(foregroundLuminance, backgroundLuminance) + 0.05);
var radioButtons = document.getElementsByName("threshold");
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].checked) var threshold = radioButtons[i].value;
}
if ((threshold == "aa-ui" && contrastRatio >= 3) || (threshold == "aa-text" && contrastRatio >= 4.5) || (threshold == "aaa-text" && contrastRatio >= 7) || threshold == "all") {
swatches += "<li><span style='background-color:" + backgroundColor + ";color:" + foregroundColor + "'>Sample text</span>";
swatches += "<h3>" + foregroundColor.toUpperCase() + "<br>" + backgroundColor.toUpperCase() + "</h3>";
swatches += "<dl><dt>Ratio</dt><dd>" + contrastRatio.toFixed(2) + ":1</dd>";
if (contrastRatio < 3) {
swatches += "<dt>AAA text</dt><dd class='fail'>Fail</dd>";
swatches += "<dt>AA text</dt><dd class='fail'>Fail</dd>";
swatches += "<dt>AA UI</dt><dd class='fail'>Fail</dd>";
} else if (contrastRatio < 4.5) {
swatches += "<dt>AAA text</dt><dd class='fail'>Fail</dd>";
swatches += "<dt>AA text</dt><dd class='fail'>Fail</dd>";
swatches += "<dt>AA UI</dt><dd class='pass'>Pass</dd>";
} else if (contrastRatio < 7) {
swatches += "<dt>AAA text</dt><dd class='fail'>Fail</dd>";
swatches += "<dt>AA text</dt><dd class='pass'>Pass</dd>";
swatches += "<dt>AA UI</dt><dd class='pass'>Pass</dd>";
} else {
swatches += "<dt>AAA text</dt><dd class='pass'>Pass</dd>";
swatches += "<dt>AA text</dt><dd class='pass'>Pass</dd>";
swatches += "<dt>AA UI</dt><dd class='pass'>Pass</dd>";
}
swatches += "</dl></li>";
}
});
});
document.getElementById("swatches").innerHTML = swatches;
}
function removeDups(names) {
let unique = {};
names.forEach(function(i) {
if(!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
function getLuminance(colour) {
// Strip leading #
var rgb = colour.replace("#","");
// Expand shorthand hex values, e.g. #123 => #112233
if (rgb.length == 3) {
rgb = rgb.substring(0,1) + rgb.substring(0,1) + rgb.substring(1,2) + rgb.substring(1,2) + rgb.substring(2,3) + rgb.substring(2,3);
}
// Calculate relative luminance https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
red = parseInt(rgb.substring(0,2), 16) / 255;
if (red <= 0.03928) {
var r = red / 12.92;
} else {
var r = ((red + 0.055) / 1.055) ** 2.4;
}
green = parseInt(rgb.substring(2,4), 16) / 255;
if (green <= 0.03928) {
var g = green / 12.92;
} else {
var g = ((green + 0.055) / 1.055) ** 2.4;
}
blue = parseInt(rgb.substring(4,6), 16) / 255;
if (blue <= 0.03928) {
var b = blue / 12.92;
} else {
var b = ((blue + 0.055) / 1.055) ** 2.4;
}
luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return luminance;
}
</script>
</body>
</html>