What is pretty-easy-hex-to-rgb?
pretty-easy-hex-to-rgb is a simple NodeJS module for converting a HEX color value to it's corresponding rgb value.
$ npm install pretty-easy-hex-to-rgb
After installing the module (localy in your project directory), in order to use it in your file you first need to require it.
let hexToRGB = ;
or if you use TypeScript
The module returns a function for you to call and supply it with a HEX color value that you'd like to be transformed into the corresponding rgb value. The function returns an Array of numbers, in the order of red color value, green color value and blue color value or an instance of Error class if the invalid HEX color representation was passed.
- hashes are optional (both '#f00' and 'f00' will produce the same output),
- you can use both lowercase and UPPERCASE letters ('F00' and 'f00' will produce the same output),
- you can use both 3 and 6 character notation for the HEX value ('FF0000' and 'F00' will produce the same output).
Convert HEX color to RGB color
; // [255, 0, 0]; // [0, 255, 0]; // [0, 0, 255]; // [255, 192, 203]; // [0, 128, 128]; // [255, 228, 225]/** Note that you can, but you're not forced to include a* hash symbol [#] in front of the HEX color value;* it works either way.* Also, the letters are NOT case sensitive;* value #f00 would produce the same output as F00*/
Consider the following
Since 1.2.0 the module will return an instance of an Error class, if argument passed is not a valid HEX color value, instead of throwing an error and terminating the Node process thus making it more dynamic and usable in production where you depend on the user input.
Having this in mind, I advise you to consider including a utility library to check the output data type, such as pretty-easy-data-types.
/** Only import the checks you will be using,* instead of including the whole library*/constisArray // check for instance of Array classisError // check for instance of Error class} = ;const hexToRGB = ;// You can pass any value/data type to a function// without causing your process to breakconst rgbColorValue = ;/** After converting the HEX color to its' corresponding RGB(a) value* you should perform the check on the value returned and see* if the conversion was successful.** If the value returned is of type Array the conversion was successful* and in this example we're going to make a css color value out of it* else it is an instance of an Error class* and we're just going to log it to the console*/const rgbOutput = ?`rgb()` :rgbColorValue;if console;
Want to contribute?
Great! Anyone can help make this project better - check out the github repository!
Found a bug?
Please open a an issue.
Copyright (c) 2017 Ognjen Jevremović
Licensed under the MIT License.