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

v1.0.1

Published

Zero-dependency, precision color contrast ratio and WCAG 2.1/2.2 accessibility checker with AA/AAA compliance reporting and auto-adjustment suggestions.

Readme

🎨 color-contrast-wcag

Zero-dependency, precision color contrast ratio and WCAG 2.1/2.2 accessibility checker with AA/AAA compliance reporting and auto-adjustment suggestions.

npm version License: MIT Sponsor TypeScript


✨ Features

  • ⚡ Zero Dependencies — Pure mathematical implementation of W3C WCAG 2.1/2.2 specs (~2 KB).
  • 🌈 Multiple Color Formats — Supports #HEX (3, 4, 6, 8-digit), rgb(), rgba(), and standard named colors.
  • 🎯 Full WCAG Evaluation — Reports compliance for Normal Text (AA >= 4.5, AAA >= 7.0), Large Text (AA >= 3.0, AAA >= 4.5), and UI Components (AA >= 3.0).
  • 💡 Auto-Fix Suggester — suggestAccessibleColor automatically adjusts an inaccessible color until it reaches passing contrast!
  • 🌓 Smart Text Color Picker — getBestTextColor picks either #000000 or #ffffff based on optimal background contrast.
  • 📦 Dual ESM & CommonJS — Works in Node.js, React, Next.js, Vite, and browsers.

📥 Installation

npm install color-contrast-wcag

or with Yarn / pnpm:

pnpm add color-contrast-wcag
# or
yarn add color-contrast-wcag

🚀 Quick Start

1. Contrast Ratio & WCAG Evaluation

import { checkWcag, getContrastRatio } from 'color-contrast-wcag';

// 1. Quick ratio calculation
const ratio = getContrastRatio('#3b82f6', '#ffffff');
console.log(ratio); // 3.68

// 2. Comprehensive WCAG compliance report
const evaluation = checkWcag('#3b82f6', '#ffffff');
console.log(evaluation);
/*
{
  ratio: 3.68,
  formattedRatio: "3.68:1",
  normalText: { aa: false, aaa: false },
  largeText: { aa: true, aaa: false },
  uiComponent: { aa: true },
  score: "Fail"
}
*/

2. Auto-Fix an Inaccessible Color (suggestAccessibleColor)

If a designer or user provides a brand color that fails accessibility audits, automatically darken or lighten it to reach compliance:

import { suggestAccessibleColor, getContrastRatio } from 'color-contrast-wcag';

// Original text color #9ca3af on white background has low contrast (~2.0:1)
const accessibleColor = suggestAccessibleColor('#9ca3af', '#ffffff', 4.5);

console.log(accessibleColor); // "#717882" (adjusted to meet AA threshold)
console.log(getContrastRatio(accessibleColor, '#ffffff')); // >= 4.5

3. Dynamic Text Color for Badges & Buttons (getBestTextColor)

Automatically pick high-contrast black or white text for dynamic background colors:

import { getBestTextColor } from 'color-contrast-wcag';

console.log(getBestTextColor('#111827')); // "#ffffff" (white text for dark background)
console.log(getBestTextColor('#fef08a')); // "#000000" (black text for light yellow background)

📊 WCAG 2.1 / 2.2 Standards Reference

| Category | Level AA Threshold | Level AAA Threshold | |---|---|---| | Normal Text (< 18pt or < 14pt bold) | 4.5 : 1 | 7.0 : 1 | | Large Text (>= 18pt or >= 14pt bold) | 3.0 : 1 | 4.5 : 1 | | UI Components & Graphics | 3.0 : 1 | N/A |


🛠 Development & Testing

# Install dependencies
npm install

# Run unit tests
npm test

# Build dual ESM/CJS bundles + .d.ts types
npm run build

💖 Support the Project

If you find this package useful, consider sponsoring its ongoing development and maintenance: 👉 Sponsor @Piratha97 on GitHub Sponsors

📄 License

MIT © 2026 Pirapaakaran Pirathapan