繁体   English   中英

如何将颜色“清除”设置为RGB或十六进制?

[英]How to set color 'Clear' to RGB or HEX?

现在我得到一个包含名为“Clear”的颜色的对象。 我想将其设置为RGB值,我使用了一个函数来做到这一点,但是它返回了我'rgb(51,51,51)',而在角度chorme图表中将其重新设置为'#3366cc'。使用其他js libaries获取rgb值? -


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;
}
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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM