3

Is there a quick way I can covert any css color such as #ffe, whitesmoke, or hsl(20,50%,80%), to a rgb color like rgb(140,75,20)?

Can I use the .css() method on a variable to convert the color to rgb somehow?

Bennett
  • 959
  • 4
  • 12
  • 18

1 Answers1

-1

This may answer your question - RGB to Hex and Hex to RGB

function hexToRgb(hex) {
   var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
   return result ? {
       r: parseInt(result[1], 16),
       g: parseInt(result[2], 16),
       b: parseInt(result[3], 16)
   } : null;
}

alert( hexToRgb("#0033ff").g ); // "51";
Community
  • 1
  • 1
JSess
  • 638
  • 4
  • 13
  • Thanks! however I'm looking for a way to convert _any_ CSS color, not just hex.. This is a step in the right direction though. – Bennett Dec 08 '12 at 18:55
  • I've not seen an all-in-one before, so you may need to do some digging. Here's what I came across for HSL - http://stackoverflow.com/questions/11804027/farbtastic-convert-hsl-back-to-rgb-or-hex. – JSess Dec 08 '12 at 19:09