@asamuzakjp/css-color
CSS color - Resolve and convert CSS colors.
Last updated 19 days ago by asamuzakjp .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @asamuzakjp/css-color 
SYNC missed versions from official npm registry.

CSS color

build CodeQL npm (scoped)

A robust and modern library to resolve, parse, and convert CSS colors. Supports the latest CSS Color Module Level 4 & 5 specifications.

Features

  • Modern CSS Color Support: Accurately resolves color-mix(), color(), modern color spaces (oklch, oklab, lch, lab, hwb, etc.), and relative colors (lab(from red l a b), color(from red xyz-d50 x y z)).
  • Deep Resolution: Deeply resolves var() and calc() functions embedded within color values.
  • Gradient Resolution & Parsing: Supports resolving, parsing, and validation for linear-gradient, radial-gradient, and conic-gradient.
  • Comprehensive Color Conversion: Highly accurate converters between HEX, HSL, HWB, LAB, LCH, Oklab, Oklch, RGB, and XYZ color spaces.
  • Bonus Utilities: Includes convenient functions to validate colors or gradients, extract CSS variables, and safely split CSS values.
  • Used in jsdom: Adopted as the CSS color parser and resolver for jsdom.
  • Pure ESM with TypeScript Ready: Native ESM (type: "module") with comprehensive TypeScript definitions.

Install

npm i @asamuzakjp/css-color

Quick Start

import {
  convert,
  resolve,
  resolveGradient,
  utils
} from '@asamuzakjp/css-color';

Samples

  1. Resolve complex modern CSS colors:
const resolvedMix = resolve(
  'color-mix(in oklab, lch(67.5345 42.5 258.2), color(srgb 0 0.5 0))'
);
// => 'oklab(0.620754 -0.0931934 -0.00374881)'
  1. Resolve with Custom Properties and calc():
const resolvedVar = resolve('hsl(calc(var(--base-hue) * 3) 100% 50% / .5)', {
  customProperty: { '--base-hue': '210deg' }
});
// => 'rgba(128, 0, 255, 0.5)'
  1. Resolve gradients:
const resolvedGradient = resolveGradient(
  'linear-gradient(to right, red, blue)'
);
// => 'linear-gradient(to right, rgb(255 0 0), rgb(0 0 255))'
  1. Convert between color spaces:
const hex = convert.colorToHex('lab(46.2775% -47.5621 48.5837)');
// => '#008000'
  1. Validate colors and gradients:
const isColor = utils.isColor('light-dark(red, blue)');
// => true

const isGradient = utils.isGradient(
  'conic-gradient(from 0.5turn at 50% 50%, red, blue)'
);
// => true

API Reference

resolve(color, opt?)

Resolves a CSS color string into its computed or specified value. System colors are not supported.

  • color <string>: The CSS color value to resolve.
  • opt <object> (optional):
    • opt.currentColor: Color to use for the currentcolor keyword.
    • opt.customProperty: Object containing -- prefixed keys and their values, or a callback(propertyName) function to dynamically resolve CSS variables.
    • opt.dimension: Object mapping units (e.g., em, rem, vw) to pixel numbers, or a callback(unit) function for dynamic length resolution.
    • opt.format: Output format. Options: computedValue (default), specifiedValue, hex, hexAlpha.
    • opt.colorScheme: normal (default), light, or dark (useful for light-dark() resolution).

resolveGradient(gradient, opt?)

Resolves a CSS gradient string (e.g., linear-gradient, radial-gradient, conic-gradient) into its resolved form.

  • gradient <string>: The CSS gradient string to resolve.
  • opt <object> (optional): Same options as resolve.

convert

A collection of color conversion utilities.

Function Returns Options Description
convert.colorToA98Rgb(value, opt?) number[] See resolve options Converts to Adobe RGB (1998) channels: [r, g, b, alpha].
convert.colorToDisplayP3(value, opt?) number[] See resolve options Converts to Display P3 channels: [r, g, b, alpha].
convert.colorToHex(value, opt?) string \| null opt.alpha <boolean>
(+ see resolve options)
Returns #rrggbb or #rrggbbaa (if opt.alpha is true).
convert.colorToHsl(value, opt?) number[] See resolve options Converts to HSL channels: [h, s, l, alpha].
convert.colorToHwb(value, opt?) number[] See resolve options Converts to HWB channels: [h, w, b, alpha].
convert.colorToLab(value, opt?) number[] See resolve options Converts to CIE LAB channels: [l, a, b, alpha].
convert.colorToLch(value, opt?) number[] See resolve options Converts to CIE LCH channels: [l, c, h, alpha].
convert.colorToLinearP3(value, opt?) number[] See resolve options Converts to linear Display P3 channels: [r, g, b, alpha].
convert.colorToOklab(value, opt?) number[] See resolve options Converts to Oklab channels: [l, a, b, alpha].
convert.colorToOklch(value, opt?) number[] See resolve options Converts to Oklch channels: [l, c, h, alpha].
convert.colorToProphotoRgb(value, opt?) number[] See resolve options Converts to ProPhoto RGB channels: [r, g, b, alpha].
convert.colorToRec2020(value, opt?) number[] See resolve options Converts to Rec. 2020 channels: [r, g, b, alpha].
convert.colorToRgb(value, opt?) number[] See resolve options Converts to sRGB channels: [r, g, b, alpha].
convert.colorToXyz(value, opt?) number[] See resolve options Converts to CIE XYZ channels (defaults to D65): [x, y, z, alpha].
convert.colorToXyzD50(value, opt?) number[] See resolve options Converts to CIE XYZ channels with D50 white point.
convert.numberToHex(value) string None Converts a numeric value to a two-digit hexadecimal string.

