MooColor
MooColor is a modern TypeScript library designed for comprehensive color parsing, conversion, and manipulation. Currently stable at version 2.0.0, the package maintains an active release cadence with minor updates and patches, and announces significant breaking changes with major versions. Key differentiators include its strictly immutable API, where all manipulation methods return new instances rather than mutating the original, and robust WCAG 2.1 compliance for accurate luminance and contrast ratio calculations. It provides dual ESM, CJS, and IIFE bundles, making it versatile across Node.js environments (requiring Node.js >=18 since v2) and browsers. The library is fully typed, including Template Literal Types for advanced color string representation, and supports a wide array of color models like hex, RGB, HSL, HWB, HSV, and CMYK, alongside named colors.
Common errors
-
TypeError: color.lighten is not a function
cause Attempting to call a manipulation method on a `MooColor` instance that was not correctly assigned after an immutable operation (post v2.0.0) or if `color` is not a `MooColor` instance.fixEnsure you are using the return value of manipulation methods, as they now return new instances. For example, `const newColor = originalColor.lighten(10);`. -
TypeError: color.setColor is not a function
cause Calling the `setColor()` method, which was removed in `moo-color` v2.0.0.fixInstead of `color.setColor('blue')`, create a new instance with the desired color: `const blueColor = new MooColor('blue');`. -
TypeError: Cannot read properties of undefined (reading 'random')
cause Attempting to call the `random()` method on an instance of `MooColor` (`new MooColor().random()`) after v0.2.0, when it became a static method.fixCall `MooColor.random()` as a static method directly on the class. -
Error: Cannot find module 'moo-color' (when running on older Node.js versions)
cause Running `moo-color` v2.0.0+ in a Node.js environment older than version 18.fixUpgrade your Node.js runtime to version 18 or higher to meet the package's engine requirements.
Warnings
- breaking All color manipulation methods (`lighten`, `darken`, `saturate`, `grayscale`, `whiten`, `blacken`, `rotate`, `complement`, `invert`), `setAlpha()`, and `changeModel()` now return a new `MooColor` instance instead of mutating the original object.
- breaking The `setColor()` instance method has been completely removed.
- breaking Node.js version 18 or higher is now required. Running the package in older Node.js environments will result in errors.
- breaking The `random()` method was changed from an instance method (`new MooColor().random()`) to a static method (`MooColor.random()`) of the `MooColor` class.
- gotcha When formatting colors to hexadecimal, the `toHex()` method's `mode` argument (introduced in v0.1.1) allows control over the output format ('full', 'short', or 'name'). Without specifying, it defaults to full hex.
Install
-
npm install moo-color -
yarn add moo-color -
pnpm add moo-color
Imports
- MooColor
const MooColor = require('moo-color')import { MooColor } from 'moo-color' - MooColor.random
const color = new MooColor('red'); const randomColor = color.random();import { MooColor } from 'moo-color'; const randomColor = MooColor.random(); - ColorData
import { ColorData } from 'moo-color';import type { ColorData } from 'moo-color';
Quickstart
import { MooColor } from 'moo-color';
// Parse various color formats, including named colors and RGBA
const red = new MooColor('red');
const semiTransparentBlue = new MooColor('rgba(0, 0, 255, 0.7)');
const orangeHsl = new MooColor('hsl(30, 100%, 50%)');
console.log('Original Red (hex):', red.toHex()); // #ff0000
// Manipulation methods return new instances (immutable API since v2.0.0)
const lightenedRed = red.lighten(20);
const desaturatedBlue = semiTransparentBlue.desaturate(30);
console.log('Lightened Red (hex):', lightenedRed.toHex()); // Example: #ff6666
console.log('Original Red is unchanged:', red.toHex()); // #ff0000
// Chaining operations
const adjustedOrange = orangeHsl
.rotate(60) // Rotate hue by 60 degrees
.saturate(15) // Increase saturation
.darken(10) // Darken the color
.setAlpha(0.9); // Set transparency
console.log('Adjusted Orange (RGBA):', adjustedOrange.toRgba()); // Example: rgba(..., 0.9)
// WCAG contrast ratio check
const white = new MooColor('#fff');
const black = new MooColor('#000');
console.log('Contrast Ratio (Black vs White):', black.contrastRatioWith(white)); // 21
// Generate a random color with specific constraints
const randomWarmColor = MooColor.random({ hue: [0, 60], saturation: [50, 100] });
console.log('Random warm color:', randomWarmColor.toHsl());