Skip to content

Instantly share code, notes, and snippets.

@christophermark
Created December 18, 2025 18:43
Show Gist options
  • Select an option

  • Save christophermark/064561c4d824f07ccc733d7f42a48b76 to your computer and use it in GitHub Desktop.

Select an option

Save christophermark/064561c4d824f07ccc733d7f42a48b76 to your computer and use it in GitHub Desktop.
makeHexTransparent
/**
* Adds transparency to a hex color by appending an alpha value.
*
* @param hexColor - A 6-digit hex color string (e.g., '#C1E38C' or 'C1E38C')
* @param alpha - A number between 0 (fully transparent) and 1 (fully opaque)
* @returns An 8-digit hex color string with the alpha value appended
*
* @example
* withAlpha('#C1E38C', 0.4) // Returns '#C1E38C66'
* withAlpha('#FF0000', 0.5) // Returns '#FF000080'
*/
export function withAlpha(hexColor: string, alpha: number): string {
const alphaHex = Math.round(alpha * 255)
.toString(16)
.padStart(2, '0')
return `${hexColor}${alphaHex}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment