-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContrastChecker.html
470 lines (424 loc) · 14.6 KB
/
ContrastChecker.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Level Access - Contrast Checker</title>
<style>
#sample {
width:70%;
padding:.5em;
}
.float {
float:left;
}
#fontSize {
max-width:11em;
}
.sliderlbl {
min-width:3em;
float:left;
}
input[type="color"] {
height:1.3em;
width:4em;
}
.group {
background-color:#f3f3f3;
border: thin solid darkgray;
max-width: 100%;
margin-bottom: .5em;
padding:.5em;
}
.slidegroup input[type="number"] {
vertical-align:top;
display:inline-block;
width:4em;
}
.topgroup input {
vertical-align:top;
display:inline-block;
margin-bottom:.5em;
}
form {
width: 55%;
float:left;
}
#right {
float: left;
margin: 1em;
width:35%;
}
#footer {
clear:both;
}
h2 {
margin-bottom: 0em;
padding-bottom: 0em;
font-size:medium;
}
p {
margin-top:0em;
}
#samplegroup {
margin-bottom:1em;
}
input[type="range"] {
min-width:65%;
}
.slidegroup {
height:2em;
}
</style>
<script>
// *************************************************************************
function checkQSP() {
// grab query string parameter from url
var qsp = location.search;
// if exists update grab the values and update the fields
if(qsp){
var params = new URLSearchParams(qsp),
fg = params.get('fg'),
bg = params.get('bg');
setTimeout(function() {
// update text inputs
document.getElementById("fg").value = '#' + fg;
document.getElementById("bg").value = '#' + bg;
// update color inputs
document.getElementById("fgc").value = document.getElementById("fg").value;
document.getElementById("bgc").value = document.getElementById("bg").value;
// update sliders
synchRGBToHex();
},0);
}
}
// *************************************************************************
function synchHexToRGB() {
var fgr = document.getElementById("fgr");
var fgg = document.getElementById("fgg");
var fgb = document.getElementById("fgb");
var bgr = document.getElementById("bgr");
var bgg = document.getElementById("bgg");
var bgb = document.getElementById("bgb");
document.getElementById("fg").value = rgbToHex(fgr.value, fgg.value, fgb.value);
document.getElementById("bg").value = rgbToHex(bgr.value, bgg.value, bgb.value);
document.getElementById("fgc").value = document.getElementById("fg").value
document.getElementById("bgc").value = document.getElementById("bg").value
}
// **********************************************************************
function synchRGBToHex() {
var fgr = document.getElementById("fgr");
var fgg = document.getElementById("fgg");
var fgb = document.getElementById("fgb");
var bgr = document.getElementById("bgr");
var bgg = document.getElementById("bgg");
var bgb = document.getElementById("bgb");
var hex = document.getElementById("fg").value;
fgr.value = hexToRgb(hex).r;
fgri.value = hexToRgb(hex).r;
fgg.value = hexToRgb(hex).g;
fggi.value = hexToRgb(hex).g;
fgb.value = hexToRgb(hex).b;
fgbi.value = hexToRgb(hex).b;
hex = document.getElementById("bg").value;
bgr.value = hexToRgb(hex).r;
bgri.value = hexToRgb(hex).r;
bgg.value = hexToRgb(hex).g;
bggi.value = hexToRgb(hex).g;
bgb.value = hexToRgb(hex).b;
bgbi.value = hexToRgb(hex).b;
}
// **********************************************************************
function check(e)
{
var color1, color2;
var l1; // higher value
var l2; // lower value
var contrast;
var l1R, l1G, l1B, l2R, l2G, l2B;
var txtSizeOp;
color1 = document.getElementById('fg').value;
color2 = document.getElementById('bg').value;
txtSizeOp = document.getElementById('fontSize').selectedIndex;
// change of range, update rgb inputs
try {
if (e.target.tagName == "INPUT" && e.target.type == "range") {
var id = e.target.id + "i";
document.getElementById(id).value = e.target.value;
synchHexToRGB();
} // change of rgb inputs, change range sliders
else if (e.target.tagName == "INPUT" && e.target.type == "number") {
if (e.target.value > 255)
e.target.value = 255;
else if (e.target.value < 0)
e.target.value = 0;
var id = e.target.id.slice(0,e.target.id.length-1);
document.getElementById(id).value = e.target.value;
synchHexToRGB();
}
}
catch(e) {
}
// check hex value for errors - display error and return
// |[A-Fa-f0-9]{3})$
var reg = new RegExp("^#([A-Fa-f0-9]{6})");
if (!color1.match(reg) || !color2.match(reg)) {
document.getElementById('errtxt').textContent = "Error: ";
document.getElementById('instructions').style.display = "block";
document.getElementById('instructions').tabIndex = -1;
document.getElementById('instructions').focus();
return;
}
if (e) {
if (e.target.id == "bgc")
document.getElementById("bg").value = e.target.value;
else if (e.target.id == "bg")
document.getElementById("bgc").value = e.target.value;
else if (e.target.id == "fg")
document.getElementById("fgc").value = e.target.value;
else if (e.target.id == "fgc")
document.getElementById("fg").value = e.target.value;
}
var hexColors = ["bg","bgc","fg","fgc"];
if (hexColors.indexOf(e.target.id) >= 0)
synchRGBToHex()
// get colors again in case they have changed
color1 = document.getElementById('fg').value;
color2 = document.getElementById('bg').value;
// remove pound sign if present
if (color2.indexOf('#') == 0) {
color2 = color2.substr(1, color2.length-1);
}
if (color1.indexOf('#') == 0) {
color1 = color1.substr(1, color1.length-1);
}
// make sure no error message is shown
document.getElementById('errtxt').textContent = "";
//Linearised R (for example) = (R/FS)^2.2 where FS is full scale value (255
//for 8 bit color channels). L1 is the higher value (of text or background)
//alert(parseInt("0x"+color1.substr(0,2)));
//Math.pow(n,x);
l1R = parseInt("0x"+color1.substr(0,2))/255;
if (l1R <= 0.03928)
{
l1R = l1R/12.92;
}
else
{
l1R = Math.pow(((l1R+0.055)/1.055),2.4);
}
l1G = parseInt("0x"+color1.substr(2,2))/255;
if (l1G <= 0.03928)
{
l1G = l1G/12.92;
}
else
{
l1G = Math.pow(((l1G+0.055)/1.055),2.4);
}
l1B = parseInt("0x"+color1.substr(4,2))/255;
if (l1B <= 0.03928)
{
l1B = l1B/12.92;
}
else
{
l1B = Math.pow(((l1B+0.055)/1.055),2.4);
}
l2R = parseInt("0x"+color2.substr(0,2))/255;
if (l2R <= 0.03928)
{
l2R = l2R/12.92;
}
else
{
l2R = Math.pow(((l2R+0.055)/1.055),2.4);
}
l2G = parseInt("0x"+color2.substr(2,2))/255;
if (l2G <= 0.03928)
{
l2G = l2G/12.92;
}
else
{
l2G = Math.pow(((l2G+0.055)/1.055),2.4);
}
l2B = parseInt("0x"+color2.substr(4,2))/255;
if (l2B <= 0.03928)
{
l2B = l2B/12.92;
}
else
{
l2B = Math.pow(((l2B+0.055)/1.055),2.4);
}
//where L is luminosity and is defined as
l1 = (.2126*l1R) + (.7152*l1G) + (.0722*l1B); //using linearised R, G, and B value
l2 = (.2126*l2R) + (.7152*l2G) + (.0722*l2B); //using linearised R, G, and B value
//and L2 is the lower value.
l1 = l1 + .05;
l2 = l2 + .05;
if (l1 < l2)
{
temp = l1;
l1 = l2;
l2 = temp;
}
l1 = l1/l2;
l1 = l1.toFixed(2);
document.getElementById('contrastRatio').value = l1.toString();
document.getElementById('contrastRatio1').value = 1;
document.getElementById('sample').style.color = "#"+ color1;
document.getElementById('sample').style.backgroundColor = "#"+color2;
document.getElementById('sample').style.borderColor = "#" + color1;
document.getElementById('sample').style.borderWidth = "thin";
document.getElementById('sample').style.borderStyle = "solid";
if (txtSizeOp == 1) {
document.getElementById('sample').style.fontSize = "18pt";
}
else { //0
document.getElementById('sample').style.fontSize = "12pt";
document.getElementById('sample').style.fontWeight = "normal";
}
if (l1 >= 7) {
text = "Section 508 (revised 2017) and WCAG 2 - Level AAA Conformant";
}
else if (l1 >= 4.5) {
text = "Section 508 (revised 2017) and WCAG 2 - Level AA Conformant";
}
else if (l1 >=3) {
if (txtSizeOp == 1) {
text = "Section 508 (revised 2017) and WCAG 2 - Level AA Conformant";
}
else {
text = "Not conformant to Secton 508 (revised) and not conformant at WCAG level AA. Note: there are not any contrast requirements in WCAG 2 - Level A. Thus, content may meet these requirements independent of the color palette used.";
}
}
else {
text = "Not conformant to Section 508 (revised) and not conformant at WCAG level AA. Note: there are not any contrast requirements in WCAG 2 - Level A. Thus, content may meet these requirements independent of the color palette used.";
}
document.getElementById('compliance').textContent = text;
} // check function
// ****************************************************************
function setup() {
if (msie()) {
document.getElementById("fgc").style.display = "none";
document.getElementById("bgc").style.display = "none";
}
}
// ****************************************************************
// Is the IE browser in use?
// *****************************************************************
function msie() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
var trident = ua.indexOf('Trident/');
if ((trident > 0) || (msie > 0) )
return true;
else
return false;
}
// *******************************************************
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (parseInt(r,10) << 16) + (parseInt(g,10) << 8) + parseInt(b,10)).toString(16).slice(1);
}
// *******************************************************
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
// Check for query string parameters
checkQSP();
</script>
</head>
<body onload="check(window.event); setup();">
<div id="instructions">
<p>
<span id="errtxt"></span>
Enter a foreground and background color in #hex or RGB integer format to check contrast for accessibility (pressing enter or tab after changing the value will update the color selection).</p>
</div>
<form onchange="check(event);">
<!-- foreground -->
<div class="group">
<div class="topgroup">
<label for="fg">Foreground color in hex:</label>
<input id="fg" type="text" value="#000000" size="7" pattern="^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" title="Enter # followed by a six digit hex number between #000000 and #ffffff">
<input aria-label="Forground color picker" id="fgc" type="color" value="#000000">
</div>
<div class="slidegroup">
<label for="fgr" class="sliderlbl">Red:</label>
<input id="fgr" type="range" min="0" max="255" value="0">
<input aria-label="foreground red" id="fgri" size="3" type="number" min="0" max="255" value="0">
</div>
<div class="slidegroup">
<label for="fgg" class="sliderlbl">Green:</label>
<input id="fgg" type="range" min="0" max="255" value="0">
<input aria-label="foreground green" id="fggi" size="3" type="number" min="0" max="255" value="0">
</div>
<div class="slidegroup">
<label for="fgb" class="sliderlbl">Blue:</label>
<input id="fgb" type="range" min="0" max="255" value="0">
<input aria-label="foreground blue" id="fgbi" size="3" type="number" min="0" max="255" value="0">
</div>
</div>
<!-- background -->
<div class="group">
<div class="topgroup">
<label for="bg">Background color in hex:</label>
<input id="bg" type="text" value="#FFFFFF" size="7" >
<input aria-label="Background color picker" id="bgc" type="color" value="#ffffff">
</div>
<div class="slidegroup">
<label for="bgr" class="sliderlbl">Red:</label>
<input id="bgr" type="range" min="0" max="255" value="255">
<input aria-label="background red" id="bgri" size="3" type="number" min="0" max="255" value="255">
</div>
<div class="slidegroup">
<label for="bgg" class="sliderlbl">Green:</label>
<input id="bgg" type="range" min="0" max="255" value="255">
<input aria-label="background green" id="bggi" size="3" type="number" min="0" max="255" value="255">
</div>
<div class="slidegroup">
<label for="bgb" class="sliderlbl">Blue:</label>
<input id="bgb" type="range" min="0" max="255" value="255">
<input aria-label="background blue" id="bgbi" size="3" type="number" min="0" max="255" value="255">
</div>
</div>
<div class="group">
<label for="fontSize">Text size:</label>
<select id="fontSize">
<option selected="selected">Less than 18 points</option>
<option>18 Points or larger or 14 points and bold</option>
</select>
</div>
</form>
<div id="right">
<div id="samplegroup">
Sample: <span id="sample">This is sample text</span>
</div>
<div>
<!-- right -->
<label for="contrastRatio">Contrast ratio</label>
<input title="contrast ratio" style="width:3em;" id="contrastRatio" type="text" maxlength="5" readonly /> :
<output tabindex="0" title="to" style="width:1em;" id="contrastRatio1"> 1</output>
</div>
<div>
<h2>Compliance Level</h2>
<div id="compliance">
N/A
</div>
</div>
</div>
<div id="footer">
<h2>Note</h2>
<p>The compliance rate shown above is the highest compliance rate met. The WCAG 2 level AA and Section 508 refresh
compliance level is based on achieving a contrast ratio of 3:1 for text with a size of 18 points (14 points if bolded) or
larger or 4.5:1 for text with a size less than 18 points. The WCAG 2 level AAA compliance level is meet when a contrast
ration of 7:1 is achieved for text less than 18 points and 4.5:1 for text 18 points (14 points if bolded) or larger.</p>
<p>The algorithm used in this tool and subsequent results are based on the text luminosity algorithm recommended in the WCAG 2
guidelines to test for contrast. This tool is for general assessment purposes only and not a guarantee of conformance/compliance.</p>
</div>
</body>
</html>