mrkeyoor.com_
Wed 23 Sept 00:34 UTC
npmWeb Frontendupdated 22 Sept 2026

parse-css-color review

parse-css-color 0.2.1 accepts literal CSS color strings and returns `{ type, values, alpha }` or null. It covers 3, 4, 6, and 8-digit hex; comma and space forms of rgb() and hsl(); numeric or percentage RGB channels; deg, rad, and turn hues; named colors; and transparent. RGB-like inputs become byte channels, while HSL stays HSL. The 0.2.1 release fixed the bundled declaration file rather than expanding CSS syntax. There is no support for currentColor, CSS variables, lab(), oklch(), color(), or color-mix(), and no conversion or formatting API.

Verdict

parse-css-color 0.2.1 added 7.2 KB minified and 3 KB gzipped in our browser build, and its 3-package install had 0 audit findings. Use it for a deliberately small literal-color grammar; choose a current color engine when modern CSS functions, strict rejection, conversion, or manipulation are part of the job.

We installed it

Lab card: what happened when we installed parse-css-colorScreenshot of parse-css-color documentation
Install✓ · 0.8s3 packages on disk · 1 MB
ImportESM import works · require() works · CommonJS package
Browser3 KBgzipped (7.2 KB minified), bundled with esbuild
TypesTypeScript types bundled
Known vulns00 critical · 0 high · 0 moderate · 0 low (npm audit)

Answers from our run

Does parse-css-color install cleanly?

Yes. In a fresh container with an empty cache, npm install parse-css-color finished in 0.8s, leaving 3 packages and 1 MB on disk. npm audit reported no known vulnerabilities.

How much does parse-css-color add to a browser bundle?

3 KB gzipped (7.2 KB minified) when the whole package is bundled for the browser with esbuild. Importing only part of it is usually smaller.

Does parse-css-color work with both ESM and CommonJS?

Yes. Both import 'parse-css-color' and require('parse-css-color') worked in Node 22 in our run. The package is published as CommonJS.

Does parse-css-color include TypeScript types?

Yes, type declarations ship inside the package, so no @types install is needed.

parse-css-color or color-parse: which should you use?

color-parse: Choose it for a similarly focused parser with a different output model and syntax coverage. parse-css-color 0.2.1 added 7.2 KB minified and 3 KB gzipped in our browser build, and its 3-package install had 0 audit findings.

When should you not use parse-css-color?

Input may contain currentColor, inherit, var(), calc(), lab(), lch(), oklab(), oklch(), color(), or color-mix(); version 0.2.1 cannot resolve or parse them.

API stability4/5Version 0.2.1 still exposes one default parser and one small result object with type, values, and alpha. The changelog from 0.1.0 through 0.2.1 shows no public redesign, and the latest patch corrected declarations. The package remains below 1.0, has no exports map, and types result.type as a general string. A future syntax update could therefore change accepted input or packaging with less compatibility expectation than a mature major release.
Docs4/5The README enumerates supported hex widths, RGB and HSL forms, hue units, keywords, transparent, and two unsupported contextual values. Examples print exact objects for alpha, percentages, mixed-channel rejection, and clamped values, with the tests linked for more cases. It does not call out missing input trimming, unwrapped degree hues, null for non-strings, or the broad string type in declarations, all of which affect validation code.
Maintenance2/5Version 0.2.1 was published on April 7, 2022, and GitHub reports the last push on August 28, 2023. The repository is not archived, has 15 stars, and shows 5 open issues and pull requests. The latest patch fixed TypeScript declarations, but there has been no release covering newer CSS Color functions or subsequent specification growth. High transitive use has not translated into visible current maintenance.
Ecosystem3/5npm counted 3,310,058 downloads in the latest completed week. CommonJS, an ESM module field, a UMD browser file, and bundled declarations make version 0.2.1 easy to place in older and newer builds. The package stops after parsing and has 15 GitHub stars, so it does not provide the converters, serializers, contrast tools, plugins, or newer color-space modules found around colord and larger color libraries.

Use it if

  • A color input accepts only literal hex, RGB, HSL, named colors, or transparent and needs a null result for invalid syntax.
  • The same small parser must run in CommonJS, ESM, and a browser without consulting computed styles or the DOM.
  • Downstream code can preserve separate rgb and hsl result types and does not need color-space conversion from this package.
  • Forgiving numeric input is desired, including rounding and clamping channels into their allowed ranges.
Skip it if

Setup reality

We installed parse-css-color 0.2.1 in a fresh Node 22 Bookworm sandbox. npm completed in 0.8 seconds, left 3 packages using 1 MB, and reported 0 known vulnerabilities. The package is 44 KB unpacked with 2 direct dependencies and no peers. It publishes CommonJS without an exports map, includes TypeScript declarations, and both require() and ESM import worked. Our browser build measured 7.2 KB minified and 3 KB gzipped.

