Registry / serialization / color-string

color-string

JSON →
library0.0.0jsnpmunverified

color-string is a focused utility library for parsing and generating CSS-compatible color strings, supporting various formats like RGB, HSL, HWB, and named colors. The current stable version is 2.1.4. The library maintains a reasonably active release cadence, with recent updates introducing features like case-insensitive string checks and improved parsing for modern CSS color notations, alongside crucial bug fixes. Its key differentiators include its lightweight nature, explicit support for multiple color models, and direct conversion capabilities to and from different string representations, making it a reliable choice for frontend and backend color manipulation tasks where CSS compatibility is paramount. It ships with TypeScript types for enhanced developer experience.

npm install color-string
INSTALL
IMPORT
SIG · COLOR-STRING
C
color-string
serializationjavascriptv0.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

colorString
✓ import colorString from 'color-string';
✗ const colorString = require('color-string');
Since version 2.0.0, color-string is an ESM-only package. Use `import` syntax.
get
✓ import colorString from 'color-string'; const parsed = colorString.get('#FFF');
The `get` and `to` properties are nested objects on the default export for parsing and generation functions.
to
✓ import colorString from 'color-string'; const hex = colorString.to.hex(255, 255, 255);
Access generation methods via `colorString.to`.

Demonstrates parsing various CSS color strings and generating color strings in different formats (hex, rgb, rgba), including handling invalid input.

import colorString from 'color-string'; // Parsing a hex color string const hexColor = '#FFFA'; const parsedHex = colorString.get(hexColor); console.log(`Parsed ${hexColor}:`, parsedHex); // { model: 'rgb', value: [255, 255, 255, 0.6666666666666666] } // Parsing a named color to RGB values const namedColor = 'blue'; const rgbValues = colorString.get.rgb(namedColor); console.log(`RGB values for ${namedColor}:`, rgbValues); // [0, 0, 255, 1] // Generating a hex string from RGB values const generatedHex = colorString.to.hex(255, 165, 0); // Orange console.log(`Generated hex for orange:`, generatedHex); // #FFA500 // Generating an RGBA string with transparency const generatedRgba = colorString.to.rgb(128, 0, 128, 0.5); console.log(`Generated RGBA string:`, generatedRgba); // rgba(128, 0, 128, 0.5) // Handling invalid input const invalidColor = 'not-a-color'; const parsedInvalid = colorString.get.rgb(invalidColor); console.log(`Parsed invalid color:`, parsedInvalid); // null
Debug
Known issues
breakingVersion 2.0.0 transitioned color-string to an ESM-only package. This requires `import` statements and affects environments that do not support ESM or rely on CommonJS `require()`.
fix
Migrate your codebase to use ES module `import` syntax. For Node.js, ensure your `package.json` has `"type": "module"` or use `.mjs` file extensions. If stuck on CommonJS, consider using a bundler like Webpack or Rollup, or stick to an older `1.x.x` version.
affects: >=2.0.0
breakingVersion 2.0.0 removed 'swizzle' support. If your application relied on `color-string.swizzle`, this functionality is no longer available.
fix
Refactor your code to no longer depend on 'swizzle'. This might involve using direct color conversion functions or finding an alternative library for color component manipulation.
affects: >=2.0.0
breakingVersion 2.0.1 changed typings to consistently return `null` instead of `undefined` for invalid color inputs. While this was a typing change, it might affect runtime assumptions in JavaScript code relying on `undefined` checks.
fix
Ensure your code checks for `null` when handling potentially invalid color string results from `colorString.get()` or its sub-methods.
affects: >=2.0.1
breakingThe `color-string@2.1.1` package was compromised due to a supply chain attack. It is critical to avoid this specific version.
fix
Immediately upgrade to version `2.1.2` or higher. Do not use `2.1.1` under any circumstances. Check your `package-lock.json` or `yarn.lock` files.
affects: 2.1.1
gotcha`get.rgb()` for named colors had a bug returning invalid results prior to v2.1.3.
fix
Upgrade to version `2.1.3` or newer to ensure correct parsing of named colors into RGB values.
affects: >=2.0.0 <2.1.3
gotchaString checks in parsing methods became case insensitive in v2.1.4. While this is generally an improvement, if you relied on strict case sensitivity for validation (e.g., 'RGB' vs 'rgb'), this behavior has changed.
fix
Review any code that might depend on strict case-sensitive parsing of color string models. Most applications will benefit from this change, but it's a behavioral shift.
affects: >=2.1.4
Errors
Common errors & fixes
TypeError: colorString is not a function
Attempting to `require()` color-string in a CommonJS environment after version 2.0.0, which is ESM-only.
fix
Use `import colorString from 'color-string';` instead of `require()`. For Node.js, ensure your environment is configured for ESM (e.g., `"type": "module"` in `package.json`).
colorString.get.rgb('blue') returns [0, 0, 0, 0] or similar incorrect values.
A bug in versions prior to 2.1.3 caused `get.rgb()` to return invalid results when parsing named colors.
fix
Upgrade `color-string` to version `2.1.3` or newer.
Property 'model' does not exist on type 'null'.
Attempting to access properties like `model` or `value` on the result of `colorString.get()` without checking if the parsed result is `null`, which indicates an invalid color string.
fix
Always check if the result of `colorString.get()` or its sub-methods is `null` before attempting to access its properties. For TypeScript, use optional chaining or a null check, e.g., `const parsed = colorString.get(str); if (parsed) { console.log(parsed.model); }`.
Upgrade
Version history
0.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
color-string — npm install color-string · libregistry