Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

811 B
Raw Blame
export function withAlpha(color: string, alpha: number): string {
  const a = Math.max(0, Math.min(1, alpha))

  if (color.startsWith('#')) {
    const hex = color.slice(1)
    let r = 0
    let g = 0
    let b = 0
    if (hex.length === 3) {
      r = parseInt(hex[0] + hex[0], 16)
      g = parseInt(hex[1] + hex[1], 16)
      b = parseInt(hex[2] + hex[2], 16)
    } else if (hex.length === 6 || hex.length === 8) {
      r = parseInt(hex.slice(0, 2), 16)
      g = parseInt(hex.slice(2, 4), 16)
      b = parseInt(hex.slice(4, 6), 16)
    } else {
      return color
    }
    return `rgba(${r}, ${g}, ${b}, ${a})`
  }

  const rgbMatch = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/i)
  if (rgbMatch) {
    return `rgba(${rgbMatch[1]}, ${rgbMatch[2]}, ${rgbMatch[3]}, ${a})`
  }

  return color
}