Parse any rgb or rgba string into a hex color. Lightweight library, no dependencies!
via NPM:
$ npm install rgb2hex
via Bower
$ bower install rgb2hex
Include rgb2hex.js
in your web app, by loading it as usual:
<script src="rgb2hex.js"></script>
var rgb2hex = require('rgb2hex');
console.log(rgb2hex('rgb(210,43,2525)'));
/**
* returns:
* {
* hex: '#d22bff',
* alpha: 1
* }
*/
console.log(rgb2hex('rgba(12,173,22,.67)'));
/**
* returns:
* {
* hex: '#d22bff',
* alpha: 0.67
* }
*/
rgb2hex can be also loaded with AMD:
require(['rgb2hex'], function (rgb2hex) {
// ...
});
Please fork, add specs, and send pull requests! In lieu of a formal styleguide, take care to maintain the existing coding style.