Get background color in Hex - Node.js CSS

Node.js examples for CSS:Color

Description

Get background color in Hex

Demo Code


function getBgColorHex(elem){ 
  var color = elem.css('background-color');
  var hex; // w  w w . j  a va2  s .  co m
  if(color.indexOf('#')>-1)
  {
    hex = color; 
  }
  else
  { 
    var rgb = color.match(/\d+/g);
    hex = '#'+ ('0' + parseInt(rgb[0], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[2], 10).toString(16)).slice(-2); 
  } 
  return hex; 
}

Related Tutorials