There are no credentials, native builds, globals to configure, or runtime options. The function returns null for unsupported syntax and non-string input, so guard before destructuring. It does not trim the string. Decide whether your boundary should call trim(), since accepting pasted whitespace is reasonable for a form but can conceal malformed machine input in a parser or linter.

Result values are not one canonical color space. Hex, RGB, named colors, and transparent produce type rgb; HSL input produces type hsl. Alpha is separate in both cases. Numeric RGB channels are rounded and clamped to 0 through 255, saturation and lightness to 0 through 100, and alpha to 0 through 1. Mixed percentage and numeric RGB channels return null.

Hue handling has another edge: turns and radians are converted to rounded degrees, while degree input is not wrapped into 0 through 359. TypeScript declares result.type as string instead of the narrower rgb or hsl union. Add your own normalizer and runtime schema when downstream code needs exhaustive branching, strict ranges, or a single output model.

Patterns

Read hex with optional alpha parse-hex

import parse from 'parse-css-color'
parse('#0af')
parse('#00aaff80')

Four and 8-digit forms put alpha in the last component; the returned alpha is a 0 through 1 number.

Check unsupported input before use guard-null

const color = parse(userInput)
if (color === null) return { ok: false, error: 'unsupported color' }
const { type, values, alpha } = color

Invalid syntax, unsupported CSS functions, and non-string values return null rather than throwing.

Choose whether form whitespace is valid trim-form-value

const color = parse(colorInput.value.trim())
colorInput.setCustomValidity(color ? '' : 'Enter a literal CSS color')

Version 0.2.1 does not trim. Its tests reject leading or trailing spaces around transparent.

Read space syntax and slash alpha parse-modern-rgb

const color = parse('rgb(255 0 153 / 20%)')
// { type: 'rgb', values: [255, 0, 153], alpha: 0.2 }

All 3 RGB channels must be numbers or all percentages; mixed channel units return null.

Convert percentage channels to bytes parse-percent-rgb

const color = parse('rgb(41.2% 69.88% 96.64%)')
// values: [105, 178, 246]

Percentage channels are multiplied by 255 and rounded, so the original percentage precision is not preserved.

Convert turns and radians to degrees parse-hsl-units

parse('hsl(.75turn 60% 70% / 50%)')
parse('hsl(4.71239rad 60% 70%)')

Both examples yield hue 270, but the result type remains hsl and is not converted to RGB.

Resolve a CSS named color parse-name

const color = parse('RebeccaPurple')
// { type: 'rgb', values: [102, 51, 153], alpha: 1 }

Name lookup is case-insensitive. System colors and misspellings return null.

Represent transparent black parse-transparent

const color = parse('transparent')
// { type: 'rgb', values: [0, 0, 0], alpha: 0 }

RGB channels have no visible meaning at alpha 0, so do not use them to preserve an author's hidden color.

Separate literals from CSS context reject-context-values

for (const value of ['currentColor', 'var(--brand)', 'oklch(60% .2 20)']) {
  if (parse(value) === null) console.log('requires CSS evaluation', value)
}

The package cannot resolve computed values or modern color functions; null does not mean the CSS itself is invalid.

Validate ranges before forgiving parsing detect-clamping

const raw = 'rgb(500 -100 12.6 / 200%)'
const color = parse(raw)
// values: [255, 0, 13], alpha: 1

Version 0.2.1 clamps and rounds numeric channels. Add a strict pre-check when range errors must be reported.

Handle RGB and HSL separately branch-on-model

const color = parse(input)
if (!color) throw new TypeError('unsupported color')
if (color.type === 'rgb') renderRgb(color.values, color.alpha)
else if (color.type === 'hsl') renderHsl(color.values, color.alpha)

Bundled types call type a string, so TypeScript does not enforce an exhaustive rgb and hsl switch.

Load the CommonJS entry use-commonjs

const parse = require('parse-css-color')
const color = parse('rgba(255, 0, 0, 0.5)')

require() worked in our Node 22 check; the package has no exports map and points main at dist/index.cjs.js.

Alternatives

PackageRegistryPick it when
color-parsenpmChoose it for a similarly focused parser with a different output model and syntax coverage.
colordnpmChoose it when parsing must lead into conversion, formatting, manipulation, or plugin-based color spaces.
colornpmChoose it for a fluent Node and browser API covering conversion, mixing, lightening, and contrast.

More web frontend guides

postcss · react · react-dom · tailwindcss · htmlparser2 · tailwind-merge · the whole shelf →

How this guide is made: grounded in the library's documentation, release notes, changelog, and issue history, on a fixed rubric — not a hands-on install of every release. The 50 most-downloaded entries are additionally install-verified in clean containers. Corrections: contact the desk.