js生成随机颜色

js生成随机颜色

徐徐
前端
发布于2024-08-04 15:06:02
🌺前言
js生成暗色,亮色的几种方法

typescript
/**
 * 生成亮色
 * @returns
 */
export function randomBrightColor() {
  const saturation = Math.random(); // 饱和度在0到1之间
  const brightness = Math.random(); // 亮度在0到1之间
  return `hsl(${Math.random() * 360}, ${saturation * 100}%, ${brightness * 100}%)`;
}
/**
 *生成暗色
 * @returns
 */
export function randomDarkColor() {
  const saturation = Math.random(); // 饱和度在0到1之间
  const lightness = 1 - Math.random(); // 亮度在0到1之间,并保证是暗色
  return `hsl(${Math.random() * 360}, ${saturation * 100}%, ${lightness * 100}%)`;
}
/**
 * 现颜色
 * @returns 
 */
export function randomPrettyColor() {
    const randomHue = Math.random(); // 生成一个0到1之间的随机数作为色相
    const saturation = 0.4 + Math.random() * 0.6; // 饱和度:0.4到1之间
    const lightness = 0.4 + Math.random() * 0.4; // 亮度:0.4到0.8之间
    const hsl = `hsl(${randomHue * 360}, ${saturation * 100}%, ${lightness * 100}%)`;
   
    return hsl;
  }
文章最后更新于 2024-08-11 23:08:44
留言
暂无数据

~~空空如也