Skip to content

Commit

Permalink
expand shorthand hex to 6-digit hex. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
scottcorgan committed Oct 22, 2015
1 parent b394f63 commit 75130b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
43 changes: 29 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
module.exports = function hexToRgb (hex) {

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

if (hex.length === 3) {
hex = expand(hex)
}

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

return [r, g, b];
};
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('');
}

var arr = hex.split('')
arr.shift()
return arr.join('')
}

function expand (hex) {

return hex
.split('')
.reduce(function (accum, value) {

return accum.concat([value, value])
}, [])
.join('')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hex-to-rgb",
"version": "1.0.0",
"version": "1.0.1",
"description": "Convert color hex value to rgb",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var hexToRgb = require('./index');
var test = require('tape');

test('converts', function (t) {

t.deepEqual(hexToRgb('000000'), [0,0,0], 'black converted');
t.deepEqual(hexToRgb('000'), [0,0,0], 'black shorthand converted');
t.deepEqual(hexToRgb('#ffffff'), [255,255,255], 'white converted with hash');
Expand Down

0 comments on commit 75130b7

Please sign in to comment.