|
1 | 1 | // An input validator for the color picker. Points out low-contrast tag color |
2 | 2 | // choices. |
3 | | -const input = document.getElementById("id_color"); |
4 | | -input.addEventListener("input", (event) => { |
5 | | - // Don't do anything if the color code doesn't pass default validity. |
6 | | - input.setCustomValidity(""); |
7 | | - if (!input.validity.valid) { |
8 | | - return; |
9 | | - } |
| 3 | +const inputs = document.getElementsByClassName("color-picker"); |
| 4 | +for (let i = 0; i < inputs.length; i++) { |
| 5 | + inputs[i].addEventListener("change", (event) => { |
| 6 | + // Don't do anything if the color code doesn't pass default validity. |
| 7 | + const element = event.target; |
| 8 | + element.setCustomValidity(""); |
| 9 | + if (!element.validity.valid) { |
| 10 | + return; |
| 11 | + } |
10 | 12 |
|
11 | | - // Break the #rrggbb color code into RGB components. |
12 | | - color = parseInt(input.value.substr(1), 16); |
13 | | - red = ((color & 0xFF0000) >> 16) / 255.; |
14 | | - green = ((color & 0x00FF00) >> 8) / 255.; |
15 | | - blue = (color & 0x0000FF) / 255.; |
| 13 | + // Break the #rrggbb color code into RGB components. |
| 14 | + color = parseInt(element.value.substr(1), 16); |
| 15 | + red = ((color & 0xff0000) >> 16) / 255; |
| 16 | + green = ((color & 0x00ff00) >> 8) / 255; |
| 17 | + blue = (color & 0x0000ff) / 255; |
16 | 18 |
|
17 | | - // Compare the contrast ratio against white. All the magic math comes from |
18 | | - // Web Content Accessibility Guidelines (WCAG) 2.2, Technique G18: |
19 | | - // |
20 | | - // https://www.w3.org/WAI/WCAG22/Techniques/general/G18.html |
21 | | - // |
22 | | - function l(val) { |
23 | | - if (val <= 0.04045) { |
24 | | - return val / 12.92; |
25 | | - } |
26 | | - return ((val + 0.055) / 1.055) ** 2.4; |
27 | | - } |
| 19 | + // Compare the contrast ratio against white. All the magic math comes from |
| 20 | + // Web Content Accessibility Guidelines (WCAG) 2.2, Technique G18: |
| 21 | + // |
| 22 | + // https://www.w3.org/WAI/WCAG22/Techniques/general/G18.html |
| 23 | + // |
| 24 | + function l(val) { |
| 25 | + if (val <= 0.04045) { |
| 26 | + return val / 12.92; |
| 27 | + } |
| 28 | + return ((val + 0.055) / 1.055) ** 2.4; |
| 29 | + } |
28 | 30 |
|
29 | | - lum = 0.2126 * l(red) + 0.7152 * l(green) + 0.0722 * l(blue); |
30 | | - contrast = (1 + 0.05) / (lum + 0.05); |
| 31 | + lum = 0.2126 * l(red) + 0.7152 * l(green) + 0.0722 * l(blue); |
| 32 | + contrast = (1 + 0.05) / (lum + 0.05); |
31 | 33 |
|
32 | | - // Complain if we're below WCAG 2.2 recommendations. |
33 | | - if (contrast < 4.5) { |
34 | | - input.setCustomValidity( |
35 | | - "Consider choosing a darker color. " |
36 | | - + "(Tag text is small and white.)\n\n" |
37 | | - + "Contrast ratio: " + (Math.trunc(contrast * 10) / 10) + " (< 4.5)" |
38 | | - ); |
| 34 | + // Complain if we're below WCAG 2.2 recommendations. |
| 35 | + if (contrast < 4.5) { |
| 36 | + element.setCustomValidity( |
| 37 | + "Consider choosing a darker color. " + |
| 38 | + "(Tag text is small and white.)\n\n" + |
| 39 | + "Contrast ratio: " + |
| 40 | + Math.trunc(contrast * 10) / 10 + |
| 41 | + " (< 4.5)", |
| 42 | + ); |
39 | 43 |
|
40 | | - // The admin form uses novalidate, so manually display the browser's |
41 | | - // validity popup. (The user can still ignore it if desired.) |
42 | | - input.reportValidity(); |
43 | | - } |
44 | | -}); |
| 44 | + // The admin form uses novalidate, so manually display the browser's |
| 45 | + // validity popup. (The user can still ignore it if desired.) |
| 46 | + element.reportValidity(); |
| 47 | + } |
| 48 | + }); |
| 49 | +} |
0 commit comments