-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Use safer colors for ASCII output. Try for a combination of
if curColor fetched:
if outputColors.contains(curColor):
outputColors[curColor] -> change by small amount
else: // could not be found
use color safe output, patterns that already exist in most term. shells.
Whether or not an image is outputted > if that image's color as ascii rendering is accurate
Here's a preliminary look at what ChatGPT has to say about grabbing color (mainly on linux):
Short answer: there’s no fully portable, standard way to ask “what color is my terminal’s background?” across all terminals. Most terminals don’t expose theme colors via POSIX/terminfo.
Longer answer: some xterm-compatible terminals support private escape sequences that let you query colors. If they support it, you can ask for the default background using OSC 11 and read the reply. Others ignore it (security/compat reasons), and multiplexers like tmux/screen may block it unless configured.
Here are practical options:
1) Bash script (OSC 11 query — works on terminals that support it)
#!/usr/bin/env bash
# Prints the terminal's default background color as #RRGGBB, or errors if unsupported.
oldstty=$(stty -g)
stty -echo -icanon time 5 min 0
# Send OSC 11 query: ESC ] 11 ; ? BEL
printf '\033]11;?\a' > /dev/tty
# Read reply, which should end with BEL (\a) or ST (ESC \)
resp=''
IFS= read -r -d $'\a' -t 1 resp < /dev/tty || {
IFS= read -r -d $'\033\\' -t 1 resp < /dev/tty || true
}
stty "$oldstty"
# Typical reply: ESC ] 11 ; rgb:RRRR/GGGG/BBBB BEL
payload=${resp#$'\033]11;'}
if [[ $payload =~ rgb:([0-9A-Fa-f]{1,4})/([0-9A-Fa-f]{1,4})/([0-9A-Fa-f]{1,4}) ]]; then
r=${BASH_REMATCH[1]} g=${BASH_REMATCH[2]} b=${BASH_REMATCH[3]}
# Scale 1–4 hex digits to 8-bit each
scale() { local v=$1 n=${#1}; printf '%d' $(( (16#$v)*255 / ((1<<(4*n))-1) )); }
printf '#%02X%02X%02X\n' "$(scale "$r")" "$(scale "$g")" "$(scale "$b")"
exit 0
fi
echo "No reply or unsupported by this terminal." >&2
exit 1Notes
- If you’re inside tmux, enable passthrough (e.g.,
set -g allow-passthrough onin newer tmux) or run the script outside tmux. - Some terminals reply with
rgb:RR/GG/BB(8-bit) orrgb:RRRR/GGGG/BBBB(16-bit). The script normalizes both.
2) Python example (same idea, a bit more robust)
import os, sys, re, termios, tty
def query_bg():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
os.write(fd, b"\x1b]11;?\x07") # OSC 11 query
buf = b""
# Read until BEL or ST
while True:
ch = os.read(fd, 1)
if not ch:
break
buf += ch
if buf.endswith(b"\x07") or buf.endswith(b"\x1b\\"):
break
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
m = re.search(rb"rgb:([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})", buf)
if not m:
raise RuntimeError("No color reply")
def scale(hexpart: bytes) -> int:
v = int(hexpart, 16)
n = len(hexpart)
return (v * 255) // ((1 << (4*n)) - 1)
r, g, b = (scale(m.group(i)) for i in (1,2,3))
return f"#{r:02X}{g:02X}{b:02X}"
print(query_bg())3) Windows PowerShell (Console host only)
If you’re in the classic Windows console host (or Windows Terminal forwarding this state), PowerShell exposes the current 16-color background:
$Host.UI.RawUI.BackgroundColor # e.g., DarkBlue(That returns a named console color, not a true #RRGGBB value.)
4) “Kinda-portable” fallbacks (not reliable)
-
Some terminals set
COLORFGBG="fg;bg"with palette indexes (e.g.,15;0). You can read it, but it’s nonstandard and often absent/wrong:[[ -n "$COLORFGBG" ]] && echo "$COLORFGBG"
Bottom line
- Portable answer: No — terminfo/tput can’t tell you theme colors.
- Best bet on Unix: Try OSC 11; gracefully handle “no reply.”
- Beware: Terminal/multiplexer support varies; don’t assume it works everywhere.