SHARE
TWEET

Untitled

a guest Jul 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function hexToRgb(hex) {
  2.   // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  3.   var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  4.   hex = hex.replace(shorthandRegex, function(m, r, g, b) {
  5.     return r + r + g + g + b + b;
  6.   });
  7.  
  8.   var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  9.   return result ? {
  10.     r: parseInt(result[1], 16),
  11.     g: parseInt(result[2], 16),
  12.     b: parseInt(result[3], 16)
  13.   } : null;
  14. }
  15.  
  16. alert(hexToRgb("#0033ff").g); // "51";
  17. alert(hexToRgb("#03f").g); // "51";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top