Skip to content

Instantly share code, notes, and snippets.

@Mottie
Created July 2, 2010 15:17
Show Gist options
  • Save Mottie/461493 to your computer and use it in GitHub Desktop.
Save Mottie/461493 to your computer and use it in GitHub Desktop.
Return Hex color from RGB format
/* Javascript function to convert rgb format to a hex color
* call : rgb2hex( "rgb(0, 70, 255)" );
* returns: #0046ff
*/
function rgb2hex(rgb){
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2);
}
@Mottie
Copy link
Author

Mottie commented Oct 12, 2010

Removed internal function & its calls.

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