Convert colors of different formats with ease. Use it to convert a HEX color code value to an RGB triplet value or the other way round.
To add the module to your project, run:
npm install --save color-formatter
Find out the type of the color value.
const Color = require('color-formatter');
/**
* Get color type.
* @param {string} colorcode - RGB or HEX value.
*/
Color.getType("255, 68, 133")
// => 'rgb'
Color.getType("#FF4485")
// => 'hex'
Convert an RGB color value to a HEX value.
const Color = require('color-formatter');
/**
* Get color type.
* @param {string} rgb - RGB value.
*/
Color.toHex("255, 68, 133");
// => #FF4485
Convert a HEX color value to an RGB value.
const Color = require('color-formatter');
/**
* Get color type.
* @param {string} hex - HEX value.
* @param {boolean} asArray - (Optional) Set it to true to get the output as an array.
*/
Color.toRGB("#F8E100");
// => '248, 225, 0'
Color.toRGB("#F8E100", true);
// => [248, 255, 0]
//Include the module in your code
const Color = require('color-formatter');
const rgb = '255, 68, 133';
const hex = '#F8E100';
Color.getType(rgb)
// => 'rgb'
Color.toRGB(hex)
// => '248, 225, 0'
// OR
// Second argument of true means we want the output as an array
Color.toRGB(hex, true);
// => [248, 255, 0]
// You can also convert to hex
Color.toHex(rgb);
// => #FF4485
MIT © Josh Ghent