Skip to content

Commit

Permalink
Display the mean contrast ratio of a color palette
Browse files Browse the repository at this point in the history
  • Loading branch information
darekkay committed Nov 23, 2020
1 parent 896d22f commit 7f44929
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- :rocket: Ignore invalid colors.
- :rocket: Don't calculate unnecessary contrast ratios.
- :rocket: Display the mean contrast ratio of a color palette.
- :hammer: Replace TravisCI with GitHub Actions.

## [1.1.0] - 2020-10-28
Expand Down
1 change: 1 addition & 0 deletions examples/all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

../bin/a11y-contrast.js black-white.json
../bin/a11y-contrast.js ibm-v1.json
../bin/a11y-contrast.js ibm-v2.1.json --min-ratio-3=40 --min-ratio-4.5=50 --min-ratio-7=70
../bin/a11y-contrast.js open-color.json
Expand Down
8 changes: 8 additions & 0 deletions examples/black-white.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"black": {
"black-100": "#000000"
},
"white": {
"white-0": "#ffffff"
}
}
6 changes: 6 additions & 0 deletions examples/uswds.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"black": {
"black-100": "#000000"
},
"white": {
"white-0": "#ffffff"
},
"blue": {
"blue-5": "#eff6fb",
"blue-10": "#d9e8f6",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"prepublishOnly": "npm run ci",
"start": "node ./bin/a11y-contrast.js examples/uswds.json",
"start": "node ./bin/a11y-contrast.js examples/black-white.json",
"run-all": "cd examples && sh all.sh",
"test": "jest"
}
}
26 changes: 25 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const verifyContrastRatio = (palette, magicNumber) => {

palette.forEach((color1) => {
palette.forEach((color2) => {
if (color1.grade > color2.grade) {
if (color1.grade > color2.grade || color1 === color2) {
return; // don't compare same colors to each other
}

Expand Down Expand Up @@ -94,6 +94,25 @@ const removeInvalidColors = (palette) => {
});
};

const calculateMeanContrastRatio = (palette) => {
let mean = 0;
let count = 0;

palette.forEach((color1) => {
palette.forEach((color2) => {
if (color1.grade > color2.grade || color1 === color2) {
return; // don't compare same colors to each other
}

const contrastRatio = color1.onecolorValue.contrast(color2.onecolorValue);
count++;
mean += (contrastRatio - mean) / count;
});
});

return Math.round(mean * 1000) / 1000;
};

const run = (colors, config) => {
logger.info(`${green("Analyzing")}: ${config.file}`);

Expand All @@ -103,6 +122,11 @@ const run = (colors, config) => {
const magicNumbers = calculateMagicNumbers(validPalette);

logger.log(`${green("Colors:")} ${validPalette.length}`);
logger.log(
`${green("Mean contrast ratio:")} ${calculateMeanContrastRatio(
validPalette
)}`
);
logger.log(green("Magic numbers:"));
Object.entries(magicNumbers).forEach(([grade, magicNumber]) => {
logger.log(
Expand Down

0 comments on commit 7f44929

Please sign in to comment.