Skip to content

Instantly share code, notes, and snippets.

@dhoko
Created May 29, 2013 17:01
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 dhoko/5671907 to your computer and use it in GitHub Desktop.
Save dhoko/5671907 to your computer and use it in GitHub Desktop.
Convert color
var color = {
rgb : function() {
var color = [Math.floor(Math.random()*256),Math.floor(Math.random()*256),Math.floor(Math.random()*256)];
return 'rgb('+color.join(',')+')';
},
rgb2Hex : function(color) {
var hex = "#",
c = color.split('(')[1].split(')')[0].split(',');
hex += parseInt(c[0]).toString(16)+parseInt(c[1]).toString(16)+parseInt(c[2]).toString(16);
return hex;
},
hex : function() {
return '#'+Math.floor(Math.random()*16777215).toString(16);
},
hex2rgb : function(color) {
var str = color.split('#')[1],
c = [str.substr(0,2),str.substr(2,2),str.substr(4,2)];
return 'rgb('+parseInt(c[0],16)+','+parseInt(c[1],16)+','+parseInt(c[2],16)+')';
},
fromPicker : function(o) {
return 'rgb('+o.r+','+o.g+','+o.b+')';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment