Chromatism Color Utilities
Chromatism is a lightweight JavaScript utility library designed for comprehensive color manipulation, offering a rich set of functions for color transformations, conversions, and metering. It supports various color types including RGB, HSL, Hex, CMYK, and can intelligently process mixed input types. The current stable version is 3.0.0, which marked a significant breaking change focused on optimizing for ES Modules (ESM) to achieve the smallest possible bundle sizes. Key differentiators include its strong emphasis on ESM for tree-shaking, the implementation of uniform hue-shifting transforms using HSLuv for more accurate color adjustments, and an efficient internal conversion chain. The library also ships with first-class TypeScript definitions, enhancing developer experience and type safety. While previous major versions supported function chaining, v3.0.0 removed this pattern in favor of a more functional, direct API.
Common errors
-
chromatism.someFunction is not a function
cause Attempting to use named exports with CommonJS `require()` or incorrect ES Module import syntax.fixFor ES Modules, use `import { someFunction } from 'chromatism';` for individual functions or `import * as chromatism from 'chromatism';` and then `chromatism.someFunction(...)`. If targeting a CommonJS environment that still supports the library, ensure `const chromatism = require('chromatism');` is used before accessing `chromatism.someFunction(...)`. -
TypeError: chromatism.chain is not a function
cause Attempting to use the deprecated function chaining API that was removed in v3.0.0.fixRefactor your code to use individual function calls. For example, instead of `chromatism.chain(color).shade(10).hex`, you would write `const intermediateColor = chromatism.convert(color); const finalColor = chromatism.shade(10, intermediateColor.rgb); const hexValue = finalColor.hex;` -
Property 'hex' does not exist on type '{ r: number; g: number; b: number; }' (or similar color object without getters).cause While many functions return a full color object with getters (like `.hex`, `.rgb`), you might be accessing a raw color component object (e.g., `{ r, g, b }`) directly from a destructured result or an intermediate step that hasn't been converted to the full chromatism color object.fixEnsure you are using the full color object returned by chromatism functions to access format getters. For example, `const myColor = chromatism.shade(10, baseColor); myColor.hex;` is correct. If you have a raw `{ r, g, b }` object, convert it first: `chromatism.convert({ r: 255, g: 0, b: 0 }).hex;`
Warnings
- breaking Function chaining, a core feature in pre-v2.x versions, was entirely removed in v3.0.0. Functions now return color objects with getters for various types (e.g., `.hex`, `.rgb`), requiring a functional approach rather than chained method calls.
- breaking Illuminant and Transformation Matrix constants were removed in v3.0.0 to streamline the library and reduce its bundle size. If your application relied on these specific constants, they are no longer available directly from the library.
- gotcha Version 3.0.0 is heavily optimized for ES Modules (ESM) and is shipped with a `pkg.module` entry. While CommonJS `require()` might still function in some environments, for optimal bundle size and adherence to modern JavaScript standards, always use `import` statements.
- gotcha Prior to v2.5.8, TypeScript definitions for `chromatism` were either missing or incomplete. While the library now ships with built-in types, older versions might lead to type errors or require external `@types/chromatism` packages.
Install
-
npm install chromatism -
yarn add chromatism -
pnpm add chromatism
Imports
- brightness
const { brightness } = require('chromatism');import { brightness } from 'chromatism'; - * as chromatism
const chromatism = require('chromatism');import * as chromatism from 'chromatism';
- RGB, HEX
import type { RGB, HEX } from 'chromatism';
Quickstart
import { convert, shade, invert, complementary, rgb } from 'chromatism';
// Start with a hex color
const baseHex = '#FF6347'; // Tomato
// Convert to RGB and then apply a shade adjustment
const shadedColor = shade(15, convert(baseHex).rgb);
console.log('Shaded Color (RGB):', shadedColor.rgb); // Access specific format
console.log('Shaded Color (HEX):', shadedColor.hex);
// Get the complementary color in HSL
const complementaryColor = complementary(baseHex).hsl;
console.log('Complementary Color (HSL):', complementaryColor);
// Invert the original color
const invertedColor = invert(baseHex).rgb;
console.log('Inverted Color (RGB):', invertedColor);
// Example with direct RGB input and brightness adjustment
const newColor = rgb(255, 0, 0); // Red
const brighterRed = shade(-20, newColor); // shade can also brighten
console.log('Brighter Red (HEX):', brighterRed.hex);