Skip to content

Plugin System ​

xcolor's core stays lean; advanced features are loaded on demand through plugins.

Installing Plugins ​

All official plugins are imported from @xpyjs/color/plugins/* and registered via xcolor.use():

ts
import xcolor from '@xpyjs/color'
import labPlugin from '@xpyjs/color/plugins/lab'
import namePlugin from '@xpyjs/color/plugins/name'

xcolor.use(labPlugin)
xcolor.use(namePlugin)

After registration, new methods are directly mounted on all xcolor instances:

ts
xcolor('#ff0000').toLab()     // { l: 53.24, a: 80.09, b: 67.2 }
xcolor('#ff0000').toName()    // 'red'

Registration Timing ​

xcolor.use() is global — after registration, all instances created before and after can use the plugin methods. It's recommended to register uniformly at the application entry:

ts
// main.ts / index.ts
import xcolor from '@xpyjs/color'
import labPlugin from '@xpyjs/color/plugins/lab'
import harmonyPlugin from '@xpyjs/color/plugins/harmony'

xcolor.use(labPlugin)
xcolor.use(harmonyPlugin)

// Then use anywhere

WARNING

Registering a plugin with an existing name runs its installer again and logs a warning. A successful install replaces the previous parser and formatter registrations.

Plugins with Options ​

Some plugins support configuration options, passed as the second argument to use():

ts
xcolor.use(somePlugin, { /* options */ })

Official Plugins ​

Color Spaces ​

Convert between color spaces, parse color inputs, and format outputs.

PluginDescription
cmykCMYK print color space
labCIE Lab / LCH perceptual color space
hsvHSV input, channel access, and output
hwbHWB color space
oklabOklab / Oklch modern perceptual color space
a98RgbAdobe RGB (1998) wide gamut
displayP3Display P3 wide gamut
proPhotoRgbProPhoto RGB wide gamut
rec2020Rec. 2020 wide gamut
xyzCIE XYZ (D65/D50)
colorSpacePreserve the original color space, convert coordinates, map gamuts, compare colors, and create multi-stop gradients
nameParse CSS named colors and output matching names
percentageRgbFormat RGB channels as percentages

Utilities ​

Manipulate colors, build palettes and themes, and check readability.

PluginDescription
a11yContrast calculation, readability checks, contrast adjustment, and batch audits
blendLayer blending and Porter–Duff compositing
gradientGradient sequences and tonal range generation
harmonyColor schemes (complement, triad, tetrad, etc.)
paletteGenerate a shade palette from a base color
scaleInterpolate in a chosen color space with easing and position sampling
simulateColor vision deficiency simulation
temperatureColor temperature read and write
themeGenerate semantic colors and light and dark themes, then export or inject CSS variables

Custom Plugin Development ​

xcolor's plugin interface is fully open. You can create your own plugins to extend instance methods, register new parsers, or add output formats. See Custom Plugin Development for details.

Released under the MIT License.