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 v2 β npm 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.
npm i invert-colorimport 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)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.
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)| 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 0β255 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.
See CHANGELOG.md. Version 3 is ESM-only; if you still need CommonJS/UMD, pin invert-color@2.
Β© 2026, Onur YΔ±ldΔ±rΔ±m. MIT License.
