type Rgb = {
  r: number
  g: number
  b: number
}

function hexToRgb(hex: string): Rgb {
  const value = hex.replace('#', '')
  const normalized =
    value.length === 3
      ? value
          .split('')
          .map((char) => `${char}${char}`)
          .join('')
      : value

  const intValue = Number.parseInt(normalized, 16)

  return {
    r: (intValue >> 16) & 255,
    g: (intValue >> 8) & 255,
    b: intValue & 255,
  }
}

export function withAlpha(hex: string, alpha: number): string {
  const { r, g, b } = hexToRgb(hex)
  return `rgba(${r}, ${g}, ${b}, ${alpha})`
}

export function lighten(hex: string, amount: number): string {
  const { r, g, b } = hexToRgb(hex)

  const blend = (channel: number): number =>
    Math.round(channel + (255 - channel) * amount)

  return `rgb(${blend(r)}, ${blend(g)}, ${blend(b)})`
}
