Skip to main content

hexToHexAlpha

The hexToHexAlpha function converts a HEX color string to a HEX color string with alpha. This is useful for adding transparency to HEX colors.

Syntax

hexToHexAlpha(hex: string, alpha: number): string;

Parameters

  • hex (string): The HEX color string (e.g., #RRGGBB or #RGB).
  • alpha (number): The alpha value (0-1).

Returns

  • string: The HEX color string with alpha in the format #RRGGBBAA.

Throws

  • Error: Throws an error if the provided string is not a valid HEX color or if the alpha value is out of range.

Example

import { hexToHexAlpha } from 'colore-js';

const hexWithAlpha = hexToHexAlpha('#ff0000', 0.5);
console.log(hexWithAlpha);
// Output: '#ff000080'

const hexWithAlphaShorthand = hexToHexAlpha('#f00', 0.75);
console.log(hexWithAlphaShorthand);
// Output: '#ff0000bf'

Usage

The hexToHexAlpha function is used to add transparency to HEX colors, making it useful for web design and other applications where color manipulation is needed.