Skip to content

Instantly share code, notes, and snippets.

@32teeth
Last active October 3, 2016 17:06
Show Gist options
  • Save 32teeth/a93e8e0c3dceff345bd6272ad0f6b53a to your computer and use it in GitHub Desktop.
Save 32teeth/a93e8e0c3dceff345bd6272ad0f6b53a to your computer and use it in GitHub Desktop.
var color_utils = (function(){
function getLong(color)
{
return color[0] << 16 | color[1] << 8 | color[2];
}
function getRGB(color)
{
if(color.indexOf("#") == -1)
{
var r = (color>>16);
var g = ((color>>8) & 0xff);
var b = (color & 0xff);
}
else
{
var color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
var r = parseInt(color[1], 16);
var g = parseInt(color[2], 16);
var b = parseInt(color[3], 16);
}
var rgb = [r,g,b];
return rgb;
}
function getHEX(color)
{
if(color instanceof Array)
{
var r = color[0];
var g = color[1];
var b = color[2];
return getHEX(getLong(r,g,b));
}
else
{
var hex = Number(color).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return "#" + hex;
}
}
return {
getLong:function(r,g,b){return getLong(r,g,b);},
getRGB:function(color){return getRGB(color);},
getHEX:function(color){return getHEX(color);},
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment