/** * 随机生成颜色代码 * @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 isDark = document.documentElement.getAttribute('data-theme') === 'dark'; let red: number; let green: number; let blue: number; if (isDark) { // 暗色模式下生成较亮的颜色 red = Math.floor(Math.random() * 156) + 100; // 100-255 green = Math.floor(Math.random() * 156) + 100; // 100-255 blue = Math.floor(Math.random() * 156) + 100; // 100-255 // 确保至少有一个通道的值较高,使颜色更明亮 const brightChannel = Math.floor(Math.random() * 3); switch (brightChannel) { case 0: red = Math.min(255, red + 50); break; case 1: green = Math.min(255, green + 50); break; case 2: blue = Math.min(255, blue + 50); break; } } else { // 亮色模式下生成正常的颜色 red = Math.floor(Math.random() * 256); // 0-255 green = Math.floor(Math.random() * 256); // 0-255 blue = Math.floor(Math.random() * 256); // 0-255 } if (hasAlpha) { const alpha = Math.floor(Math.random() * 100); return `rgba(${red}, ${green}, ${blue}, 0.${alpha})`; } return `rgb(${red}, ${green}, ${blue})`; }