What is rgb2hex?
The rgb2hex npm package is a utility that allows you to convert RGB color values to hexadecimal color codes. This can be useful in various scenarios such as web development, graphic design, and any other field where color manipulation is required.
What are rgb2hex's main functionalities?
Convert RGB to Hex
This feature allows you to convert an RGB color string to its hexadecimal representation. In this example, the RGB color 'rgb(255, 0, 0)' is converted to the hex color '#ff0000'.
const rgb2hex = require('rgb2hex');
const hexColor = rgb2hex('rgb(255, 0, 0)');
console.log(hexColor); // { hex: '#ff0000', alpha: 1 }
Convert RGBA to Hex
This feature allows you to convert an RGBA color string to its hexadecimal representation, including the alpha value. In this example, the RGBA color 'rgba(255, 0, 0, 0.5)' is converted to the hex color '#ff0000' with an alpha value of 0.5.
const rgb2hex = require('rgb2hex');
const hexColor = rgb2hex('rgba(255, 0, 0, 0.5)');
console.log(hexColor); // { hex: '#ff0000', alpha: 0.5 }
Other packages similar to rgb2hex
color-convert
The color-convert package provides a wide range of color conversions, including RGB to Hex. It supports many other color models and conversions, making it more versatile than rgb2hex.
color
The color package is a comprehensive library for color manipulation and conversion. It supports RGB to Hex conversion along with many other color formats and operations, offering more functionality compared to rgb2hex.
tinycolor2
The tinycolor2 package is a small, fast library for color manipulation and conversion. It supports RGB to Hex conversion and offers additional features like color mixing, lightening, darkening, and more, making it a more feature-rich alternative to rgb2hex.
rgb2hex
Parse any rgb or rgba string into a hex color. Lightweight library, no dependencies!
Installation
via NPM:
$ npm install rgb2hex
via Bower
$ bower install rgb2hex
Usage
Include rgb2hex.js
in your web app, by loading it as usual:
<script src="rgb2hex.js"></script>
Using NodeJS
var rgb2hex = require('rgb2hex');
console.log(rgb2hex('rgb(210,43,2525)'));
console.log(rgb2hex('rgba(12,173,22,.67)'));
Using RequireJS
rgb2hex can be also loaded with AMD:
require(['rgb2hex'], function (rgb2hex) {
});
Contributing
Please fork, add specs, and send pull requests! In lieu of a formal styleguide, take care to
maintain the existing coding style.
Release History
- 2013-04-22 v0.1.0 first working version
- 2018-05-24 v0.1.1 updated dependencies switch test framework to jest
- 2018-06-13 v0.1.2 Fixes uncontrolled resource consumption vulnerability referenced in #1. (ref1, ref2)