# get-contrast

> Get the contrast ratio and WCAG score between common CSS color types.

Latest version **3.0.0** (published 2021-04-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-contrast
pnpm add get-contrast
yarn add get-contrast
bun add get-contrast
```

Provides the command `contrast`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-04-26 |
| First published | 2014-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 5.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 84 |
| Author | John Otander |
| Maintainers | johno |
| Keywords | wcag, color, contrast, contrast-ratio |

## Links

- npm: https://www.npmjs.com/package/get-contrast
- Repository: https://github.com/johno/get-contrast
- Homepage: https://github.com/johno/get-contrast#readme
- Issues: https://github.com/johno/get-contrast/issues
- npm.io page: https://npm.io/package/get-contrast

## Dependencies (5)

- [rgb](https://npm.io/package/rgb.md) ^0.1.0
- [is-blank](https://npm.io/package/is-blank.md) 2.1.0
- [wcag-contrast](https://npm.io/package/wcag-contrast.md) 3.0.0
- [css-color-names](https://npm.io/package/css-color-names.md) 1.0.1
- [is-named-css-color](https://npm.io/package/is-named-css-color.md) 1.0.0

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-04-26
- 2.0.0 — 2016-05-02
- 1.1.1 — 2015-05-14
- 1.1.0 — 2015-05-14
- 1.0.0 — 2015-02-17
- 0.0.1 — 2014-12-11

## README

# get-contrast

Get the contrast ratio and WCAG score for two colors based on
[W3C Accessibility Standards](http://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast).
It handles rgb, rgba, hex, hsl, hsla, and named CSS colors.

## Installation

```
npm i --save get-contrast
# yarn add get-contrast
```

## Usage

```javascript
const contrast = require("get-contrast");

contrast.ratio("#fafafa", "rgba(0,0,0,.75)"); // => 10
contrast.score("#fafafa", "rgba(0,0,0,.75)"); // => 'AAA'
contrast.isAccessible("#fafafa", "rgba(0,0,0,.75)"); // => true
contrast.isAccessible("#fafafa", "#fff"); // => false
contrast.score("rebeccapurple", "tomato"); // => 'F'
```

#### Options

- `ignoreAlpha` (default: `false`) - Don't raise an error when transparent values are passed (`rgba(0, 0, 0, 0)`)

```js
contrast.score("rgba(0, 0, 0, 0)", "rgba(255, 255, 255, 0)", {
  ignoreAlpha: true,
}); // => 'AAA'
```

## CLI

This module includes a command line interface `contrast`.

```sh
$ npm i get-contrast -g
$ contrast "#000" "#fff"
Ratio: 21
Score: AAA
# The second parameter defaults to #fff
$ contrast white
Ratio: 1
Score: F
The contrast is not accessible.
# Contrast will exit with an error code, when the values are not accessible.
$ contrast "#ff0" "#fff" && ./deploy.sh
Ratio: 1.0738392309265699
Score: F
The contrast is not accessible.
```

## Related

Uses the following packages:

- <https://github.com/tmcw/wcag-contrast>
- <https://github.com/kamicane/rgb>

Inspired by:

- <https://leaverou.github.com/contrast-ratio>

## License

MIT

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

> Authored by [John Otander](http://johno.com) ([@4lpine](https://twitter.com/4lpine)).

---
_Source: https://npm.io/package/get-contrast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
