jquery - How to extract color values from rgb string in javascript -


javascript code

$(".designer-element").each(function () {         var $this = $(this),         var  moot = $this.css(["color", "border-color", "background-color"]); } ); 

is used element colors. returns colors strings like

"background-color": "rgba(0, 0, 0, 0)" "border-color": "rgb(211, 211, 211)" "color": "rgb(51, 51, 51)" 

how extract individual r , g , b values strings. value not needed. or there better method returns color values directly?

substrg cannot used since values have variable sizes 1-3 digits can regex used ?

html5, jquery, jquery-ui , bootstrap used.

you can use regex:

function getrgb(str){    var match = str.match(/rgba?\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)?(?:, ?(\d(?:\.\d?))\))?/);    return match ? {      red: match[1],      green: match[2],      blue: match[3]    } : {};  }    console.log(getrgb("rgb(211, 211, 211)"));  console.log(getrgb("rgba(211, 0, 211, 0.5)"));


Comments