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

sunasty-color-math

v1.0.0

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

sunasty-color-math

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

Extracted from the core engine powering the Sunasty Color Picker and the Sunasty Developer Tools suite.

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; autoFixContrastForAA solves for the nearest lightness value that satisfies a target contrast ratio (4.5:1 by default) while preserving hue and saturation.
  • Design System Generators: findClosestTailwindShade, generateShadesScale (9-stop 50-900 scale), and generateHarmonies (complementary, analogous, triadic).
  • RGBA Palette Quantizer: extractDominantPaletteFromRgba clusters raw ImageData pixel buffers into dominant swatches and exports to CSS custom properties (exportPaletteToCssVars) or JSON (exportPaletteToJson).

Installation

Via npm:

npm install sunasty-color-math

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

import {
  rgbToHex,
  rgbToOklch,
  computeContrastRatio,
  autoFixContrastForAA,
} from 'https://cdn.jsdelivr.net/gh/bsulyan/[email protected]/index.js';

Usage

Color Conversion and Notations

import {
  rgbToHex,
  rgbToOklch,
  oklchToRgb,
  formatAllColorNotations,
  parseColorInput,
} from 'sunasty-color-math';

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.19 260)"
console.log(notations.tailwind);  // { name: "blue-600", hex: "#2563EB", distance: 0 }

const parsed = parseColorInput('oklch(0.55 0.19 260)');
console.log(parsed);              // { r: 37, g: 99, b: 234, hex: "#2563EA" }

WCAG 2.1 Contrast Verification and Automatic Lightness Adjustment

import { computeContrastRatio, autoFixContrastForAA } from 'sunasty-color-math';

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); // "#596B82"
console.log(computeContrastRatio(accessibleFg, '#FFFFFF').ratio); // 4.56

Shade Scales, Harmonies, and RGBA Quantization

import {
  generateShadesScale,
  generateHarmonies,
  extractDominantPaletteFromRgba,
  exportPaletteToCssVars,
} from 'sunasty-color-math';

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

const harmonies = generateHarmonies(37, 99, 235);
// { base: "#2563EB", complementary: "#EBAD25", analogous1: "#25C7EB", ... }

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

Web Utilities

License

MIT