Skip to content

Commit

Permalink
Make it so
Browse files Browse the repository at this point in the history
  • Loading branch information
scottcorgan committed Dec 1, 2014
1 parent c8a11ca commit c034d43
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = function hexToRgb (hex) {

if (hex.charAt && hex.charAt(0) === '#') {
hex = removeHash(hex);
}

var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;

return [r, g, b];
};

function removeHash (hex) {

var arr = hex.split('');
arr.shift();
return arr.join('');
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "hex-to-rgb",
"version": "1.0.0",
"description": "Convert color hex value to rgb",
"main": "index.js",
"scripts": {
"test": "node test.js | tap-spec"
},
"repository": {
"type": "git",
"url": "https://github.com/scottcorgan/hex-to-rgb.git"
},
"author": "Scott Corgan",
"license": "MIT",
"bugs": {
"url": "https://github.com/scottcorgan/hex-to-rgb/issues"
},
"homepage": "https://github.com/scottcorgan/hex-to-rgb",
"devDependencies": {
"tap-spec": "^2.1.0"
}
}
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var hexToRgb = require('./index');
var test = require('tape');

test('converts', function (t) {

t.deepEqual(hexToRgb('000000'), [0,0,0], 'black converted');
t.deepEqual(hexToRgb('#ffffff'), [255,255,255], 'white converted with hash');
t.end();
});

0 comments on commit c034d43

Please sign in to comment.