/**
 * 获取随机颜色
 * @param baseColor 基础颜色
 * @param alpha 透明度 
 */
export const getRandomColor = (baseColors: string[], alpha?: number) => {
  const randomIndex = Math.floor(Math.random() * baseColors.length);
  const randomAlpha = Math.random();
  alpha = alpha || randomAlpha;
  const r = parseInt(baseColors[randomIndex].slice(1, 3), 16);
  const g = parseInt(baseColors[randomIndex].slice(3, 5), 16);
  const b = parseInt(baseColors[randomIndex].slice(5, 7), 16);
  const bColor = Number((b * alpha).toFixed(0)).toString(16);
  // 返回hex颜色
  return `#${r.toString(16)}${g.toString(16)}${bColor.length < 2 ? '0' + bColor : bColor}`
}

/**
 * 改变颜色亮度
 * @param color 颜色
 * @param amount 改变值
 * @returns 
 */
export function lightenColor(color: string, amount: number): string {
  let usePound = false;
  if (color[0] === '#') {
    color = color.slice(1);
    usePound = true;
  }

  const num = parseInt(color, 16);
  let r = (num >> 16) + amount;
  if (r > 255) r = 255;
  else if (r < 0) r = 0;

  let b = ((num >> 8) & 0x00ff) + amount;
  if (b > 255) b = 255;
  else if (b < 0) b = 0;

  let g = (num & 0x0000ff) + amount;
  if (g > 255) g = 255;
  else if (g < 0) g = 0;

  return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16).padStart(6, '0');
}