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 🙏

© 2026 – Pkg Stats / Ryan Hefner

color-contrast-js

v2.0.0

Published

Analyze CSS color contrast and choose accessible foreground colors.

Readme

colorContrast.js

Analyze CSS color contrast, check WCAG compliance, and choose accessible foreground colors. It is a browser-ready, ESM-only module with TypeScript declarations and no runtime dependencies for consumers.

Install

After publication:

npm install color-contrast-js
import {
  backgroundTone,
  analyzeColor,
  contrastRatio,
} from "color-contrast-js";

Quick start

backgroundTone("#336699"); // "dark" — use light text
backgroundTone("tomato"); // "light" — use dark text

contrastRatio("#000", "#fff"); // 21

analyzeColor("oklch(65% 0.2 30)");
// Color, luminance, black/white contrast, recommended text,
// background tone, and WCAG AA/AAA results.

The tone describes the background, so it can be used as a class name:

.light { color: #000; }
.dark { color: #fff; }

Common tasks

bestTextColor("#336699", ["#fff", "#000", "#ff0"]); // "#fff"

meetsContrast("#000", "#FF5733", {
  level: "AA",
  textSize: "normal",
}); // true

compositeColors("rgb(0 0 0 / 50%)", "#fff");
// "rgb(128, 128, 128)"

Analyze an element using its computed foreground, composited background, font size, and font weight:

const result = analyzeElement(document.querySelector(".sample"));

result.contrastRatio;
result.textSize; // "normal" or "large"
result.aa;
result.aaa;

Or apply a background-tone class to elements:

applyBackgroundTone(".sample", {
  lightClass: "on-light",
  darkClass: "on-dark",
});

API

| API | Purpose | | --- | --- | | backgroundTone(color, options) | Classify a background as "light" or "dark" | | analyzeColor(color, options) | Return luminance, recommendations, contrast, and compliance | | contrastRatio(first, second, options) | Compare two colors from 1 to 21 | | bestTextColor(background, candidates, options) | Choose the strongest supplied foreground | | meetsContrast(foreground, background, options) | Check WCAG AA or AAA | | compositeColors(foreground, background) | Resolve the visible result of transparency | | parseColor(color) | Parse a CSS color into RGBA channels | | analyzePalette(colors, options) | Analyze every color in a palette | | contrastMatrix(colors, options) | Calculate all palette contrast pairs | | getBackgroundColor(element, options) | Resolve a composited DOM background | | analyzeElement(element, options) | Check computed element contrast and WCAG results | | applyBackgroundTone(target, options) | Apply tone classes to DOM elements |

See the complete API reference for parameters, return values, options, examples, and TypeScript types.

The historical colorBrightness, colourBrightness, applyColorBrightness, and applyColourBrightness names remain as compatibility aliases. New code should use backgroundTone and applyBackgroundTone because their names make the result unambiguous.

Supported colors

  • HEX, RGB, RGBA, HSL, and HSLA
  • CSS named colors such as tomato
  • lab(), lch(), oklab(), and oklch()
  • color() spaces including sRGB, linear sRGB, Display P3, A98 RGB, ProPhoto RGB, Rec. 2020, and XYZ

Wide-gamut colors are converted to sRGB and clipped to the sRGB gamut before the WCAG 2 contrast calculation. Alpha values and CSS percentage syntax are supported.

Which build should I use?

Both builds expose the same API:

  • color-contrast.js is the readable ES module and the default npm entry. Use import ... from "color-contrast-js" in applications and libraries. Your bundler can minify it as part of the final build.
  • color-contrast.min.js is the pre-minified ES module for direct browser or CDN use. npm consumers can explicitly import color-contrast-js/min, although this is rarely necessary.

Keeping the readable build makes debugging, code review, source attribution, and unbundled development easier. The minified build provides the smaller delivery option; it should not be the only published file.

The package is ESM-only. Use import rather than CommonJS require().

Live example

hex.madebynifty.com uses colorContrast.js to keep its text readable as the background changes.

Development

Requires Node.js 22 or newer for development:

npm install
npm run build
npm run check

See CHANGELOG.md and THIRD_PARTY_NOTICES.md.

License

MIT © 2013–2026 Jamie Brittain.