0

What is the ANSI color code for a purple (or dark purple) background with black bold text (foreground).

As an example, here is cyan with black bold text: echo -e "\e[30;1;46mExample\e[0m"

Cyan background with bold black text

After consulting a number of ANSI color code lists and examples, I can't figure out how to do the same with a purple background. The closest I can come up with is a magenta background with black, bold text, by using 30;1;45m:

Magenta background with bold black text

  • Are you using an 8- or 24-bit colour terminal? If so, which? Magenta is the only purply one on conventional terminals (where there are only eight colours in total). – Michael Homer May 6 at 1:06
  • My terminal supports up to 256 colors. – Andy Forceno May 6 at 1:37
1

Your escape sequence \e[30;1;45m is perhaps easier understood as the merger of three sequences:

  1. \e[30m -- this means "black foreground"
  2. \e[1m -- this means "bright foreground"
  3. \e[45m -- this means "magenta background"

Now these colour sequences are limited to 8 colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

These are the traditional ANSI colours.

However there are sequences that allow 256 colours to be chosen, if your terminal supports them.

\e[38;5;###m where ### is a number between 0 and 255 will select a foreground colour.

\e[48;5;###m similarly choses the background colour.

So maybe \e[38;5;0;1;48;5;92m might be close to what you want.

Note: if you use 256 colour background settings then you also need to use the 256 colour foreground setting.

FWIW, the following python script will show all the background colours which can help you pick the colour you want.

import sys
for i in range(0, 16):
    for j in range(0, 16):
        code = str(i * 16 + j)
        sys.stdout.write(u"\u001b[48;5;" + code + "m " + code.ljust(4))
    print u"\u001b[0m"

(taken from http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#background-colors )

  • Using 256 colors, I can get the purple background I want, but the foreground is white: echo -e "\e[48;5;55mExample\e[0m" – Andy Forceno May 6 at 1:35
  • You need to use the 256 foreground colour setting as well, as per my example. \e[38;5;0;1;48;5;55mExample\e[0m. I've updated the answer to make that clearer – Stephen Harris May 6 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.