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

invert-color

v3.0.0

Published

Generates the inverted (opposite) version of a given color.

Readme

invert-color

This module is ESM 🔆. Please read this.

[!IMPORTANT] v3 is ESM-only — a breaking change. The CommonJS and UMD builds are gone: require('invert-color') no longer works, and the minimum Node.js is now 20. If you still need CommonJS/UMD, stay on v2npm i invert-color@2. See the changelog for the full upgrade note.

Generates the inverted (opposite) version of a given color. Tiny, zero-dependency, and typed.

[!NOTE] The results match Adobe Photoshop CC exactly — the suite is verified against a long list of Photoshop-inverted colors.

Installation

npm i invert-color

Usage

import invert from 'invert-color';
// or a named import — both resolve to the same function:
import { invert } from 'invert-color';
// with types:
import invert, { type RGB, type RgbArray, type HexColor, type BlackWhite } from 'invert-color';
invert('#000');                      // → '#ffffff'
invert('#282b35');                   // → '#d7d4ca'

// RGB array or object input
invert([69, 191, 189]);              // → '#ba4042'
invert({ r: 249, g: 119, b: 121 });  // → '#068886'

// CSS rgb() / rgba() strings
invert('rgb(40, 43, 53)');           // → '#d7d4ca'
invert('rgba(40, 43, 53, 0.5)');     // → '#d7d4ca80'  (alpha preserved as 8-digit hex)

Amplify to Black or White

Pass bw: true to amplify the result to black or white — chosen by the source color's luminance. Handy for picking a readable foreground over a given background.

invert('#282b35', true);   // → '#ffffff'
invert('#d7d4ca', true);   // → '#000000'

Pass an object to customize the two colors, and/or the luminance threshold:

invert('#282b35', { black: '#3a3a3a', white: '#fafafa' });                   // → '#fafafa'
invert('#282b35', { black: '#3a3a3a', white: '#fafafa', threshold: 0.01 });  // → '#3a3a3a'

[!TIP] bw is ideal for contrast — e.g. a background vs. its foreground text — so content stays legible. The animation above is exactly this in action.

Other Output Shapes

invert.asRGB('#fff');        // → { r: 0, g: 0, b: 0 }
invert.asRgbArray('#000');   // → [255, 255, 255]
invert.asRgbObject('#fff');  // → { r: 0, g: 0, b: 0 }   (alias of asRGB)

API

| Member | Signature | Description | | ------ | --------- | ----------- | | invert | (color, bw?) => HexColor | Inverts color, returns a HEX string. | | invert.asRGB | (color, bw?) => RGB | Inverts color, returns an { r, g, b } object. | | invert.asRgbArray | (color, bw?) => RgbArray | Inverts color, returns a [r, g, b] tuple. | | invert.asRgbObject | (color, bw?) => RGB | Alias of invert.asRGB. | | invert.defaultThreshold | number | Default luminance threshold (≈ 0.179) used to amplify to black/white. |

Parameters

| Name | Type | Description | | ---- | ---- | ----------- | | color | HexColor \| number[] \| RGB | The color to invert. Accepts a HEX string (3- or 6-digit, with or without #), a CSS rgb()/rgba() string, an [r, g, b] array, or an { r, g, b } object. | | bw | boolean \| BlackWhite | Optional. true amplifies to black/white by luminance. An object customizes black, white and/or threshold. Defaults to false. |

Types

type RGB = { r: number; g: number; b: number };
type RgbArray = [number, number, number];   // the shape returned by asRgbArray
type HexColor = string;                      // hex, or a CSS rgb()/rgba() string
type Color = RGB | number[] | HexColor;

interface BlackWhite {
    black: HexColor;
    white: HexColor;
    threshold?: number; // 0–1, defaults to invert.defaultThreshold
}

[!NOTE] Input handling. Array/object channels are clamped to 0255 and rounded, so out-of-range values degrade predictably (invert([300, 300, 300]) → '#000000'). Malformed input — a non-3-length array or a non-finite channel — throws, as does an invalid HEX or rgb() string.

Alpha. An rgba() alpha below 1 is preserved on invert() as the trailing hex byte (#rrggbbaa); asRGB / asRgbArray return RGB only and drop it.

Changelog

See CHANGELOG.md. Version 3 is ESM-only; if you still need CommonJS/UMD, pin invert-color@2.

License

© 2026, Onur Yıldırım. MIT License.