Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created February 28, 2012 03:00
Show Gist options
  • Save minikomi/1929015 to your computer and use it in GitHub Desktop.
Save minikomi/1929015 to your computer and use it in GitHub Desktop.
Get rgb color from css color name
var name2rgb = function(str){
var canv=document.createElement("canvas");
canv.height = 1; canv.width = 1;
var ctx = canv.getContext("2d");
ctx.fillStyle="rgba(1,1,1,0.1)";
ctx.fillRect(0,0,1,1);
ctx.fillStyle=str;
ctx.fillRect(0,0,1,1);
var pxl= ctx.getImageData(0,0,1,1).data;
if(pxl[3] === 47){
return"Sorry, that color doesn't exist.";
} else {
return "rgb("+pxl[0]+", "+pxl[1] +", " +pxl[2]+")";
}
}
@matthandlersux
Copy link

OMG DO IT IN COFFEESCRIPT!!!

@minikomi
Copy link
Author

haha.. thought of a much less stupid way to do it

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