Skip to content

Commit

Permalink
fix: fix the way to get the tmp element (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Jul 30, 2024
1 parent c499a30 commit 7cefa29
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/color/torgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ const RGB_REG = /rgba?\(([\s.,0-9]+)\)/;
* 创建辅助 tag 取颜色
* @returns
*/
function createTmp(): HTMLElement {
const i = document.createElement('i');
function getOrCreateTmp(): HTMLElement {
let i = document.querySelector('i[title="Web Colour Picker"]') as HTMLElement;
if (i) {
return i;
}
i = document.createElement('i');
i.title = 'Web Colour Picker';
i.style.display = 'none';
document.body.appendChild(i);
return i;
}

let iEl: HTMLElement;

/**
* 将颜色转换到 rgb 的格式
* @param {color} color 颜色
Expand All @@ -28,10 +30,7 @@ function toRGBString(color: string): string {
return color;
}

if (!iEl) {
// 防止防止在页头报错
iEl = createTmp();
}
const iEl = getOrCreateTmp();

iEl.style.color = color;

Expand Down

0 comments on commit 7cefa29

Please sign in to comment.