Skip to content

Commit

Permalink
Modernize module
Browse files Browse the repository at this point in the history
  • Loading branch information
johno committed May 2, 2016
1 parent 2820be8 commit 3042eaf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
50 changes: 25 additions & 25 deletions index.js
@@ -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
}
}
2 changes: 1 addition & 1 deletion LICENSE → license
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2015 John Otander
Copyright (c) 2014-2016 John Otander

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 5 additions & 8 deletions package.json
Expand Up @@ -3,6 +3,10 @@
"version": "1.1.1",
"description": "Get the contrast ratio and WCAG score between common CSS color types.",
"main": "index.js",
"files": [
"cli.js",
"index.js"
],
"directories": {
"test": "test"
},
Expand All @@ -12,10 +16,7 @@
"scripts": {
"test": "mocha test"
},
"repository": {
"type": "git",
"url": "https://github.com/johnotander/get-contrast.git"
},
"repository": "johnotander/get-contrast",
"keywords": [
"wcag",
"color",
Expand All @@ -24,10 +25,6 @@
],
"author": "John Otander <johnotander@gmail.com> (http://johnotander.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/johnotander/get-contrast/issues"
},
"homepage": "https://github.com/johnotander/get-contrast",
"dependencies": {
"css-color-names": "0.0.1",
"is-blank": "0.0.1",
Expand Down
2 changes: 1 addition & 1 deletion README.md → readme.md
Expand Up @@ -15,7 +15,7 @@ npm i --save get-contrast
## Usage

```javascript
var contrast = require('get-contrast');
var contrast = require('get-contrast')

contrast.ratio('#fafafa', 'rgba(0,0,0,.75)') // => 10
contrast.score('#fafafa', 'rgba(0,0,0,.75)') // => 'AAA'
Expand Down
2 changes: 1 addition & 1 deletion test/test.js → test.js
@@ -1,5 +1,5 @@
var assert = require('assert');
var contrast = require('..');
var contrast = require('./');

describe('ratio', function() {

Expand Down

0 comments on commit 3042eaf

Please sign in to comment.