Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine a terminal's background color?

I'd like to know if there is any way to determine a terminal's background color ?

In my case, using gnome-terminal.
It might matter, since it's entirety up to the terminal application to draw the background of its windows, which may even be something else than a plain color.

like image 215
Romuald Brunet Avatar asked Mar 24 '10 11:03

Romuald Brunet


People also ask

How do I change the background color in Unix?

“tput setaf” sets foreground color, “tput setab” sets background color, and “tput sgr0” resets all the settings to terminal default. There are 8 standard colors encoded in numbers from 0 to 7 (in order: black, red, green, yellow, blue, magenta, cyan, white).

How do you select a background Colour from the colors group?

Answer. Press "Windows," type "Paint" and click "Paint" to launch the Paintprogram. ... Click the image's background colorand note that Paint changes the color of the "Color 1" square to match that color. ... Move to the Colors section and click the color you'd like to use to replace the existing background color.


2 Answers

There's an xterm control sequence for this:

\e]11;?\a 

(\e and \a are the ESC and BEL characters, respectively.)

Xterm-compatible terminals should reply with the same sequence, with the question mark replaced by an X11 color name, e.g. rgb:0000/0000/0000 for black.

like image 186
ak2 Avatar answered Sep 20 '22 01:09

ak2


I've came up with the following:

#!/bin/sh # # Query a property from the terminal, e.g. background color. # # XTerm Operating System Commands #     "ESC ] Ps;Pt ST"  oldstty=$(stty -g)  # What to query? # 11: text background Ps=${1:-11}  stty raw -echo min 0 time 0 # stty raw -echo min 0 time 1 printf "\033]$Ps;?\033\\" # xterm needs the sleep (or "time 1", but that is 1/10th second). sleep 0.00000001 read -r answer # echo $answer | cat -A result=${answer#*;} stty $oldstty # Remove escape at the end. echo $result | sed 's/[^rgb:0-9a-f/]\+$//' 

Source/Repo/Gist: https://gist.github.com/blueyed/c8470c2aad3381c33ea3

like image 29
blueyed Avatar answered Sep 21 '22 01:09

blueyed