How to enable rgb colored output in terminal?
I'm trying to print a colored string to a gnome-terminal using ANSI escape codes, but, although it works for the custom colors, or even the 256 extra color, it doesn't work with RGB codes.
So, simplifying:
cout << "\033[33m" << '.' << "\033[0m"; # prints with color
cout << "\033[38;5;135m" << '.' << "\033[0m"; # prints with color
cout << "\033[38;2;0;135;0m" << '.' << "\033[0m"; # doesn't work, prints with default color
How can I output something with an RGB color code in the gnome-terminal?
I'm following this link for outputs: Wikipedia ANSI escape code.
I added:
$ export TERM=xterm-256color
To my .bashrc file to support 256 colors and:
$ tput colors
Outputs 256, if that's important.
RGB colors can not be used in the terminal for these reasons:
- Bash does not choose the commandline colors.
- Bash can only specify ANSI colors.
The two above reasons are very closely linked. Most of these are dependant on your screen and ANSI color specification. If you use a good terminal emulator, you might be able to set custom RGB colors for certain ANSI color codes.
Bash as a shell is powerless as to what the screen can do with displays. It only passes the words on the terminal into your screen. The screen can decide what to do with it. The screen can decide what colors the ANSI escape codes represent (if the terminal even allows colors).
A workaround is to change the terminal color specs. However, very few (I don't know any) will allow you to change colors.
You need vte >= 0.36 for true colors to work in gnome-terminal. Make sure your vte is recent enough, and in that case your code should work.
(Note: when you output hard-coded strings, as you do in your example application, the value of $TERM is necessarily absolutely irrelevant. It only matters if you're using libraries whose behavior depends on this, such as ncurses.)