1

Now I get a object which contains a color named 'Clear'. I want to set it to RGB value, and I used a function to do that, but it return me 'rgb(51, 51, 51)', while in angular chorme chart it resolove it to'#3366cc'.How can I get the rgb value with out using other js libaries? -


function(){
    var div = document.createElement('div');
    var rgbColor;
    div.style.color = 'Clear';
    document.body.appendChild(div);
    rgbColor = window.getComputedStyle(div).color
    div.remove();
    return rgbColor;
}
miranda.hu
  • 11
  • 2

1 Answers1

0
function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

function getColor(){
   var r = 0;
   var g = 0;
   var b = 0;
   var rgbColor;
   var div = document.createElement('div');

   div.style.color = 'Clear';
   document.body.appendChild(div);
   rgbColor = window.getComputedStyle(div).color
   var matches = div.style.color.match(/^rgb\((\d+), (\d+), (\d+)\)$/);

   if (matches) {
       r = parseInt(matches[1]);
       g = parseInt(matches[2]);
       b = parseInt(matches[3]);
   }
   div.remove();
   return rgbToHex(r, g, b);
}
Sagi
  • 8,972
  • 3
  • 33
  • 41