diff --git a/index.js b/index.js new file mode 100644 index 0000000..c27ba24 --- /dev/null +++ b/index.js @@ -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(''); +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..5f311f2 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..316f8a6 --- /dev/null +++ b/test.js @@ -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(); +}); \ No newline at end of file