UNPKG

822 BJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = getContrast;
5var _getLuminance = _interopRequireDefault(require("./getLuminance"));
6function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
7/**
8 * Returns the contrast ratio between two colors based on
9 * [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).
10 *
11 * @example
12 * const contrastRatio = getContrast('#444', '#fff');
13 */
14function getContrast(color1, color2) {
15 var luminance1 = (0, _getLuminance["default"])(color1);
16 var luminance2 = (0, _getLuminance["default"])(color2);
17 return parseFloat((luminance1 > luminance2 ? (luminance1 + 0.05) / (luminance2 + 0.05) : (luminance2 + 0.05) / (luminance1 + 0.05)).toFixed(2));
18}
19module.exports = exports.default;
\No newline at end of file