private/Convert/Convert-HexToColor.ps1
function Convert-HexToColor { <# .SYNOPSIS Converts a hex color or ARGB string into a System.Windows.Media.Color object. .DESCRIPTION The Convert-HexToColor function accepts a color in common formats such as "#RRGGBB", "#AARRGGBB", named color strings (e.g., "Blue", "LightSlateGray"), or a comma-separated ARGB value ("255,100,150,200"). It returns a fully-typed .NET Color object that can be used with WPF or PowerShell GUI elements. .PARAMETER Hex A color value in hex format, named color string, or ARGB notation (e.g., "#FFCC00", "OrangeRed", or "255,0,128,255"). .EXAMPLE Convert-HexToColor -Hex "#0078D7" Returns a Color object corresponding to the specified hex value. .EXAMPLE Convert-HexToColor -Hex "255,0,128,255" Parses ARGB components and returns the corresponding Color object. .NOTES Author: Jeff Pollock GitHub: https://github.com/phriendx/ISEColorTheme.cmdlets Website: https://pxlabs.info #> param ( [Parameter(Mandatory)] [string]$Hex ) # If it's already a Color string like "#RRGGBB" or named color, try parsing directly if ($Hex -match '^#?[A-Fa-f0-9]{6,8}$' -or $Hex -match '^[a-zA-Z]+$') { try { return [System.Windows.Media.ColorConverter]::ConvertFromString($Hex) } catch { throw "Failed to convert color string '$Hex' using ColorConverter." } } # If it looks like "255,255,255,255" if ($Hex -match '^\d{1,3},\d{1,3},\d{1,3},\d{1,3}$') { $rgba = $Hex -split ',' | ForEach-Object { [byte]$_ } return [System.Windows.Media.Color]::FromArgb($rgba[0], $rgba[1], $rgba[2], $rgba[3]) } throw "Unrecognized color format: '$Hex'" } |