Skip to content

Instantly share code, notes, and snippets.

@ecasilla
Last active October 1, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecasilla/f8297086161a5cd94bf3 to your computer and use it in GitHub Desktop.
Save ecasilla/f8297086161a5cd94bf3 to your computer and use it in GitHub Desktop.
Hex2Rgb.js
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];
}
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");
})
});
@beauterre
Copy link

digit = [ digit[0],digit[0],digit[1],digit[1],digit[2],digit[2] ] (missing ;)
Just saying :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment