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

get-contrast-color

v2.0.13

Published

A production-ready utility to determine readable text color (black or white) based on any background including gradients.

Downloads

16

Readme

get-contrast-color

A production-ready JavaScript/TypeScript library to determine readable contrast text color for any CSS background — including solid colors and gradients.

Supports:

  • hex / rgb(a) / hsl(a)
  • linear-gradient() with multiple stops
  • Custom contrast rule for two-color 50/50 gradients (auto-reverse)

Meets WCAG contrast guidelines for accessibility.

📦 Installation

npm install get-contrast-color

or

yarn add get-contrast-color

🚀 Quick Usage

Node.js / CommonJS

import {getContrastColor} from 'get-contrast-color';;

console.log(getContrastColor('#ff0000').color); // "#ffffff"
console.log(getContrastColor('rgb(240,240,240)').color); // "#000000"
console.log(getContrastColor('linear-gradient(90deg,#2563EB,#9C27B0)').color); // "#ffffff"

ES Modules / TypeScript

import { getContrastColor } from 'get-contrast-color';

const result = getContrastColor('#1e293b');
console.log(result.color); // "#ffffff"

🧠 Special Feature — Contrast Mode

If input is a ColorItem with:

  • type: "contrast"
  • exactly 2 colors
  • both gradient positions 50% split

Then it reverses the colors to ensure readable text.

Example:

const contrastItem = {
  type: 'contrast',
  colors: ['#10B981', '#FFFBEB'],
  gradientAngle: '135deg',
  gradientPositions: ['50%', '50%']
};

console.log(getContrastColor(contrastItem));

Output format:

{
  format: {
    usage: 'Text',
    type: 'gradient',
    colors: ['#FFFBEB', '#10B981'], // reversed
    gradientAngle: '135deg',
    gradientPositions: ['50%', '50%'],
    displayName: 'Contrast Reverse Color',
    value: 'ContrastReverseColor',
    description: 'Reversed for readable text'
  },
  color: 'linear-gradient(135deg, #FFFBEB 50%, #10B981 50%)'
}

💡 Perfect when backgrounds are dynamically selected by end-users.

📌 API

getContrastColor(input: string | ColorItem): GetContrastResult

| Input Type | Behavior | | ------------------ | ----------------------------------------------- | | Solid color | Returns best contrast as solid #000/#fff | | Gradient | Uses average luminance → solid #000/#fff | | Contrast ColorItem | Returns reversed gradient + format metadata | | Invalid input | Safe fallback to #000000 |

Return Type (GetContrastResult)

interface GetContrastResult {
  color: string; // "#000000" | "#ffffff" | "linear-gradient(...)"
  format: {
    usage: 'Text';
    type: 'solid' | 'gradient';
    colors: string[];
    gradientAngle: string;
    gradientPositions: string[];
    displayName: string;
    value: string;
    description: string;
  };
}

👀 Real UI Example

React + inline style:

const cardBg =
  'linear-gradient(45deg, #ff0 0%, #f06 100%)';

const { color: textColor } = getContrastColor(cardBg);

return (
  <div style={{ background: cardBg, color: textColor }}>
    Dynamic Contrast Text Demo
  </div>
);

🔒 Robustness

✔ Handles malformed colors ✔ Handles alpha (rgba/hsla) ✔ Graceful fallback ✔ No regex-only parsing — luminance-accurate

⚙ Build Outputs

  • ESM (modern bundlers)
  • CJS (Node.js)
  • UMD (browsers)
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"browser": "dist/index.umd.js"

🔑 License

MIT