Last active
September 27, 2025 12:41
-
-
Save realslacker/d4808bbf8ccc2203c426602563dae133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Set the color of the current terminal tab and background. | |
| .DESCRIPTION | |
| This script sets the color of the current terminal tab and background based on various input parameters. | |
| It supports random colors, hexadecimal color codes, console colors, known colors, and RGBA values | |
| for more precise color control. | |
| .PARAMETER Random | |
| If specified, a random color will be generated. | |
| .PARAMETER Hex | |
| A hexadecimal color code (6 or 8 characters) to set the color. | |
| .PARAMETER ConsoleColor | |
| A System.ConsoleColor value to set the color. | |
| .PARAMETER KnownColor | |
| A System.Drawing.KnownColor value to set the color. | |
| .PARAMETER Red | |
| The red component of the color (0-255). | |
| .PARAMETER Green | |
| The green component of the color (0-255). | |
| .PARAMETER Blue | |
| The blue component of the color (0-255). | |
| .PARAMETER Alpha | |
| The alpha (transparency) component of the color (0-255). | |
| .PARAMETER TabScale | |
| A percentage (1-100) to scale the tab color brightness. Default is 100. | |
| .PARAMETER BackgroundScale | |
| A percentage (1-100) to scale the background color brightness. Default is 30. | |
| .PARAMETER TabOnly | |
| If specified, only the tab color will be set, not the terminal background. | |
| .PARAMETER BlacklistCurrent | |
| If specified, the current tab color will be added to a blacklist file to avoid reuse in future random selections. | |
| .EXAMPLE | |
| Set-TabColor -Random | |
| Sets a random color for the terminal tab and background. | |
| .EXAMPLE | |
| Set-TabColor -Hex 'FF5733' | |
| Sets the terminal tab and background color to the specified hexadecimal color code. | |
| .EXAMPLE | |
| Set-TabColor -ConsoleColor 'DarkCyan' | |
| Sets the terminal tab and background color to the specified console color. | |
| .EXAMPLE | |
| Set-TabColor -KnownColor 'Coral' | |
| Sets the terminal tab and background color to the specified known color. | |
| #> | |
| [CmdletBinding( DefaultParameterSetName = 'Random' )] | |
| param( | |
| [Parameter( ParameterSetName = 'Random' )] | |
| [switch] | |
| $Random, | |
| [Parameter( ParameterSetName = 'Hex', Mandatory )] | |
| [ValidateScript({ | |
| if ( $_ -match '[^a-f\d]' ) { | |
| throw 'Hex must be a valid hexadecimal string.' | |
| } | |
| if ( $_.Length -ne 6 -and $_.Length -ne 8 ) { | |
| throw 'Hex must be exactly 6 or 8 characters long.' | |
| } | |
| return $true | |
| })] | |
| [string] | |
| $Hex, | |
| [Parameter( ParameterSetName = 'ConsoleColor', Mandatory )] | |
| [System.ConsoleColor] | |
| $ConsoleColor, | |
| [Parameter( ParameterSetName = 'KnownColor', Mandatory )] | |
| [System.Drawing.KnownColor] | |
| $KnownColor, | |
| [Parameter( ParameterSetName = 'RGBA', Mandatory )] | |
| [ValidateRange( 0, 255 )] | |
| [System.Nullable[UInt32]] | |
| $Red, | |
| [Parameter( ParameterSetName = 'RGBA', Mandatory )] | |
| [ValidateRange( 0, 255 )] | |
| [System.Nullable[UInt32]] | |
| $Green, | |
| [Parameter( ParameterSetName = 'RGBA', Mandatory )] | |
| [ValidateRange( 0, 255 )] | |
| [System.Nullable[UInt32]] | |
| $Blue, | |
| [ValidateRange( 1, 100 )] | |
| [UInt32] | |
| $TabScale = 100, | |
| [UInt32] | |
| $BackgroundScale = 30, | |
| [switch] | |
| $TabOnly, | |
| [Parameter( ParameterSetName = 'BlacklistCurrent' )] | |
| [switch] | |
| $BlacklistCurrent | |
| ) | |
| #Requires -PSEdition Core | |
| $BlackListPath = [IO.Path]::Combine( $env:USERPROFILE, '.config', 'PowerShell', 'TabColorBlacklist.txt' ) | |
| if ( $BlacklistCurrent ) { | |
| if ( -not $env:__TabColor ) { | |
| throw 'No current tab color found in environment variable __TabColor.' | |
| } | |
| # append to the blacklist file | |
| $BlackListFolder = [IO.Path]::GetDirectoryName( $BlackListPath ) | |
| New-Item -Path $BlackListFolder -ItemType Directory -Force > $null | |
| [string[]] $Blacklist = Get-Content -Path $BlackListPath -ErrorAction SilentlyContinue | |
| if ( $env:__TabColor -notin $Blacklist ) { | |
| Write-Verbose ( 'Adding {0} to color blacklist at {1}' -f $env:__TabColor, $BlackListPath ) | |
| Add-Content -Path $BlackListPath -Value $env:__TabColor -Force | |
| } else { | |
| Write-Verbose ( '{0} is already in the color blacklist at {1}' -f $env:__TabColor, $BlackListPath ) | |
| } | |
| } | |
| [System.Drawing.Color] $Color = switch ( $PSCmdlet.ParameterSetName ) { | |
| 'ConsoleColor' { | |
| Write-Verbose ( 'Using ConsoleColor: {0}' -f $ConsoleColor ) | |
| $ColorKeyName = 'ColorTable{0:00}' -f [int] $ConsoleColor | |
| $ColorValue = Get-ItemProperty -Path HKCU:\Console\ -Name $ColorKeyName -ErrorAction Stop | Select-Object -ExpandProperty $ColorKeyName | |
| $R, $G, $B, $null = [BitConverter]::GetBytes($ColorValue) | |
| [System.Drawing.Color]::FromArgb( $R, $G, $B ) | |
| } | |
| 'KnownColor' { | |
| Write-Verbose ( 'Using KnownColor: {0}' -f $KnownColor ) | |
| [System.Drawing.Color]::FromKnownColor( $KnownColor ) | |
| } | |
| 'Hex' { | |
| Write-Verbose ( 'Using Hex: {0}' -f $Hex ) | |
| $R = [Convert]::ToInt32( $Hex.Substring( 0, 2 ), 16 ) | |
| $G = [Convert]::ToInt32( $Hex.Substring( 2, 2 ), 16 ) | |
| $B = [Convert]::ToInt32( $Hex.Substring( 4, 2 ), 16 ) | |
| if ( $Hex.Length -eq 8 ) { | |
| $A = [Convert]::ToInt32( $Hex.Substring( 6, 2 ), 16 ) | |
| } else { | |
| $A = 255 | |
| } | |
| [System.Drawing.Color]::FromArgb( $A, $R, $G, $B ) | |
| } | |
| 'RGBA' { | |
| Write-Verbose ( 'Using RGBA: {0}, {1}, {2}, {3}' -f $Red, $Green, $Blue, $Alpha ) | |
| [System.Drawing.Color]::FromArgb( $Alpha, $Red, $Green, $Blue ) | |
| } | |
| default { | |
| # enumerate the color blacklist | |
| [string[]] $Blacklist = ( Test-Path -Path '~\.config\PowerShell\TabColorBlacklist.txt' -PathType Leaf ) ? | |
| ( Get-Content -Path '~\.config\PowerShell\TabColorBlacklist.txt' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) -and -not $_[0] -eq '#' } ) : | |
| @() | |
| Write-Verbose 'Selecting a random KnownColor...' | |
| [System.Drawing.KnownColor].GetEnumNames() | | |
| ForEach-Object { [System.Drawing.Color]::FromKnownColor($_) } | | |
| Where-Object { | |
| $_.Name -notin $Blacklist -and | |
| $_.IsSystemColor -eq $false -and | |
| ( | |
| $_.R -ne $_.G -or | |
| $_.G -ne $_.B -or | |
| $_.B -ne $_.R | |
| ) | |
| } | Get-Random | |
| } | |
| } | |
| $Red = $Color.R | |
| $Green = $Color.G | |
| $Blue = $Color.B | |
| $Alpha = $Color.A | |
| # store the current color for blacklisting if requested | |
| $ColorHex = '{0:x2}{1:x2}{2:x2}{3:x2}' -f [int] $Red, [int] $Green, [int] $Blue, [int] $Alpha | |
| $env:__TabColor = $ColorHex | |
| Write-Verbose ( 'Starting Color: {0}' -f $ColorHex ) | |
| $TabRed = $Red * $TabScale / 100 | |
| $TabGreen = $Green * $TabScale / 100 | |
| $TabBlue = $Blue * $TabScale / 100 | |
| Write-Verbose ( 'Tab Color: {0:x2}{1:x2}{2:x2}{3:x2}' -f [int] $TabRed, [int] $TabGreen, [int] $TabBlue, [int] $Alpha ) | |
| $TabColor = 'rgb:{0:x2}/{1:x2}/{2:x2}' -f [int] $TabRed, [int] $TabGreen, [int] $TabBlue | |
| Write-Host "`e]4;264;$TabColor`a" | |
| # If only setting the tab color, exit here | |
| if ( $TabOnly ) { return } | |
| $BackgroundRed = $TabRed * $BackgroundScale / 100 | |
| $BackgroundGreen = $TabGreen * $BackgroundScale / 100 | |
| $BackgroundBlue = $TabBlue * $BackgroundScale / 100 | |
| Write-Verbose ( 'Background Color: {0:x2}{1:x2}{2:x2}' -f [int] $BackgroundRed, [int] $BackgroundGreen, [int] $BackgroundBlue ) | |
| $BackgroundColor = 'rgb:{0:x2}/{1:x2}/{2:x2}' -f [int] ($BackgroundRed), [int] ($BackgroundGreen), [int] ($BackgroundBlue) | |
| Write-Host "`e]4;262;$BackgroundColor`a" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment