Function RGBAToHexAlpha

  • Converts an RGBA or RGB string to a hexadecimal color value with optional alpha channel.

    Parameters

    • rgba: string

      The RGBA or RGB string to convert.

    • forceRemoveAlpha: boolean = false

      Whether to remove the alpha channel from the resulting hexadecimal color value. Default is false.

    Returns string

    The hexadecimal color value.

    Throws

    Error if the input RGBA/RGB string is invalid.

    Example

    console.log(RGBAToHexAlpha('rgba(255, 0, 0, 1)')); // Output: '#ff0000ff'
    console.log(RGBAToHexAlpha('rgb(255, 0, 0)')); // Output: '#ff0000'
    console.log(RGBAToHexAlpha('rgba(255, 0, 0, 0.5)'));// Output: '#ff000080'
    console.log(RGBAToHexAlpha('rgba(255, 0, 0, 0.5)', true)); // Output: '#ff0000'