My question is in general shell scripting with ansi colors but for reference I am using an Apple Mac OS X 10.9 Mavericks. I use "iTerm" terminal app as my default terminal but also checked with the built in "terminal" app as well. I use ZSH (5.0.7) as my default shell but also checked in BASH (3.2.51).
I have been trying to find out if there is a list of the RGB values for the 256 color indexed extended fore/background Ansi escape codes that are available using esc[38;5;xm and esc[48;5;xm where x is a number from 0 to 255. I have found some scripts that print out the colors as blocks (using the index) but I want to know the rgb values of each of the colors in the indexes.
Here is an example of the ansi codes in use:
printf '\e[38;5;222;48;5;238m Hi \e[m\n'
(\e can be replaced by \033 or \x1b)
So basically I am wondering if there is a list or agreed upon settings for these indexed colors? For example 232-255 seem to always be a gray gradient. Every site that I have found that references the extended colors pretty much just says how to use it and doesn't list any specific colors.
I found some references to X11 and an rgb.txt file which at first seemed like it was what I was looking for but they don't seem to match up to the index numbers. There are 752 colors in the file I found (most are duplicates so say 376 which is still more the 256). Also there are 50 shades of gray (100 if you count duplicates) but the Ansi indexed colors have 23 so it doesn't seem to be the same. If these in some way do contain the colors in the ansi extended color index does anyone have a list of which names are in which index?
PS. I know esc[38;2;r;g:bm can supposedly set a color using rgb values but I can't get it to work on my Mac and I am more interested in the default values for the indexed colors.
Some sites: (can only post 2 due to low rep? But I checked a lot of sites)
This one has the rgb for the standard colors but not the extended ones.
The 256 color table and its partitioning
The color range of a 256 color terminal consists of 4 parts, often 5, in which case you actually get 258 colors:
Color numbers 0 to 7 are the default terminal colors, the actual RGB value of which is not standardized and can often be configured.
Color numbers 8 to 15 are the "bright" colors. Most of the time these are a lighter shade of the color with index - 8. They are also not standardized and can often be configured. Depending on terminal and shell, they are often used instead of or in conjunction with bold font faces.
Color numbers 16 to 231 are RGB colors. These 216 colors are defined by 6 values on each of the three RGB axes. That is, instead of values 0 - 255, each color only ranges from 0 - 5.
The color number is then calculated like this:
with
r
,g
andb
in the range 0 - 5.The color numbers 232 to 255 are grayscale with 24 shades of gray from dark to light.
The default colors for foreground and background. In many terminals they can be configured independently from the 256 indexed colors, giving an additional two configurable colors . You get them when not setting any other color or disabling other colors (i.e.
print '\e[m'
).Some sources:
urxvt
manpage:xterm
manpage:Wikipedia article on ANSI escape codes (which in turn itself is lacking a citation on the topic)
Default RGB values
Theoretically, in order to get an equally distributed range of colors, the RGB values for the colors in the range 16 - 231 could be calculated like this:
But it seems that the actual method is different:
Any terminal emulators I tested seems to follow XTerm and map the values
[0, 1, 2, 3, 4, 5]
for red, green and blue to the values[0, 95, 135, 175, 215, 255]
on the RGB color axes. (I tested with XTerm (297) URxvt (v9.19), ROXTerm (2.8.1), gnome-terminal (3.6.2) and xfce4-terminal (0.6.3))The RGB values for a given index can be calculated with this algorithm:
The grayscale seems to follow this simple formula:
256colres.pl
in the root of the XTerm sources (version 313) uses a similar algorithm to generate256colres.h
, which contains the color definitions for 256 color mode:Showing available colors in a terminal
Here is a zsh function that prints all colors on a 256 color terminal (if
TERM
is set to a 256 color value):Changing RGB values during runtime
In some terminals (at least
xterm
,gnome-terminal
,termite
andurxvt
) all those colors can be changed during runtime by sending one of the following XTerm Control Sequences:where:
OSC
is the escape character (\e
or\033
) followed by]
c
is the color number (0 - 255)spec
is a color specification (e.g.red
,#ff0000
,rgb:ff/00/00
,rgbi:1/0/0
- what actually works might depend on the terminal)BEL
is the bell character (\a
or\007
)ST
is the string terminator\e\\
or\033\\
These control sequences can be sent by simply printing them with
echo
:For example, in order to set color number 5 (usually some shade of magenta) to red, either of these should work:
Those colors can be reset to their (configured) default with one of the control sequences
So the following loop will reset all colors from 0 to 255 to their configured or default value:
For the default foreground and background colors the control sequences are
OSC 10 ; spec BEL
andOSC 11 ; spec BEL
, respectively. For example:Those can be reset with
OSC 110 BEL
andOSC 111 BEL
respectively: