forked from siege-media/contrast-ratio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrast-ratio.js
286 lines (213 loc) · 6.75 KB
/
contrast-ratio.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
278
279
280
281
282
283
284
285
286
function $(expr, con) {
return typeof expr === 'string'? (con || document).querySelector(expr) : expr;
}
function $$(expr, con) {
return Array.prototype.slice.call((con || document).querySelectorAll(expr));
}
// Make each ID a global variable
// Many browsers do this anyway (it’s in the HTML5 spec), so it ensures consistency
$$('[id]').forEach(function(element) { window[element.id] = element; });
var messages = {
'semitransparent': 'The background is semi-transparent, so the contrast ratio cannot be precise. Depending on what’s going to be underneath, it could be any of the following:',
'fail': 'Fails WCAG 2.0 :-(',
'aa-large': 'Passes AA for large text (above 18pt or bold above 14pt)',
'aa': 'Passes AA level for any size text and AAA for large text (above 18pt or bold above 14pt)',
'aaa': 'Passes AAA level for any size text'
};
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = canvas.height = 16;
document.body.appendChild(canvas);
incrementable.onload = function() {
if (window.Incrementable) {
new Incrementable(background);
new Incrementable(foreground);
}
};
if (window.Incrementable) {
incrementable.onload();
}
var output = $('output');
var levels = {
'fail': {
range: [0, 3],
color: 'hsl(0, 100%, 40%)'
},
'aa-large': {
range: [3, 4.5],
color: 'hsl(40, 100%, 45%)'
},
'aa': {
range: [4.5, 7],
color: 'hsl(80, 60%, 45%)'
},
'aaa': {
range: [7, 22],
color: 'hsl(95, 60%, 41%)'
}
};
function rangeIntersect(min, max, upper, lower) {
return (max < upper? max : upper) - (lower < min? min : lower);
}
function updateLuminance(input) {
input.title = 'Relative luminance: ';
var color = input.color;
if (input.color.alpha < 1) {
input.title += color.overlayOn(Color.BLACK).luminance + ' - ' + color.overlayOn(Color.WHITE).luminance;
}
else {
input.title += color.luminance;
}
}
function update() {
if (foreground.color && background.color) {
if (foreground.value !== foreground.defaultValue || background.value !== background.defaultValue) {
window.onhashchange = null;
location.hash = '#' + encodeURIComponent(foreground.value) + '-on-' + encodeURIComponent(background.value);
setTimeout(function() {
window.onhashchange = hashchange;
}, 10);
}
var contrast = background.color.contrast(foreground.color);
updateLuminance(background);
updateLuminance(foreground);
var min = contrast.min,
max = contrast.max,
range = max - min,
classes = [], percentages = [];
for (var level in levels) {
var bounds = levels[level].range,
lower = bounds[0],
upper = bounds[1];
if (min < upper && max >= lower) {
classes.push(level);
percentages.push({
level: level,
percentage: 100 * rangeIntersect(min, max, upper, lower) / range
});
}
}
$('strong', output).textContent = contrast.ratio;
var error = $('.error', output);
if (contrast.error) {
error.textContent = '±' + contrast.error;
error.title = min + ' - ' + max;
}
else {
error.textContent = '';
error.title = '';
}
if (classes.length <= 1) {
results.textContent = messages[classes[0]];
output.style.backgroundImage = '';
output.style.backgroundColor = levels[classes[0]].color;
}
else {
var fragment = document.createDocumentFragment();
var p = document.createElement('p');
p.textContent = messages.semitransparent;
fragment.appendChild(p);
var ul = document.createElement('ul');
var message = '<p></p><ul>';
for (var i=0; i<classes.length; i++) {
var li = document.createElement('li');
li.textContent = messages[classes[i]];
ul.appendChild(li);
}
fragment.appendChild(ul);
results.textContent = '';
results.appendChild(fragment);
// Create gradient illustrating levels
var stops = [], previousPercentage = 0;
for (var i=0; i < 2 * percentages.length; i++) {
var info = percentages[i % percentages.length];
var level = info.level;
var color = levels[level].color,
percentage = previousPercentage + info.percentage / 2;
stops.push(color + ' ' + previousPercentage + '%', color + ' ' + percentage + '%');
previousPercentage = percentage;
}
if (PrefixFree.functions.indexOf('linear-gradient') > -1) {
// Prefixed implementation
var gradient = 'linear-gradient(-45deg, ' + stops.join(', ') + ')';
output.style.backgroundImage = PrefixFree.prefix + gradient;
}
else {
var gradient = 'linear-gradient(135deg, ' + stops.join(', ') + ')';
output.style.backgroundImage = gradient;
}
}
output.className = classes.join(' ');
ctx.clearRect(0, 0, 16, 16);
ctx.fillStyle = background.color + '';
ctx.fillRect(0, 0, 8, 16);
ctx.fillStyle = foreground.color + '';
ctx.fillRect(8, 0, 8, 16);
$('link[rel="shortcut icon"]').setAttribute('href', canvas.toDataURL());
}
}
function colorChanged(input) {
input.style.width = input.value.length * .56 + 'em';
input.style.width = input.value.length + 'ch';
var isForeground = input == foreground;
var display = isForeground? foregroundDisplay : backgroundDisplay;
var previousColor = getComputedStyle(display).backgroundColor;
// Match a 6 digit hex code, add a hash in front.
if(input.value.match(/^[0-9a-f]{6}$/i)) {
input.value = '#' + input.value;
}
display.style.background = input.value;
var color = getComputedStyle(display).backgroundColor;
if (color && input.value && (color !== previousColor || color === 'transparent' || color === 'rgba(0, 0, 0, 0)')) {
// Valid & different color
if (isForeground) {
backgroundDisplay.style.color = input.value;
}
input.color = new Color(color);
return true;
}
return false;
}
function hashchange() {
if (location.hash) {
var colors = location.hash.slice(1).split('-on-');
foreground.value = decodeURIComponent(colors[0]);
background.value = decodeURIComponent(colors[1]);
}
else {
foreground.value = foreground.defaultValue;
background.value = background.defaultValue;
}
background.oninput();
foreground.oninput();
}
background.oninput =
foreground.oninput = function() {
var valid = colorChanged(this);
if (valid) {
update();
}
};
swap.onclick = function() {
var backgroundColor = background.value;
background.value = foreground.value;
foreground.value = backgroundColor;
colorChanged(background);
colorChanged(foreground);
update();
};
window.encodeURIComponent = (function(){
var encodeURIComponent = window.encodeURIComponent;
return function (str) {
return encodeURIComponent(str).replace(/[()]/g, function ($0) {
return escape($0);
});
};
})();
window.decodeURIComponent = (function(){
var decodeURIComponent = window.decodeURIComponent;
return function (str) {
return str.search(/%[\da-f]/i) > -1? decodeURIComponent(str) : str;
};
})();
(onhashchange = hashchange)();