Skip to content

Commit c37ef5b

Browse files
authored
(enh) improve default theme accessibility (#3402)
* first pass at accessibility report in checkTheme * resolve CSS color names when necessary * (enh) default Dark theme is WCAG AAA wrt contrast * (enh) improve accessibility of default theme
1 parent 4ddfbe4 commit c37ef5b

File tree

6 files changed

+150
-10
lines changed

6 files changed

+150
-10
lines changed

CHANGES.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## Version 11.3.2 (most likely)
1+
## Version 11.4 (most likely)
2+
3+
Themes:
4+
5+
- `Default` is now much closer WCAG AA (contrast) (#3402) [Josh Goebel]
6+
- `Dark` now meets WCAG AA (contrast) (#3402) [Josh Goebel]
7+
8+
These changes should be for the better and should not be super noticeable but if you're super picky about your colors you may want to intervene here or copy over the older themes from 11.3 or prior.
29

310
Grammars:
411

package-lock.json

Lines changed: 54 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"colors": "^1.1.2",
6666
"commander": "8.2",
6767
"css": "^3.0.0",
68+
"css-color-names": "^1.0.1",
6869
"deep-freeze-es6": "^1.4.1",
6970
"del": "^6.0.0",
7071
"dependency-resolver": "^2.0.1",
@@ -84,6 +85,7 @@
8485
"should": "^13.2.3",
8586
"terser": "^5.7.0",
8687
"tiny-worker": "^2.3.0",
87-
"typescript": "^4.4.4"
88+
"typescript": "^4.4.4",
89+
"wcag-contrast": "^3.0.0"
8890
}
8991
}

src/styles/dark.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Dark style from softwaremaniacs.org (c) Ivan Sagalaev <Maniac@SoftwareManiacs.Or
66

77
.hljs {
88
color: #ddd;
9-
background: #444;
9+
background: #303030;
1010
}
1111

1212
.hljs-keyword,
@@ -41,7 +41,7 @@ Dark style from softwaremaniacs.org (c) Ivan Sagalaev <Maniac@SoftwareManiacs.Or
4141
.hljs-quote,
4242
.hljs-deletion,
4343
.hljs-meta {
44-
color: #777;
44+
color: #979797;
4545
}
4646

4747
.hljs-keyword,

src/styles/default.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ code.hljs {
2626
/* end baseline CSS */
2727

2828
.hljs {
29-
background: #F0F0F0;
29+
background: #F3F3F3;
3030
color: #444;
3131
}
3232

@@ -43,7 +43,7 @@ code.hljs {
4343
.hljs-params {}
4444

4545
.hljs-comment {
46-
color: #888888;
46+
color: #697070;
4747
}
4848
.hljs-tag,
4949
.hljs-punctuation {
@@ -94,13 +94,13 @@ code.hljs {
9494
.hljs-selector-attr,
9595
.hljs-operator,
9696
.hljs-selector-pseudo {
97-
color: #BC6060;
97+
color: #ab5656;
9898
}
9999

100100
/* Language color: hue: 90; */
101101

102102
.hljs-literal {
103-
color: #78A960;
103+
color: #695;
104104
}
105105

106106
.hljs-built_in,
@@ -118,7 +118,7 @@ code.hljs {
118118
}
119119

120120
.hljs-meta .hljs-string {
121-
color: #4d99bf;
121+
color: #38a;
122122
}
123123

124124

tools/checkTheme.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const fs = require("fs");
44
const css = require("css");
5+
const wcagContrast = require("wcag-contrast");
6+
const Table = require('cli-table');
7+
const csscolors = require('css-color-names');
58
require("colors");
69

710
const CODE = {
@@ -184,6 +187,79 @@ function check_group(group, rules) {
184187
}
185188
}
186189

190+
const round2 = (x) => Math.round(x*100)/100;
191+
192+
class CSSRule {
193+
constructor(rule, body) {
194+
this.rule = rule;
195+
if (rule.declarations) {
196+
this.bg = rule.declarations.find(x => x.property =="background")?.value;
197+
this.fg = rule.declarations.find(x => x.property =="color")?.value;
198+
199+
if (this.bg) {
200+
this.bg = csscolors[this.bg] || this.bg;
201+
}
202+
if (this.fg) {
203+
this.fg = csscolors[this.fg] || this.fg;
204+
}
205+
206+
// inherit from body if we're missing fg or bg
207+
if (this.hasColor) {
208+
if (!this.bg) this.bg = body.background;
209+
if (!this.fg) this.fg = body.foreground;
210+
}
211+
}
212+
}
213+
get background() {
214+
return this.bg
215+
}
216+
217+
get foreground() {
218+
return this.fg
219+
}
220+
get hasColor() {
221+
if (!this.rule.declarations) return false;
222+
return this.fg || this.bg;
223+
}
224+
toString() {
225+
return ` ${this.foreground} on ${this.background}`
226+
}
227+
228+
contrastRatio() {
229+
if (!this.foreground) return "unknown (no fg)"
230+
if (!this.background) return "unknown (no bg)"
231+
return round2(wcagContrast.hex(this.foreground, this.background));
232+
}
233+
}
234+
235+
function contrast_report(rules) {
236+
console.log("Accessibility Report".yellow);
237+
238+
var hljs = rules.find (x => x.selectors && x.selectors.includes(".hljs"));
239+
var body = new CSSRule(hljs);
240+
const table = new Table({
241+
chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''},
242+
head: ['ratio', 'selector', 'fg', 'bg'],
243+
colWidths: [7, 40, 10, 10],
244+
style: {
245+
head: ['grey']
246+
}
247+
});
248+
249+
rules.forEach(rule => {
250+
var color = new CSSRule(rule, body);
251+
if (!color.hasColor) return;
252+
table.push([
253+
color.contrastRatio(),
254+
rule.selectors,
255+
color.foreground,
256+
color.background
257+
])
258+
// console.log(r.selectors[0], color.contrastRatio(), color.toString());
259+
})
260+
console.log(table.toString())
261+
}
262+
187263
function validate(data) {
188264
const rules = data.stylesheet.rules;
189265

@@ -195,6 +271,8 @@ function validate(data) {
195271
check_group(CODE, rules);
196272
check_group(OTHER, rules);
197273
check_group(HIGH_FIDELITY, rules);
274+
275+
contrast_report(rules);
198276
}
199277

200278
process.argv.shift();

0 commit comments

Comments
 (0)