@antv/util
Version:
> AntV 底层依赖的工具库,不建议在自己业务中使用。
40 lines • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toRGB = void 0;
var arr2rgb_1 = require("./arr2rgb");
var RGB_REG = /rgba?\(([\s.,0-9]+)\)/;
/**
* 创建辅助 tag 取颜色
* @returns
*/
function createTmp() {
var i = document.createElement('i');
i.title = 'Web Colour Picker';
i.style.display = 'none';
document.body.appendChild(i);
return i;
}
var iEl;
/**
* 将颜色转换到 rgb 的格式
* @param {color} color 颜色
* @return 将颜色转换到 '#ffffff' 的格式
*/
function toRGB(color) {
// 如果已经是 rgb的格式
if (color[0] === '#' && color.length === 7) {
return color;
}
if (!iEl) {
// 防止防止在页头报错
iEl = createTmp();
}
iEl.style.color = color;
var rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color');
var matches = RGB_REG.exec(rst);
var cArray = matches[1].split(/\s*,\s*/).map(function (s) { return Number(s); });
rst = (0, arr2rgb_1.arr2rgb)(cArray);
return rst;
}
exports.toRGB = toRGB;
//# sourceMappingURL=torgb.js.map