Getting Started
@xpyjs/color (abbreviated as xcolor) is a lightweight, fast, zero-dependency JavaScript / TypeScript color manipulation library. The core is about 5.7 kB (gzipped), supports chaining, unified Getter/Setter design, and a flexible plugin system.
Environment Requirements
- Node.js ≥ 14.0 (supports ES2015)
- All modern browsers (Chrome, Firefox, Safari, Edge, etc.)
- Deno, Bun, and other runtimes can be used directly
xcolor does not depend on any third-party packages and can run in any JavaScript environment that supports ES2015.
Installation
npm install @xpyjs/colorpnpm add @xpyjs/coloryarn add @xpyjs/colorbun add @xpyjs/colorFor more installation methods (CDN, UMD, Deno, etc.), see Installation.
First Example
import xcolor from '@xpyjs/color'
// Create a color
const color = xcolor('#3498db')
// Get different formats
color.toHex() // '#3498db'
color.toRgbString() // 'rgb(52, 152, 219)'
color.toHslString() // 'hsl(204, 70%, 53%)'
// Chain operations
color.lighten(20).saturate(10).toHex()xcolor() is a factory function — pass in a color value to create an instance, no new needed. All manipulation methods return the current instance, enabling natural chaining.
Three Core Designs
Mutable Instances
All manipulation methods modify the instance in place. When you need to preserve the original color, call .clone() first:
const original = xcolor('#ff0000')
const copy = original.clone()
copy.lighten(30)
original.toHex() // '#ff0000' — unchanged
copy.toHex() // '#ff4d4d'Getter / Setter in One
The same method reads without arguments and writes with arguments:
const c = xcolor('#ff0000')
c.red() // 255 — read
c.red(128) // this — write, returns instance for continued chainingOn-demand Plugin Loading
The core library stays lean; advanced features are extended through plugins:
import xcolor from '@xpyjs/color'
import harmony from '@xpyjs/color/plugins/harmony'
xcolor.use(harmony)
xcolor('#ff6600').triad() // triad color schemeMultiple official plugins are available, covering color space conversions, color schemes, color vision simulation, and more. See Plugin System for details.
Next Steps
- Installation — Complete installation methods and compatibility notes
- Create Color — All supported input formats
- Manipulation — lighten, darken, mix, and more with visual demos
- Plugin System — On-demand loading of advanced features
