PoshCode Archive  Artifact [16026a4a71]

Artifact 16026a4a716deaebc679b6ead3b16fe37ca17f1280310663d4730814e764551b:

  • File Remove-Alpha-from-ARGB.ps1 — part of check-in [cd008a9f30] at 2018-06-10 13:57:27 on branch trunk — Removes the alpha channel of a given ARGB Color in Hex with the specified background color and returns a new [System.Drawing.Color]. (user: Felix Bayer size: 1271)

# encoding: ascii
# api: powershell
# title: Remove Alpha from ARGB
# description: Removes the alpha channel of a given ARGB Color in Hex with the specified background color and returns a new [System.Drawing.Color].
# version: 1.0
# type: function
# author: Felix Bayer
# license: CC0
# function: Remove-Alpha
# x-poshcode-id: 5582
# x-archived: 2014-11-25T11:10:19
# x-published: 2014-11-12T10:45:00
#
# i.e. foreground #33FFFFFF + background #000000 gives #FF333333
#
function Remove-Alpha {
	param([Parameter(Mandatory=$true)]
		  [string]$ForegroundHex,
		  [string]$BackgroundHex = '#000000')
	
	Add-Type -Assembly System.Drawing -ErrorAction SilentlyContinue
	
	if(!$ForegroundHex.StartsWith('#')) {
		$ForegroundHex = '#' + $ForegroundHex
	}
	
	if(!$BackgroundHex.StartsWith('#')) {
		$BackgroundHex = '#' + $BackgroundHex
	}
	
	$fg = [System.Drawing.ColorTranslator]::FromHtml($ForegroundHex)
	$bg = [System.Drawing.ColorTranslator]::FromHtml($BackgroundHex)
	
	if ($fg.A -eq 255){
		return $fg
	}

	$alpha = $fg.A / 255.0
	$diff = 1.0 - $alpha
	return [System.Drawing.Color]::FromArgb(255,
		[byte]($fg.R * $alpha + $bg.R * $diff),
		[byte]($fg.G * $alpha + $bg.G * $diff),
		[byte]($fg.B * $alpha + $bg.B * $diff))
}