Registry / serialization / rgb2hex

rgb2hex

JSON →
library0.1.0jsnpmunverified

rgb2hex is a lightweight JavaScript library designed to parse RGB and RGBA color strings into their hexadecimal representation, including an alpha value for transparency. It operates without any external dependencies, making it suitable for environments where bundle size is critical. The current stable version is 0.2.5, released in November 2020. While updates have been infrequent since then, the library provides a focused solution for color conversion. A key differentiator is its minimal footprint and direct functionality, contrasting with larger color manipulation libraries that offer broader feature sets. It includes TypeScript type definitions for enhanced developer experience in modern projects. The library gained significant stability and security improvements between its early `v0.1.x` and `v0.2.x` releases, particularly addressing RegExp Denial of Service vulnerabilities.

npm install rgb2hex
INSTALL
IMPORT
SIG · RGB2HEX
R
rgb2hex
serializationjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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.

rgb2hex
import rgb2hex from 'rgb2hex';
import { rgb2hex } from 'rgb2hex';
The library exports a default function. Use `import rgb2hex from 'rgb2hex';` for ESM/TypeScript.
rgb2hex
const rgb2hex = require('rgb2hex');
CommonJS `require` is the historical way to import in Node.js, still valid for older projects or CJS modules.
RgbResult
import type { RgbResult } from 'rgb2hex';
import { RgbResult } from 'rgb2hex';
RgbResult is a TypeScript type, specifically imported using `import type` for clarity and bundler optimization.

Demonstrates parsing of various RGB/RGBA strings, including alpha values and the 'transparent' keyword, showing typical outputs and error handling for invalid input.

import rgb2hex from 'rgb2hex'; // Example 1: Basic RGB string const result1 = rgb2hex('rgb(210,43,255)'); console.log('RGB(210,43,255):', result1); // Expected: { hex: '#d22bff', alpha: 1 } // Example 2: RGBA string with transparency const result2 = rgb2hex('rgba(12, 173, 22, .67)'); console.log('RGBA(12,173,22,.67):', result2); // Expected: { hex: '#0cadaa', alpha: 0.67 } // Example 3: Handle transparent keyword const result3 = rgb2hex('transparent'); console.log('transparent:', result3); // Expected: { hex: '#000000', alpha: 0 } // Example 4: Invalid input (returns default/error structure) try { const result4 = rgb2hex('not a color string'); console.log('Invalid input:', result4); } catch (e) { console.error('Invalid input error:', e.message); }
Debug
Known issues
breakingOlder versions (v0.1.x) were vulnerable to Regular Expression Denial of Service (ReDoS) attacks. Specifically, v0.1.2 addressed an uncontrolled resource consumption vulnerability, and v0.1.6 implemented further fixes against ReDoS.
fix
Upgrade to rgb2hex@0.1.6 or newer (preferably the latest v0.2.x) to mitigate known security vulnerabilities related to regular expression parsing.
affects: <=0.1.5
gotchaThe parser expects 'rgb' or 'rgba' formats. It will not parse other color formats like named colors ('red'), HSL, or hexadecimal strings directly.
fix
Ensure input strings strictly adhere to `rgb(r,g,b)` or `rgba(r,g,b,a)` formats. For other color types, pre-process them or use a more comprehensive color library.
affects: >=0.1.0
deprecatedWhile CommonJS `require` is still supported, new projects should prefer ES Modules (ESM) `import` syntax for better tree-shaking and future compatibility, especially when using TypeScript.
fix
For modern JavaScript and TypeScript projects, use `import rgb2hex from 'rgb2hex';` instead of `const rgb2hex = require('rgb2hex');`.
affects: <=0.2.5
Errors
Common errors & fixes
TypeError: rgb2hex is not a function
Attempting to call `rgb2hex` as a named export when it is a default export, or incorrect CommonJS require.
fix
For ESM, use `import rgb2hex from 'rgb2hex';`. For CJS, use `const rgb2hex = require('rgb2hex');`.
Error: Could not parse RGB/RGBA string
The input string does not match the expected `rgb(...)` or `rgba(...)` format.
fix
Verify the input string format. Ensure it starts with 'rgb(' or 'rgba(', contains valid comma-separated numerical values, and ends with ')'. This error also occurs for named colors or hex strings.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
rgb2hex — npm install rgb2hex · libregistry