Skip to content

Instantly share code, notes, and snippets.

@lzybkr
Created September 22, 2016 21:29
Show Gist options
  • Save lzybkr/05bf1fe79852ee5f6facaf62999e53a7 to your computer and use it in GitHub Desktop.
Save lzybkr/05bf1fe79852ee5f6facaf62999e53a7 to your computer and use it in GitHub Desktop.
[CmdletBinding(DefaultParameterSetName = 'FixedG')]
param(
[Parameter(ParameterSetName = 'FixedR', Mandatory)]
[ValidateRange(0, 255)]
$fixedR,
[Parameter(ParameterSetName = 'FixedG')]
[ValidateRange(0, 255)]
[int]$fixedG = 105,
[Parameter(ParameterSetName = 'FixedB', Mandatory)]
[ValidateRange(0, 255)]
[int]$fixedB,
[char]$Character = [char]" ")
$h = [Console]::WindowHeight - 4 # Use most of the vertical
$w = [Console]::BufferWidth - 2 # and even more of the horizontal
$e = [string][char]0x1b
$sb = [System.Text.StringBuilder]::new()
$null = $sb.AppendLine("$e[mPrinting 24bit gradient with ANSI sequences using powershell")
# Run cycles. Use {ESC [ 48 ; 2 ; R ; G ; B m} to set background
# RGB color of the next printing character (space in this example)
for ($l = 0; $l -lt $h; $l++)
{
$c1 = [int]($l*255/$h)
$null = $sb.Append("$e[m ")
for ($c = 0; $c -lt $w; $c++)
{
$c2 = [int]($c*255/$w)
switch ($PSCmdlet.ParameterSetName)
{
'FixedR' { $null = $sb.Append("$e[48;2;$fixedR;$c1;${c2}m$Character"); break; }
'FixedG' { $null = $sb.Append("$e[48;2;$c1;$fixedG;${c2}m$Character"); break; }
'FixedB' { $null = $sb.Append("$e[48;2;$c2;$c1;${fixedB}m$Character"); break; }
}
}
$null = $sb.Append("$e[m ")
}
$null = $sb.Append( "Gradient done")
$sb.ToString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment