npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

color-contrast-picker

v1.0.8

Published

Color picking tool for WCAG 2.1 compliant colors. Finds colors meeting the luminance ratio for any WCAG 2.1 standard, while preserving tone.

Downloads

304

Readme

color-contrast-picker

color-contrast-picker is a color conversion library for JavaScript and node.

This library provides a method for finding a color with a specific contrast ratio to another color and that matches the hue and saturation of the first.

API

getContrastingHex(color, contrastRatio)

For situations when you want a lighter/darker version of a color to precisely contrast the original. Returns a color of the same hue/saturation as the color and contrasts the original color with a contrast ratio of contrastRatio.

Uses

Potential applications include generating text colors for UI components with designated colors constrained by design specifications.

More specifically, say you had a button colored #1749DE #1749DE, and you needed to add text to it. This is what getContrastingHex is for:

const textColor = getContrastingHex('#1749DE', 'AA_text');

Now, textColor equals #C3D0F9 #C3D0F9, which has a contrast ratio of 4.5 meeting the AA_text WCAG standard.

Params

color

  • definition: the color you want to be altered into a lighter/darker color with the same hue and saturation
  • type: string
  • pattern: /^#[\da-fA-F]{6}/
  • examples: '#db1f90', '#ffffff', '#123456'

contrastRatio

  • definition: the minimum contrast ratio between your color and the function's output color see w3 glossary definition here
  • type: number | string
  • default value: 'AA' (equivalent to 3)
  • examples: 3, 'AA', 'AA_text', 'AA_text_large', 'AAA_text', 'AAA_text_large';

Example

const { getContrastingHex } = require('color-contrast-picker');

const hex1 = getContrastingHex('#7524B7', 4.5);
const hex2 = getContrastingHex('#7524B7', 'AA_text');

// hex1 = hex2 = '#D9BBF2' (contrast ratio = 4.58)
// the next lighter shade is '#DFBFF3' (contrast ratio = 4.79)
// the next darker shade is '#DAB6F1' (contrast ratio = 4.44)

const hex3 = getContrastingHex('#6D6D6D', 7);

// hex3 = null
// #000000 (black) has a contrast ratio of 4.05
// #FFFFFF (white) has a contrast ratio of 5.17
// thus, no color has sufficient contrast

makeHexesContrast(color, fixedColor, contrastRatio)

For situations when you want a two colors to have a specific contrast ratio. Returns a color of the same hue/saturation as the color that has a contrasts the fixedColor, with a contrast ratio of contrastRatio.

Uses

Potential applications include procedurally generating colors for charts.

More specifically, say you had two different colors, and you would like to adjust one of them so that their contrast ratio reaches a certain value. This is the function you'd want to use.

E.g. #8F428D #8F428D and #50B47B #50B47B have a contrast ratio of 2.44, not meeting the minimum WCAG standard of 3.0.

If I wanted to shift #8F428D #8F428D to achieve a contrast ratio of 3.0 I would call

const newPurple = makeHexesContrast('#8F428D', '50B47B', 3);

Now, newPurple equals #7A3878 #7A3878, which has a contrast ratio of 3.09 with #50B47B #50B47B (the minimum possible contrast ratio equal to or above 3 that is possible with 8-bit colors... without changing the hue or saturation of the purple).

Params

color

  • definition: the color you want to be altered into a lighter/darker color with the same hue and saturation
  • type: string
  • pattern: /^#[\da-fA-F]{6}/
  • examples: '#db1f90', '#ffffff', '#123456'

fixedColor

  • definition: the color you want to contrast against
  • type: string
  • pattern: /^#[\da-fA-F]{6}/
  • examples: '#db1f90', '#ffffff', '#123456'

contrastRatio

  • definition: the minimum contrast ratio between your fixedColor and the function's output color see w3 glossary definition here
  • type: number | string
  • default value: 'AA' (equivalent to 3)
  • examples: 3, 'AA', 'AA_text', 'AA_text_large', 'AAA_text', 'AAA_text_large';

Example

const { makeHexesContrast } = require('color-contrast-picker');

const hex1 = makeHexesContrast('#3c7801', '#3c7805', 4.5);
// hex1 = '#bafe76' (contrast ratio = 4.51)

const hex2 = makeHexesContrast('#3c7801', '#5a5a27', 'AA_text');
// hex1 = '#75e902' (contrast ratio = 4.58)

const hex3 = makeHexesContrast('#8F428D', 'db1f90', 7);

// hex3 = null
// #000000 (black) has a contrast ratio of 3.33
// #FFFFFF (white) has a contrast ratio of 6.3
// thus, no color has sufficient contrast

Install

$ npm install color-contrast-picker

Contribute

If there is a new method you would like to support, please make a pull request or reach out.

License

Licensed under the MIT License.

More info

  • https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
  • https://www.w3.org/WAI/WCAG21/Techniques/general/G18
  • https://webaim.org/resources/contrastchecker/