1

If I use mode 0,we can set the font color and background color,shuc as

echo -e "\e[0;31;47m teststring \e[0m"

But I hope to use a opposite color to be a background color(I think that is cyan).So I want to use the mode 7(mode 7 will get a opposite background).But it don't work always:

Any body can tell me how to use this mode?

yode
  • 1,047

3 Answers3

3

\e[7m is the code for reverse video (also often used for the standout mode) in most terminals. What that does though is replace the background colour with the foreground colour and the foreground colour with the background colour, it's not reverse-video as in a photographic negative.

For the photographic negative, a few terminals support the:

\e[38;2;RED;GREEN;BLUEm  # for foreground
\e[48;2;RED;GREEN;BLUEm  # for background

escape sequences, where RED, GREEN, BLUE is a decimal number from 0 to 255. That tells the terminal to pick a colour in their palette that is closest to that RGB specification.

So you could do:

straight_color() {
   printf '\33[48;2;%s;%s;%sm\33[38;2;%s;%s;%sm' "$@"
}
negative_color() {
  for c do
    set -- "$@" "$((255 - c))"
    shift
  done
  straightcolor "$@"
}

And then for instance:

straight_color 255 0 0  255 255 255

For bright white foreground on bright red background and:

negative_color 255 0 0  255 255 255

for the negative (black on bright cyan).

2

Read carefully wikipage on ANSI escape codes; you could use \e[46m etc

1

The best way to handle this is to use the terminfo library. It's far, far easier than remembering escape codes, and usually less prone to error. (It's also terminal agnostic, but I suspect it's unlikely you'll be using a non-ANSI terminal device these days.)

# Use reversed colours
tput smso
echo hello, world
tput rmso

You can even put the codes into variables programmatically, like this

smso=$(tput smso)
rmso=$(tput rmso)

echo "${smso}Hello again${rmso}"

There are colour controls, too, using tput setf {colour} and tput setb {colour}. (Search man terminfo for "Color Handling".) These are more fiddly so I tend to use a little script to handle them

colour blue yellow
echo this is blue on yellow

Here's the script

#!/bin/sh
#
# Take a pair of colours and set the foreground and background,
# respectively.
#
########################################################################
#
NULL=/dev/null

fg="$1"
bg="$2"


########################################################################
# Translate a colour name to the corresponding ANSI index value
#
colourNo ()
{
    case "$1" in
        black|0)        echo 0  ;;
        blue|1)         echo 1  ;;
        green|2)        echo 2  ;;
        cyan|3)         echo 3  ;;
        red|4)          echo 4  ;;
        magenta|5)      echo 5  ;;
        yellow|6)       echo 6  ;;
        white|7)        echo 7  ;;
    esac
    return
}


########################################################################
# Go
#
if test "X$1" = 'X-?'
then
    progname=`basename "$0"`
    echo "Usage:  $progname  [<fg_colour>|-  [bg_colour]]" >&2
    exit 1
fi

if test -n "$fg" -a "X$fg" != "X-"
then
    colour=`colourNo "$fg"`
    test -n "$colour" && tput setf "$colour"
fi

if test -n "$bg" -a "X$bg" != "X-"
then
    colour=`colourNo "$bg"`
    test -n "$colour" && tput setb "$colour"
fi

exit 0
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • If you want only reverse video, it's better to use tput rev. standout (smso) is usually implemented with reverse video, but not necessarily, or it could be bold+reverse for instance. – Stéphane Chazelas Jul 03 '17 at 10:34
  • @StéphaneChazelas thank you. Is the cancel operation for rev still rmso? – Chris Davies Jul 03 '17 at 17:18
  • well, there's no dedicated capability for that, so you'd need to use sgr0 which would revert all attributes (including color) to their default. rmso will work on terminals where rev and smso are the same but not necessarily on others (and like smso could change other attributes) – Stéphane Chazelas Jul 04 '17 at 06:11
  • @StéphaneChazelas Ah yes of course. I'd completely forgotten about sgr0 thank you very much. – Chris Davies Jul 04 '17 at 06:43