# 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))
}
