Created
November 24, 2025 20:27
-
-
Save kommander/fba6b1b865a29b3ad5ce9f29c75f0d4c to your computer and use it in GitHub Desktop.
output terminal palette colors as hex
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
| #!/usr/bin/env bash | |
| # Put terminal into raw mode to capture OSC responses | |
| stty -icanon -echo min 0 time 1 | |
| # Create temp file | |
| tmp=$(mktemp) | |
| # Send OSC 4 queries for all 256 colors | |
| for i in {0..255}; do | |
| printf "\e]4;%d;?\a" "$i" | |
| done | |
| # Give terminal time to respond fully (important!) | |
| sleep 0.2 | |
| # Read everything available into the temp file | |
| dd bs=1 of="$tmp" status=none | |
| # Restore terminal settings | |
| stty sane | |
| # Parse responses | |
| grep -ao $'\e]4;[0-9]\+;rgb:[0-9A-Fa-f/]\+\a' "$tmp" | | |
| while IFS= read -r resp; do | |
| # Extract index and rgb components | |
| if [[ $resp =~ $'\e]4;'([0-9]+)';rgb:'([0-9A-Fa-f]+)/([0-9A-Fa-f]+)/([0-9A-Fa-f]+)$'\a' ]]; then | |
| idx=${BASH_REMATCH[1]} | |
| r=${BASH_REMATCH[2]} | |
| g=${BASH_REMATCH[3]} | |
| b=${BASH_REMATCH[4]} | |
| # Convert 16-bit hex to 8-bit hex | |
| printf "%3d #%02X%02X%02X\n" \ | |
| "$idx" \ | |
| $((16#$r / 257)) \ | |
| $((16#$g / 257)) \ | |
| $((16#$b / 257)) | |
| fi | |
| done | | |
| sort -n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment