-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expand shorthand hex to 6-digit hex. Fixes #1
- Loading branch information
1 parent
b394f63
commit 75130b7
Showing
3 changed files
with
31 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters