Last active
October 1, 2019 19:30
-
-
Save ecasilla/f8297086161a5cd94bf3 to your computer and use it in GitHub Desktop.
Hex2Rgb.js
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
function hex (hex){ | |
if(/^#/.test(hex)){ | |
hex = hex.slice(1); | |
} | |
if(hex.length !== 3 && hex.length !== 6 ){ | |
throw new Error("Invaild hex String"); | |
} | |
var digit = hex.split(""); | |
if(digit.length === 3){ | |
digit = [ digit[0],digit[0],digit[1],digit[1],digit[2],digit[2] ] | |
} | |
var r = parseInt( [digit[0],digit[1] ].join(""), 16 ); | |
var g = parseInt( [digit[4],digit[5] ].join(""), 16 ); | |
var b = parseInt( [digit[2],digit[3] ].join(""), 16 ); | |
return [r,g,b]; | |
} |
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
var assert = require('assert'); | |
var hex2rgb = require('./hex2rgb'); | |
describe("hex2rgb",function(){ | |
it("should throw and error if the arity is incorrect",function(){ | |
assert.throws(function(){ | |
hex2rgb("shmee"); | |
},/Invaild hex String/,"You should read my api docs :/ "); | |
}); | |
it("should return a correct rgb val",function(){ | |
var rgb = hex2rgb("#fff"); | |
assert.deepEqual(rgb,[255,255,255],"Your kung fu is strong and white"); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
digit = [ digit[0],digit[0],digit[1],digit[1],digit[2],digit[2] ] (missing ;)
Just saying :)