utils.tsx 959 Bytes

// 十六进制颜色 转换成 rgba
export const hexToRgba = (hex: string | null | undefined, alpha: any) => {
	let r: string | number = 0, g: string | number = 0, b: string | number = 0;

	// 3 digits
	if (hex?.length == 4) {
		r = "0x" + hex?.[1] + hex?.[1];
		g = "0x" + hex?.[2] + hex?.[2];
		b = "0x" + hex?.[3] + hex?.[3];
		// 6 digits
	} else if (hex?.length == 7) {
		r = "0x" + hex?.[1] + hex?.[2];
		g = "0x" + hex?.[3] + hex?.[4];
		b = "0x" + hex?.[5] + hex?.[6];
	}

	return "rgba(" + +r + "," + +g + "," + +b + "," + alpha + ")";
}

// // 使用示例
// const hexColor = "#ff5733"; // 16进制颜色
// const opacity = 0.5; // 透明度(0.0 到 1.0)
// const rgbaColor = hexToRgba(hexColor, opacity);
// console.log(rgbaColor); // 输出: "rgba(255, 87, 51, 0.5)"

export function setFavicon (logoPath: string) {
	const link = document.createElement('link');
	link.rel = 'icon';
	link.href = logoPath;
	document.head.appendChild(link);
}