Functions/Convert-HexToRGB.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
function Convert-HexToRGB { <# .SYNOPSIS Converts Hex to RGB values .DESCRIPTION Converts Hex to RGB values .PARAMETER Hex An 6 character Hex value with optional # prefix .PARAMETER IncludeInput Switch to include the input in the output .EXAMPLE Convert-HexToRGB -Hex '#ffffff' -IncludeInput Hex Red Green Blue RgbString --- --- ----- ---- --------- #ffffff 255 255 255 255,255,255 .EXAMPLE Convert-HexToRGB -Hex '808080' 128,128,128 .EXAMPLE Convert-HexToRGB -Hex '808080', 'ffffff', '#abab10' -IncludeInput Hex Red Green Blue RgbString --- --- ----- ---- --------- 808080 128 128 128 128,128,128 ffffff 255 255 255 255,255,255 #abab10 171 171 16 171,171,16 #> #region Parameter [CmdletBinding(ConfirmImpact = 'None')] Param( [parameter(Mandatory, HelpMessage = 'Enter hex color val RGB in form #RRGGBB', ValueFromPipeline)] [string[]] $Hex, [switch] $IncludeInput ) #endregion Parameter begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { foreach ($curHex in $Hex) { if ($curHex -match '(#)?([0-9A-F]{6})') { #-Convert values $Red = [Convert]::ToInt32($matches[2].Substring(0, 2), 16) $Green = [Convert]::ToInt32($matches[2].Substring(2, 2), 16) $Blue = [Convert]::ToInt32($matches[2].Substring(4, 2), 16) if ($IncludeInput) { New-Object -TypeName psobject -Property ([ordered] @{ Hex = $curHex Red = $Red Green = $Green Blue = $Blue RgbString = @($Red, $Green, $Blue) -join ',' }) } else { Write-Output -InputObject ( @($Red, $Green, $Blue) -join ',' ) } } else { Write-Error -Message "$curHex is not a valid Hex color string" break } } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |