1

In the thread below

Programmatic access to current xterm background color?

Alex North-Keys provides a useful bash script that returns the background color of the current xterm. I'd like to use the output of this script to reset the background color after it has been changed (e.g., after logging into a remote system).

For example, his script might return something like

rgb:e0e0/ffff/ffff

Unfortunately the escape sequence I use to set the background color

echo -ne "\033]11;!*\007"

seems to work only if I feed it a named color, like ivory.

Is there a way to modify this command so that it will take as an argument something like e0e0/ffff/ffff?

Thanks!

Leo Simon
  • 111
  • 4
  • The "`!*`" looks odd, since none of the relevant controls have that pair. – Thomas Dickey Jun 07 '15 at 20:10
  • Yes that seems strange. And the color numbers used are not octal (as `\007` insinuates. Have a look at these pages: https://wiki.archlinux.org/index.php/Color_Bash_Prompt and http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html for valid color names. – jcoppens Jun 08 '15 at 04:01
  • sorry, I simply pasted a copy of my alias command. Here's an example of how I use it: echo -ne "\033]11;ivory\007" which changes the background color to ivory. I'd like to be able replace names like ivory with color codes of the form e0e0/ffff/ffff – Leo Simon Jun 08 '15 at 08:06
  • 1
    As I read his script, it seems that all it does is extract the response from xterm. So the prefix `rgb:` is needed if your script is going to provide an RGB value rather than a name. xterm uses `XParseColor`, whose documentation is vague regarding "names". – Thomas Dickey Jun 08 '15 at 08:56
  • The "standard" #rrggbb notation (without the `rgb:` prefix) also works, e.g. "\003]11;#e0ffff\007". – egmont Jun 23 '15 at 18:12

1 Answers1

0

Copy & paste into Emacs or Vim. Delete #????? beginning one of the lines. In the line is a sed statement which contains "^[". This is text of 2 characters which needs to be replaced with one character ESC (^[). In Vim, enter C+v ESC. In Emacs, enter C+q ESC.

# filename: OSC_11_Pt_ST.sh
echo -e "\e[7m Reverse the FG/BG colors on this terminal as a visual aid. \e[0m"
y='\' # Add to end of $result (\e]11;rgb:??/??/??\e) to complete ST terminating
# OSC sequence.
# Query the background color using ST (string terminate) vice BEL (^G).Replies
# with a control sequence in RGB specification format using same terminator as
# the query (in this case, ST): "^[]11;rgb:4242/2525/5353^[\" (see file
# ctlseqs.txt). "^[" is ESC (\e); "^[]" is OSC; "^[\" is ST.
echo -en "\e]11;?\e\\"
# Read line from the standard input & assign to variable CSPEC (color specifi-
# cation as indicated by ctlseqs.txt). Do not echo input coming from terminal
# (-s); raw (-r); delimiter \ (-d).
read -s -r -d '\' CSPEC
#?????result=$(echo $CSPEC | sed 's/^[/\\\e/g')
# Set BG to "WebGrey" (decimal 128 128 128).
echo 'Set BG to "Webgrey" in 5 seconds...'
sleep 5
echo -en "\e]11;rgb:80/80/80\007"
echo "Return to original BG ($result$y) in 5 seconds..."
sleep 5
echo -en "$result$y"