Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created August 11, 2015 03:36
Show Gist options
  • Select an option

  • Save JavaScript-Packer/4a5d9fe5cf7f6e2d52ba to your computer and use it in GitHub Desktop.

Select an option

Save JavaScript-Packer/4a5d9fe5cf7f6e2d52ba to your computer and use it in GitHub Desktop.
RGB to HEX color converter
function rgb2hex(s) {
return (s = s.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i)) && 4 === s.length ? "#" + ("0" + parseInt(s[1], 10).toString(16)).slice(-2) + ("0" + parseInt(s[2], 10).toString(16)).slice(-2) + ("0" + parseInt(s[3], 10).toString(16)).slice(-2) : "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment