Private/UI/Get-PSMBGradient.ps1
|
function Get-PSMBGradientString { <# .SYNOPSIS Renders a string with per-character gradient coloring. .DESCRIPTION Applies ANSI 24-bit color codes to each character, linearly interpolating between the start and end RGB values. Uses StringBuilder for performance. #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)] [AllowEmptyString()] [string]$Text, [Parameter(Mandatory)] [int[]]$StartRGB, [Parameter(Mandatory)] [int[]]$EndRGB, [Parameter()] [string]$Prefix = '' ) if ($Text.Length -eq 0) { return '' } $esc = [char]27 $reset = "${esc}[0m" $sb = [System.Text.StringBuilder]::new($Text.Length * 20) $len = $Text.Length if ($len -eq 1) { $null = $sb.Append("${Prefix}${esc}[38;2;$($StartRGB[0]);$($StartRGB[1]);$($StartRGB[2])m$($Text)${reset}") return $sb.ToString() } for ($i = 0; $i -lt $len; $i++) { $ratio = $i / ($len - 1) $r = [int]($StartRGB[0] + ($EndRGB[0] - $StartRGB[0]) * $ratio) $g = [int]($StartRGB[1] + ($EndRGB[1] - $StartRGB[1]) * $ratio) $b = [int]($StartRGB[2] + ($EndRGB[2] - $StartRGB[2]) * $ratio) $null = $sb.Append("${Prefix}${esc}[38;2;${r};${g};${b}m$($Text[$i])") } $null = $sb.Append($reset) return $sb.ToString() } function Get-PSMBGradientLine { <# .SYNOPSIS Renders a line of repeated characters with gradient coloring. #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)] [string]$Character, [Parameter(Mandatory)] [int]$Length, [Parameter(Mandatory)] [int[]]$StartRGB, [Parameter(Mandatory)] [int[]]$EndRGB ) $line = $Character * $Length return Get-PSMBGradientString -Text $line -StartRGB $StartRGB -EndRGB $EndRGB } |