private/Set-WtwTerminalColor.ps1

<#
.SYNOPSIS
    Sets terminal tab or window color and optional title using escape sequences.

.DESCRIPTION
    Writes OSC sequences for supported terminals (iTerm2, Windows Terminal, Kitty,
    Konsole, WezTerm, tmux). Others may only get title updates. Side effect: writes
    escape codes to the host output stream (no file I/O).

.PARAMETER Color
    Optional six-digit hex color, with or without leading '#'.

.PARAMETER Title
    Optional string for window/tab title (OSC 0).

.EXAMPLE
    Set-WtwTerminalColor -Color '#2ba7d0' -Title 'my-branch'

.NOTES
    Depends on: tmux, iTerm2, WT_SESSION, KITTY_PID, KONSOLE_VERSION, WEZTERM_PANE where applicable.
#>

function Set-WtwTerminalColor {
    [CmdletBinding()]
    param(
        [Parameter(Position = 0)]
        [string] $Color,

        [Parameter(Position = 1)]
        [string] $Title
    )

    $esc = [char]27
    $bel = [char]7
    $inTmux = $null -ne $env:TMUX

    # Set tab/window title - OSC 0 works nearly everywhere
    if ($Title) {
        if ($inTmux) {
            # tmux: passthrough + set pane title
            Write-Host "${esc}]0;${Title}${bel}" -NoNewline
            Write-Host "${esc}k${Title}${esc}\" -NoNewline
        } else {
            Write-Host "${esc}]0;${Title}${bel}" -NoNewline
        }
    }

    # Set tab color from hex
    if ($Color -and $Color -match '^#?([0-9a-fA-F]{6})$') {
        $hex = $Matches[1]
        $r = [convert]::ToInt32($hex.Substring(0, 2), 16)
        $g = [convert]::ToInt32($hex.Substring(2, 2), 16)
        $b = [convert]::ToInt32($hex.Substring(4, 2), 16)

        $termProgram = $env:TERM_PROGRAM

        if ($inTmux) {
            # tmux: set pane border and status bar color via tmux commands
            $hexColor = "#${hex}"
            try {
                & tmux select-pane -P "bg=default" 2>$null
                & tmux set-option -p pane-active-border-style "fg=${hexColor}" 2>$null
                & tmux set-option -p pane-border-style "fg=${hexColor}" 2>$null
            } catch { Write-Verbose "tmux color: $_" }
        } elseif ($termProgram -eq 'iTerm.app') {
            # iTerm2 proprietary escape for tab color
            Write-Host "${esc}]6;1;bg;red;brightness;${r}${bel}" -NoNewline
            Write-Host "${esc}]6;1;bg;green;brightness;${g}${bel}" -NoNewline
            Write-Host "${esc}]6;1;bg;blue;brightness;${b}${bel}" -NoNewline
        } elseif ($env:WT_SESSION) {
            # Windows Terminal - OSC 9;9
            Write-Host "${esc}]9;9;rgb:$($hex.Substring(0,2))/$($hex.Substring(2,2))/$($hex.Substring(4,2))${esc}\" -NoNewline
        } elseif ($env:KITTY_PID -or $termProgram -eq 'kitty') {
            # Kitty - OSC 30 sets the tab/window title bar color
            Write-Host "${esc}]30;#${hex}${bel}" -NoNewline
        } elseif ($env:KONSOLE_VERSION) {
            # Konsole - same OSC 30 as Kitty
            Write-Host "${esc}]30;#${hex}${bel}" -NoNewline
        } elseif ($env:WEZTERM_PANE) {
            # WezTerm - set tab color via user var
            Write-Host "${esc}]1337;SetUserVar=wtw_color=$(
                [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("#${hex}"))
            )${bel}"
 -NoNewline
        }
        # Unsupported terminals (Terminal.app, GNOME Terminal, Alacritty, foot, etc.)
        # fall through - title is already set above for orientation
    }
}