utils

Internal tools exposed for advanced usage, parsing, and validation.

Function Returns Options (opt) Description
utils.cssCalc(value, opt?) string opt.dimension
(+ see resolve options)
Resolves CSS calc() expressions.
utils.cssVar(value, opt?) string opt.customProperty
(+ see resolve options)
Resolves CSS var() expressions.
utils.extractDashedIdent(value) string[] None Extracts custom property names (dashed-ident tokens) from a value.
utils.isColor(value, opt?) boolean See resolve options Returns true if the string is a valid CSS color.
utils.isGradient(value, opt?) boolean See resolve options Returns true if the string is a valid CSS gradient.
utils.resolveGradient(value, opt?) string See resolve options Deprecated: Use resolveGradient directly instead.
utils.resolveLengthInPixels(value, unit, opt?) number opt.dimension
(+ see resolve options)
Converts an absolute or relative CSS length to pixels.
utils.splitValue(value, opt?) string[] opt.delimiter <string>
opt.preserveComment <boolean>
Safely splits a CSS value by a specified delimiter (, ,, /).

Acknowledgments

The following resources have been of great help in the development of this library:


Copyright (c) 2024 asamuzaK (Kazz)

Current Tags

  • 7.2.0                                ...           latest (19 days ago)
  • 2.8.3                                ...           legacy (2 years ago)
  • 2.8.3-b.2                                ...           next (2 years ago)

92 Versions

  • 7.2.0                                ...           19 days ago
  • 7.1.9                                ...           a month ago
  • 7.1.8                                ...           a month ago
  • 7.1.7                                ...           a month ago
  • 7.1.6                                ...           a month ago
  • 7.1.5                                ...           2 months ago
  • 7.1.4                                ...           2 months ago
  • 7.1.3                                ...           2 months ago
  • 7.1.2                                ...           2 months ago
  • 7.1.1                                ...           2 months ago
  • 7.1.0                                ...           2 months ago
  • 7.0.1                                ...           2 months ago
  • 7.0.0                                ...           3 months ago
  • 6.0.7                                ...           4 months ago
  • 6.0.6                                ...           4 months ago
  • 6.0.5                                ...           4 months ago
  • 6.0.4                                ...           5 months ago
  • 6.0.3                                ...           5 months ago
  • 6.0.2                                ...           6 months ago
  • 6.0.1                                ...           7 months ago
  • 6.0.0                                ...           7 months ago
  • 5.1.11                                ...           7 months ago
  • 5.1.10                                ...           8 months ago
  • 5.1.9                                ...           8 months ago
  • 5.1.8                                ...           8 months ago
  • 5.1.6                                ...           8 months ago
  • 5.1.5                                ...           8 months ago
  • 5.1.4                                ...           8 months ago
  • 5.1.1                                ...           8 months ago
  • 5.1.0                                ...           8 months ago
  • 5.0.1                                ...           9 months ago
  • 5.0.0                                ...           9 months ago
  • 4.1.2                                ...           10 months ago
  • 4.1.1                                ...           a year ago
  • 4.1.0                                ...           a year ago
  • 4.0.5                                ...           a year ago
  • 4.0.4                                ...           a year ago
  • 4.0.3                                ...           a year ago
  • 4.0.2                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 3.2.0                                ...           2 years ago
  • 3.1.7                                ...           2 years ago
  • 3.1.6                                ...           2 years ago
  • 3.1.5                                ...           2 years ago
  • 3.1.4                                ...           2 years ago
  • 3.1.3                                ...           2 years ago
  • 3.1.2                                ...           2 years ago
  • 3.1.1                                ...           2 years ago
  • 3.0.15                                ...           2 years ago
  • 3.0.14                                ...           2 years ago
  • 3.0.13                                ...           2 years ago
  • 3.0.12                                ...           2 years ago
  • 3.0.10                                ...           2 years ago
  • 3.0.9                                ...           2 years ago
  • 3.0.8                                ...           2 years ago
  • 3.0.7                                ...           2 years ago
  • 3.0.6                                ...           2 years ago
  • 3.0.5                                ...           2 years ago
  • 3.0.4                                ...           2 years ago
  • 3.0.2                                ...           2 years ago
  • 3.0.1                                ...           2 years ago
  • 2.8.3                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.8.3-b.2                                ...           2 years ago
  • 2.8.3-b.1                                ...           2 years ago
  • 2.8.2                                ...           2 years ago
  • 2.8.1                                ...           2 years ago
  • 2.7.1                                ...           2 years ago
  • 2.7.0                                ...           2 years ago
  • 2.6.7                                ...           2 years ago
  • 2.6.6                                ...           2 years ago
  • 2.6.5                                ...           2 years ago
  • 2.6.4                                ...           2 years ago
  • 2.6.3                                ...           2 years ago
  • 2.6.2                                ...           2 years ago
  • 2.6.1                                ...           2 years ago
  • 2.6.0                                ...           2 years ago
  • 2.5.0                                ...           2 years ago
  • 2.4.0                                ...           2 years ago
  • 2.3.0                                ...           2 years ago
  • 2.2.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Total 187
Today 0
This Week 1
This Month 24
Last Day 0
Last Week 11
Last Month 163
Dev Dependencies (20)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE