Back to Portfolio

hex2rgb

Node/Bower Module

An npm/Bower module that converts hex color to rgb. Calculates corresponding foreground (either black or white).

npmjs.com/package/hex2rgb

April 2015


hex2rgb

is a Node.js (npm) and Bower module I made that converts hex color to rgb. It also calculates the appropriate corresponding foreground (either black or white).

hex 2 RGB NPM

Inspiration

I currently use hex2rgb in my Angular prototype app and decided to make it a public module since I plan to use it in more projects like my React Native app. I hope this module comes in handy to someone else, particularly its yiq (foreground) feature.


Testing/Coverage

I use Mocha/Chai for testing, Travis CI as well for test & deploy and Code Climate for code coverage checking.

mocha
chai
travis


npm test

And here's an excerpt from the README I made for hex2rgb:

API

hex2rgb( hex {String}, options {Object} )

hex

A hex-only string of 3 or 6 characters. If the string has a # prefix, the # gets trimmed off.

{debug: true | false}

You can pass {debug:true} to enable errors logged to console.

{rgbStringDefault: "String e.g. transparent | black | #e9e9e9"}

As of v2 you can specify a default string that .rgbString will return when hex input is invalid or yet to be calculated. In v1, .rgbString returned "rgb(0,0,0)" (black).

{yiqDefault: "String e.g. inherit | gray | #333"}

Similar to rgbStringDefault above. In v1 .yiq returned "white" by default.

.rgb

Returns an array in [r, g, b] format. If hex input is invalid or yet to be calculated [255, 255, 255] (white) is returned as a fallback.

.rgbString

Returns a string in rgb(r, g, b) format. If hex input is invalid or yet to be calculated, either 'inherit' or your specified string value is returned as a fallback.

.yiq

Returns a string of either 'white' or 'black'. If hex input is invalid or yet to be calculated, either 'inherit' or your specified string value is returned as a fallback.