Files
fe.ems.vue3/src/utils/generate-utils.ts
2024-02-06 12:00:48 +08:00

27 lines
673 B
TypeScript

/**
* 随机生成颜色代码
* @returns #f716ed
*/
export function generateColorHEX(): string {
const str = Math.floor(Math.random() * (256 * 256 * 256 - 1)).toString(16);
return `#${str}`;
}
//
/**
* 生成随机的 RGB 颜色
* @returns rgb(24 144 255) / rgba(0,0,0,.85)
*/
export function generateColorRGBA(hasAlpha: boolean = false) {
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
if (hasAlpha) {
const alpha = Math.floor(Math.random() * 100);
return `rgb(${red}, ${green}, ${blue}, 0.${alpha})`;
}
return `rgb(${red}, ${green}, ${blue})`;
}