# safecolor

> SafeColor generates accessible colors that compiles with WCAG success criteria 1.4.3 (or any contrast ratio of your choice). It can generate either a random color that is contrast safe with a given color, or a consistent color for a given string.

Latest version **1.0.1** (published 2019-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install safecolor
pnpm add safecolor
yarn add safecolor
bun add safecolor
```

## 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 | 1.0.1 |
| Published | 2019-10-18 |
| First published | 2019-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | jessunix@gmail.com |
| Maintainers | jessuni |
| Keywords | wacg, contrast-ratio, accessibility, color, string, hex, rgb, hsl, hci, luminance, luma |

## Links

- npm: https://www.npmjs.com/package/safecolor
- Repository: https://github.com/jessuni/SafeColor
- Homepage: https://github.com/jessuni/SafeColor#readme
- Issues: https://github.com/jessuni/SafeColor/issues
- npm.io page: https://npm.io/package/safecolor

## 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

- 1.0.1 (latest) — 2019-10-18
- 1.0.0 — 2019-10-18

## README

# SafeColor
SafeColor generates accessible colors that complies with WCAG success criteria 1.4.3 (or any contrast ratio of your choice).
It can be used to:

1. generate a random color that is contrast safe with a given color
2. generate a consistent, contrast safe color for a string

No need to worry about your base color is light/dark or for foreground/background. If the given color is too light to meet your desired contrast ratio, SafeColor will look for a darker color and vise versa.


## Install

`npm install safecolor`

## Usage

`import SafeColor from 'safecolor'`

### Basic

This will assume that the generated color should be contrast safe (>= AA standard: 4.5) with black(rgb(0, 0, 0))

```javascript
  safeColor = new SafeColor()

  safeColor.random()
  // >> rgb(104, 145, 26)
  // contrast ratio = 5.65

  safeColor.random('hello world')
  // >> rgb(196,226,239)
  // contrast ratio = 15.47
```
### With options

```javascript
  safeColor = new SafeColor({
    color: [255, 255, 255], // 8bit RGB value in array [r, g, b]
    contrast: 4.5,  // the contrast ratio between the option color and the generated color will >= this
  })

  safeColor.random()
  // >> rgb(32,80,46)
  // contrast ratio = 9.34

  safeColor.random('hello world')
  // >> rgb(20,57,74)
  // contrast ratio = 12.25
```

## Options

**color**

- type: `Array`
- default: `[0, 0, 0]`

**contrast**

- type: `Number`
- default: `4.5`

## Notice
ES6 features: destructing assignment and map are used in this script. You may need polyfill for the script to work properly.

Note: to keep this as simple as possible, the output is a RGB value in string. If any built-in conversions (to HEX, to HSL) will make SafeColor much more convenient for you, please contact me to add the feature or feel free to pull request. Cheers!

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