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

oklch-wcag

v1.0.3

Published

Zero-dependency sRGB, HEX, HSL, and perceptual OKLCH color conversion engine with WCAG 2.1 contrast calculation, AA lightness auto-fix, Tailwind CSS shade matching, and RGBA palette quantization.

Readme

oklch-wcag (@sunasty/color-math)

npm version JSR CI License: MIT Dependencies: Zero

Zero-dependency JavaScript and TypeScript library for sRGB, HEX, HSL, and perceptual OKLCH color conversions, WCAG 2.1 contrast calculation, automatic AA HSL lightness adjustment, Tailwind CSS token matching, and RGBA buffer palette quantization.

Features

  • Perceptual OKLCH & sRGB: Forward (rgbToOklch) and inverse (oklchToRgb) Oklab/OKLCH transformations alongside rgbToHex, hexToRgb, rgbToHsl, and hslToRgb.
  • Unified Color Parser: parseColorInput accepts #RGB, #RRGGBB, rgb(...), hsl(...), and oklch(...) strings.
  • WCAG 2.1 Contrast & Auto-Fix: computeContrastRatio evaluates AA/AAA compliance for normal and large text against unrounded luminance ratios; autoFixContrastForAA steps HSL lightness to reach a target contrast ratio (4.5:1 by default) while preserving HSL hue and saturation.
  • Design System Generators: findClosestTailwindShade, generateShadesScale (9-stop 50-900 HSL scale), and generateHarmonies (complementary, analogous, triadic).
  • RGBA Palette Quantizer: extractDominantPaletteFromRgba clusters raw ImageData pixel buffers into dominant swatches (up to maxColors, default 12) and exports to CSS custom properties (exportPaletteToCssVars) or JSON (exportPaletteToJson).

Installation

# npm (Node.js / bundlers)
npm install oklch-wcag

# JSR (@sunasty/color-math for Deno / Bun / Node)
npx jsr add @sunasty/color-math
deno add jsr:@sunasty/color-math

Or import directly in browsers and Deno via ESM CDN (jsDelivr):

import {
  rgbToHex,
  rgbToOklch,
  computeContrastRatio,
  autoFixContrastForAA,
} from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';

Usage

Color Conversion and Notations

import {
  formatAllColorNotations,
  parseColorInput,
} from 'oklch-wcag'; // or '@sunasty/color-math' on JSR

const notations = formatAllColorNotations(37, 99, 235);
console.log(notations.hex);       // "#2563EB"
console.log(notations.hslStr);    // "hsl(221, 83%, 53%)"
console.log(notations.oklchStr);  // "oklch(0.55 0.22 263)"
console.log(notations.tailwind);  // { name: "blue-600", hex: "#2563EB", distance: 0 }

const parsed = parseColorInput('oklch(0.55 0.19 260)');
console.log(parsed);              // { r: 35, g: 106, b: 222, hex: "#236ADE" }

WCAG 2.1 Contrast Verification and Automatic Lightness Adjustment

import { computeContrastRatio, autoFixContrastForAA } from 'oklch-wcag';

const check = computeContrastRatio('#94A3B8', '#FFFFFF');
console.log(check);
// { ratio: 2.56, aaNormal: false, aaLarge: false, aaaNormal: false, aaaLarge: false }

const accessibleFg = autoFixContrastForAA('#94A3B8', '#FFFFFF', 4.5);
console.log(accessibleFg); // "#627693"
console.log(computeContrastRatio(accessibleFg, '#FFFFFF').ratio); // 4.63

Shade Scales, Harmonies, and RGBA Quantization

import {
  generateShadesScale,
  generateHarmonies,
  extractDominantPaletteFromRgba,
  exportPaletteToCssVars,
} from 'oklch-wcag';

const scale = generateShadesScale(37, 99, 235);
// [{ step: "50", hex: "#ECF2FD" }, ..., { step: "900", hex: "#071C4B" }]

const harmonies = generateHarmonies(37, 99, 235);
// { base: "#2563EB", complementary: "#EBAC24", analogous1: "#24C6EB", ... }

// Extract dominant palette from a Canvas 2D ImageData buffer:
const swatches = extractDominantPaletteFromRgba(imageData.data, imageData.width, imageData.height, 8);
console.log(exportPaletteToCssVars(swatches));

Scope and Limitations

  • sRGB clamping: Out-of-gamut OKLCH inputs converted via oklchToRgb are clamped to 8-bit 0..255 sRGB channels rather than gamut-mapped in Display P3.
  • HSL auto-fix: autoFixContrastForAA adjusts HSL lightness to guarantee deterministic hex contrast convergence against targetRatio.

Web Utilities

License

MIT