Registry / serialization / csscolorparser

csscolorparser

JSON →
library1.0.3jsnpmunverified

csscolorparser is a lightweight JavaScript utility designed to parse CSS color strings into an RGBA array. It was initially released in 2012 and has not seen updates since version 1.0.3, published approximately 9 years ago. The package directly exports a single function, `parseCSSColor`, which takes a string (e.g., 'rgba(255, 128, 12, 0.5)', '#fff', 'slateblue') and returns a four-element array `[R, G, B, A]` where R, G, B are integers (0-255) and A is a float (0-1), or `null` for invalid input. Its primary differentiator is its simplicity and lack of dependencies, but its age means it only supports older CSS Color Module Level 3/4 formats and lacks support for newer specifications like HWB, LAB, LCH, Oklab, Oklch, or the `color()` function. It operates as a CommonJS module with interop for ESM.

serialization
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

The package exports a single function as its default, so named imports are incorrect. For CommonJS, use `const parseCSSColor = require('csscolorparser');`.

import parseCSSColor from 'csscolorparser';

Demonstrates importing the default `parseCSSColor` function and using it to parse various valid and invalid CSS color strings, logging the RGBA array or null.

import parseCSSColor from 'csscolorparser'; console.log(parseCSSColor('rgba(255, 128, 12, 0.5)')); // Expected: [ 255, 128, 12, 0.5 ] console.log(parseCSSColor('#fff')); // Expected: [ 255, 255, 255, 1 ] console.log(parseCSSColor('slateblue')); // Expected: [ 106, 90, 205, 1 ] console.log(parseCSSColor('hsla(900, 15%, 90%, 0.5)')); // Expected: [ 226, 233, 233, 0.5 ] console.log(parseCSSColor('invalid-color-string')); // Expected: null console.log(parseCSSColor('rgb(100% 0% 0%)')); // Expected: [255, 0, 0, 1] (Supports percentages) console.log(parseCSSColor('hsl(240, 100%, 50%)')); // Expected: [0, 0, 255, 1]
Debug
Known footguns
breakingThe package is explicitly noted to be 'not spec compliant' for certain `hsl` and `hsla` inputs. For example, `hsl(900, 0.15, 90%)` is parsed, but the `0.15` saturation is non-standard. Always validate outputs against current CSS specifications.
gotchaThe parser returns `null` for `hsla(H, S, L)` without an explicit alpha channel (e.g., `hsla(900, 15%, 90%)`), even though the CSS spec defaults alpha to 1 in such cases. Explicitly use `hsla(H, S, L, 1)` or `hsl(H, S, L)` if you intend for full opacity.
breakingThis library has not been updated in approximately 9 years. It does not support newer CSS Color Module Level 4 and 5 syntaxes such as `hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()`, `color()`, or `color-mix()` functions, which are now widely supported in modern browsers.
gotchaHex color strings must start with a `#` symbol. Inputs like `'ffffff'` will return `null` instead of parsing as a white color.
gotchaThe `parseCSSColor` function uses `module.exports = parseCSSColor` as its export mechanism, making it a CommonJS module with a default export. While ESM `import parseCSSColor from 'csscolorparser';` works due to Node.js's CJS-ESM interop, importing via named imports (`import { parseCSSColor } from 'csscolorparser';`) will fail.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
13 hits · last 30 days
gptbot
4
ahrefsbot
4
amazonbot
4
script
1
Resources