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 rgb2hexVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing of various RGB/RGBA strings, including alpha values and the 'transparent' keyword, showing typical outputs and error handling for invalid input.
Upgrade to rgb2hex@0.1.6 or newer (preferably the latest v0.2.x) to mitigate known security vulnerabilities related to regular expression parsing.
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.
For modern JavaScript and TypeScript projects, use `import rgb2hex from 'rgb2hex';` instead of `const rgb2hex = require('rgb2hex');`.For ESM, use `import rgb2hex from 'rgb2hex';`. For CJS, use `const rgb2hex = require('rgb2hex');`.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.No dependency data recorded yet.