cococh
TypeScript icon, indicating that this package has built-in type declarations

1.2.4 • Public • Published

Cococh (Color contrast checker)

GitHub package.json version (branch) GitHub Workflow Status (with event) Codecov GitHub npm bundle size

A set of tools for validating the color contrast based on WCAG 2.0 standard.

Installation

Using npm

npm i cococh@latest

Using yarn

yarn add cococh@latest

or installing it manually on package.json

{
    //...otherConfigs,
    "dependencies": {
        "cococh": "^1.2.0"
    }   
}

and then do npm install or yarn install

Usage

It is possible to use different color formats and combine them:

// With rgb()
const ratios = getContrastRatios("rgb(255, 0, 0)", "rgb(0, 0, 0)");

// with rgb() and 3-digit hexa
const ratios = getContrastRatios("rgb(255, 0, 0)", "#000");

// with hsl() and rgb()
const ratios = getContrastRatios("hsl(0, 100%, 50%)", "rgb(0, 0, 0)");

// with 3-digit hexa and 6-digit hexa
const ratios = getContrastRatios("#f00", "#000000");

Any combination is valid, and you will receive an object like this:

{
    "normal": {
        "AA": true,
        "AAA": false
    },
    "large": {
        "AA": true,
        "AAA": true
    }
}

You can validate like this:

if (ratios.large.AAA) {
    // Is valid to use as large text on an AAA level
}

if (ratios.large.AA) {
    // Is valid to use as large text on an AA level
}

if (ratios.normal.AAA) {
    // Is valid to use as normal text on an AAA level
}

if (ratios.normal.AA) {
    // Is valid to use as normal text on an AA level
}

Test

To test it use:

yarn test

or

npm test

Package Sidebar

Install

npm i cococh

Weekly Downloads

223

Version

1.2.4

License

Apache-2.0

Unpacked Size

27.9 kB

Total Files

12

Last publish

Collaborators

  • dallegos