Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
33 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
var rgb = require('rgb'); | ||
var wcag = require('wcag-contrast'); | ||
var isBlank = require('is-blank'); | ||
var isNamedCssColor = require('is-named-css-color'); | ||
var cssColorNames = require('css-color-names'); | ||
var rgb = require('rgb') | ||
var wcag = require('wcag-contrast') | ||
var isBlank = require('is-blank') | ||
var isNamedCssColor = require('is-named-css-color') | ||
var cssColorNames = require('css-color-names') | ||
|
||
module.exports.ratio = ratio; | ||
module.exports.score = score; | ||
module.exports.isAccessible = isAccessible; | ||
module.exports.isNotTransparent = isNotTransparent; | ||
module.exports.ratio = ratio | ||
module.exports.score = score | ||
module.exports.isAccessible = isAccessible | ||
module.exports.isNotTransparent = isNotTransparent | ||
|
||
function ratio(colorOne, colorTwo, options) { | ||
colorOne = getRgbTriplet(colorOne, options); | ||
colorTwo = getRgbTriplet(colorTwo, options); | ||
colorOne = getRgbTriplet(colorOne, options) | ||
colorTwo = getRgbTriplet(colorTwo, options) | ||
|
||
return wcag.rgb(colorOne, colorTwo); | ||
return wcag.rgb(colorOne, colorTwo) | ||
} | ||
|
||
function score(colorOne, colorTwo, options) { | ||
var wcagScore = wcag.score(ratio(colorOne, colorTwo, options)); | ||
var wcagScore = wcag.score(ratio(colorOne, colorTwo, options)) | ||
|
||
if (isBlank(wcagScore)) { | ||
return 'F'; | ||
return 'F' | ||
} else { | ||
return wcagScore; | ||
return wcagScore | ||
} | ||
} | ||
|
||
function isAccessible(colorOne, colorTwo, options) { | ||
return ratio(colorOne, colorTwo, options) > 4.5; | ||
return ratio(colorOne, colorTwo, options) > 4.5 | ||
} | ||
|
||
function getRgbTriplet(color, options) { | ||
if (typeof color !== 'string') { | ||
throw new TypeError('get-contrast expects colors as strings'); | ||
throw new TypeError('get-contrast expects colors as strings') | ||
} | ||
|
||
if (isNamedCssColor(color)) { | ||
color = cssColorNames[color]; | ||
color = cssColorNames[color] | ||
} | ||
|
||
color = isNotTransparent(color, options); | ||
return color.match(/\((.*)\)/)[1].split(',').slice(0, 3); | ||
color = isNotTransparent(color, options) | ||
return color.match(/\((.*)\)/)[1].split(',').slice(0, 3) | ||
} | ||
|
||
function isNotTransparent(color, options) { | ||
options = options || {}; | ||
options = options || {} | ||
|
||
// Convert to RGB. | ||
color = rgb(color); | ||
color = rgb(color) | ||
// Check if the rgb returned color is rgba and if the 'a' value is 0. | ||
var cArray = color.match(/\((.*)\)/)[1].split(','); | ||
var cArray = color.match(/\((.*)\)/)[1].split(',') | ||
if (cArray.length == 4 && cArray[3] == '0' && !options.ignoreAlpha) { | ||
throw new TypeError('get-contrast cannot contrast transparent colors'); | ||
throw new TypeError('get-contrast cannot contrast transparent colors') | ||
} else { | ||
return color; | ||
return color | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters