-
Notifications
You must be signed in to change notification settings - Fork 243
/
1_4_3_Contrast.js
277 lines (237 loc) · 11.1 KB
/
1_4_3_Contrast.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
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
/**
* +--------------------------------------------------------------------+
* | This HTML_CodeSniffer file is Copyright (c) |
* | Squiz Australia Pty Ltd ABN 53 131 581 247 |
* +--------------------------------------------------------------------+
* | IMPORTANT: Your use of this Software is subject to the terms of |
* | the Licence provided in the file licence.txt. If you cannot find |
* | this file please contact Squiz (www.squiz.com.au) so we may |
* | provide you a copy. |
* +--------------------------------------------------------------------+
*
*/
var HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
testContrastRatio: function (top, minContrast, minLargeContrast)
{
var startDate = new Date();
var count = 0;
var xcount = 0;
var failures = [];
if (!top.ownerDocument) {
var toProcess = [top.getElementsByTagName('body')[0]];
} else {
var toProcess = [top];
}
while (toProcess.length > 0) {
var node = toProcess.shift();
// This is an element.
if ((node.nodeType === 1) && (HTMLCS.util.isHidden(node) === false) && (HTMLCS.util.isDisabled(node) === false)) {
var processNode = false;
for (var i = 0; i < node.childNodes.length; i++) {
// Load up new nodes, but also only process this node when
// there are direct text elements.
if (node.childNodes[i].nodeType === 1) {
toProcess.push(node.childNodes[i]);
} else if (node.childNodes[i].nodeType === 3) {
if (HTMLCS.util.trim(node.childNodes[i].nodeValue) !== '') {
processNode = true;
}
}
}
if (processNode === true) {
var style = HTMLCS.util.style(node);
if (style) {
var bgColour = style.backgroundColor;
var foreColour = style.color;
var bgElement = node;
var hasBgImg = false;
var isAbsolute = false;
if (style.backgroundImage !== 'none') {
hasBgImg = true;
}
if (style.position == 'absolute') {
isAbsolute = true;
}
var parent = node.parentNode;
// Calculate font size. Note that CSS 2.1 fixes a reference pixel
// as 96 dpi (hence "pixel ratio" workarounds for Hi-DPI devices)
// so this calculation should be safe.
var fontSize = parseInt(style.fontSize, 10) * (72 / 96);
var minLargeSize = 18;
if ((style.fontWeight === 'bold') || (parseInt(style.fontWeight, 10) >= 600)) {
var minLargeSize = 14;
}
var reqRatio = minContrast;
if (fontSize >= minLargeSize) {
reqRatio = minLargeContrast;
}
// Check for a solid background colour.
while ((bgColour === 'transparent') || (bgColour === 'rgba(0, 0, 0, 0)')) {
if ((!parent) || (!parent.ownerDocument)) {
break;
}
var parentStyle = HTMLCS.util.style(parent);
var bgColour = parentStyle.backgroundColor;
if (parentStyle.backgroundImage !== 'none') {
hasBgImg = true;
}
if (parentStyle.position == 'absolute') {
isAbsolute = true;
}
parent = parent.parentNode;
}//end while
if (hasBgImg === true) {
// If we have a background image, skip the contrast ratio checks,
// and push a warning instead.
failures.push({
element: node,
colour: style.color,
bgColour: undefined,
value: undefined,
required: reqRatio,
hasBgImage: true
});
continue;
} else if (isAbsolute === true) {
failures.push({
element: node,
colour: foreColour,
bgColour: undefined,
value: undefined,
required: reqRatio,
isAbsolute: true
});
} else if ((bgColour === 'transparent') || (bgColour === 'rgba(0, 0, 0, 0)')) {
// If the background colour is still transparent, this is probably
// a fragment with which we cannot reliably make a statement about
// contrast ratio. Skip the element.
continue;
}
var contrastRatio = HTMLCS.util.contrastRatio(bgColour, style.color);
if (contrastRatio < reqRatio) {
var recommendation = this.recommendColour(bgColour, style.color, reqRatio);
failures.push({
element: node,
colour: style.color,
bgColour: bgColour,
value: contrastRatio,
required: reqRatio,
recommendation: recommendation
});
}//end if
}//end if
}//end if
}//end if
}//end while
return failures;
},
recommendColour: function(back, fore, target) {
// Canonicalise the colours.
var fore = HTMLCS.util.RGBtoColourStr(HTMLCS.util.colourStrToRGB(fore));
var back = HTMLCS.util.RGBtoColourStr(HTMLCS.util.colourStrToRGB(back));
var cr = HTMLCS.util.contrastRatio(fore, back);
var foreDiff = Math.abs(HTMLCS.util.relativeLum(fore) - 0.5);
var backDiff = Math.abs(HTMLCS.util.relativeLum(back) - 0.5);
var recommendation = null;
if (cr < target) {
// Work out which colour has more room to move.
// If they are the same, prefer changing the foreground colour.
var multiplier = (1 + 1 / 400);
if (foreDiff <= backDiff) {
var change = 'back';
var newCol = back;
if (HTMLCS.util.relativeLum(back) < 0.5) {
var multiplier = (1 / multiplier);
}
} else {
var change = 'fore';
var newCol = fore;
if (HTMLCS.util.relativeLum(fore) < 0.5) {
var multiplier = (1 / multiplier);
}
}
var hsv = HTMLCS.util.sRGBtoHSV(newCol);
var chroma = hsv.saturation * hsv.value;
var newFore = fore;
var newBack = back;
var changed = false;
var i = 0;
while (cr < target) {
if ((newCol === '#fff') || (newCol === '#000')) {
// Couldn't go far enough. Reset and try the other colour.
if (changed === true) {
// We've already switched colours, so we have to start
// winding back the other colour.
if (change === 'fore') {
var oldBack = newBack;
var j = 1;
while (newBack === oldBack) {
var newBack = this.multiplyColour(newBack, Math.pow(1 / multiplier, j));
j++;
}
} else {
var oldFore = newFore;
var j = 1;
while (newFore === oldFore) {
var newFore = this.multiplyColour(newFore, Math.pow(1 / multiplier, j));
j++;
}
}
} else {
newFore = fore;
newBack = back;
multiplier = 1 / multiplier;
if (change === 'fore') {
change = 'back';
var hsv = back;
} else {
change = 'fore';
var hsv = fore;
}
hsv = HTMLCS.util.sRGBtoHSV(hsv);
chroma = hsv.saturation * hsv.value;
changed = true;
}
}
i++;
var newCol = HTMLCS.util.HSVtosRGB(hsv);
var newCol = this.multiplyColour(newCol, Math.pow(multiplier, i));
if (change === 'fore') {
var newFore = newCol;
} else {
var newBack = newCol;
}
var cr = HTMLCS.util.contrastRatio(newFore, newBack);
}//end while
recommendation = {
fore: {
from: fore,
to: newFore
},
back: {
from: back,
to: newBack
}
}
}//end if
return recommendation;
},
multiplyColour: function(colour, multiplier) {
var hsvColour = HTMLCS.util.sRGBtoHSV(colour);
var chroma = hsvColour.saturation * hsvColour.value;
// If we are starting from black, start it from #010101 instead.
if (hsvColour.value === 0) {
hsvColour.value = (1 / 255);
}
hsvColour.value = hsvColour.value * multiplier;
if (hsvColour.value === 0) {
hsvColour.saturation = 0;
} else {
hsvColour.saturation = chroma / hsvColour.value;
}
hsvColour.value = Math.min(1, hsvColour.value);
hsvColour.saturation = Math.min(1, hsvColour.saturation);
var newColour = HTMLCS.util.RGBtoColourStr(HTMLCS.util.HSVtosRGB(hsvColour));
return newColour;
}
}