Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added message about expected contrast ratio (#381) #562

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var data = {
contrastRatio: cr ? truncatedResult : undefined,
fontSize: (fontSize * 72 / 96).toFixed(1) + 'pt',
fontWeight: bold ? 'bold' : 'normal',
missingData: missing
missingData: missing,
expectedContrastRatio: cr.expectedContrastRatio + ':1'
};

this.data(data);
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/color/color-contrast.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"impact": "serious",
"messages": {
"pass": "Element has sufficient color contrast of {{=it.data.contrastRatio}}",
"fail": "Element has insufficient color contrast of {{=it.data.contrastRatio}} (foreground color: {{=it.data.fgColor}}, background color: {{=it.data.bgColor}}, font size: {{=it.data.fontSize}}, font weight: {{=it.data.fontWeight}})",
"fail": "Element has insufficient color contrast of {{=it.data.contrastRatio}} (foreground color: {{=it.data.fgColor}}, background color: {{=it.data.bgColor}}, font size: {{=it.data.fontSize}}, font weight: {{=it.data.fontWeight}}). Expected contrast ratio of {{=it.data.expectedContrastRatio}}",
"incomplete": {
"bgImage": "Element's background color could not be determined due to a background image",
"bgGradient": "Element's background color could not be determined due to a background gradient",
Expand Down
8 changes: 5 additions & 3 deletions lib/commons/color/contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ color.getContrast = function (bgColor, fgColor) {
* @param {Color} fgcolor Foreground color
* @param {number} fontSize Font size of text, in pixels
* @param {boolean} isBold Whether the text is bold
* @return {{isValid: boolean, contrastRatio: number}}
* @return {{isValid: boolean, contrastRatio: number, expectedContrastRatio: number}}
*/
color.hasValidContrastRatio = function (bg, fg, fontSize, isBold) {
var contrast = color.getContrast(bg, fg);
var isSmallFont = (isBold && (Math.ceil(fontSize * 72) / 96) < 14) || (!isBold && (Math.ceil(fontSize * 72) / 96) < 18);
var expectedContrastRatio = isSmallFont ? 4.5 : 3;

return {
isValid: (isSmallFont && contrast >= 4.5) || (!isSmallFont && contrast >= 3),
contrastRatio: contrast
isValid: contrast > expectedContrastRatio,
contrastRatio: contrast,
expectedContrastRatio: expectedContrastRatio
};

};
8 changes: 8 additions & 0 deletions test/commons/color/contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,26 @@ describe('color.Color', function () {

assert.isTrue(axe.commons.color.hasValidContrastRatio(black, white, 8, false).isValid);
assert.isTrue(axe.commons.color.hasValidContrastRatio(black, white, 8, false).contrastRatio > 4.5);
assert.isTrue(axe.commons.color.hasValidContrastRatio(black, white, 8, false).expectedContrastRatio === 4.5);

assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 24, false).isValid);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 24, false).contrastRatio > 3);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 24, false).expectedContrastRatio === 3);

assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 20, true).isValid);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 20, true).contrastRatio > 3);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 20, true).expectedContrastRatio === 3);

assert.isFalse(axe.commons.color.hasValidContrastRatio(white, gray, 8, false).isValid);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 8, false).contrastRatio < 4.5);
assert.isTrue(axe.commons.color.hasValidContrastRatio(white, gray, 8, false).expectedContrastRatio === 4.5);
});

it('should count 1-1 ratios as visually hidden', function () {
var black = new axe.commons.color.Color(0, 0, 0, 1);

assert.isFalse(axe.commons.color.hasValidContrastRatio(black, black, 16, true).isValid);
assert.isTrue(axe.commons.color.hasValidContrastRatio(black, black, 16, true).contrastRatio === 1);
assert.isTrue(axe.commons.color.hasValidContrastRatio(black, black, 16, true).expectedContrastRatio === 4.5);
});
});