There are eight colors, each of which is identified by a single digit:
0
black
1
red
2
green
3
yellow
4
blue
5
purple
6
cyan
7
white
Each of these colors has a high intensity variant, which brings the number of basic colors to 16 (and correspond to the values of the .NET enum System.ConsoleColor).
In order to create a color code, the color-digit needs to be prepended with one or two other digits (the dot represents the digit for the color):
3.
foreground / normal intensity
4.
background / normal intensity
9.
foreground / high intensity
10.
background / high intensity
Thus, for example a high intensity (=9) red (=1) text is escaped like so:
\e[91m text \e[0m
Or, … in a shell with echo:
$ echo -e "\e[91mred text\e[0m"
$ echo -e "\e[103m\e[91mred text on yellow background\e[0m"
Defining the RGB values for the 16 base colors
At least in conhost.exe, it's possible to define the RGB values for the 16 colors with \e]4;c;rgb:RR/GG/BB\x7. RR, GG and BB are the hex values for the RGB colors. c is the color number which is being defined. Note the closing bracket ].
The following PowerShell script modifies the color palette according to solarized light:
function define-color {
param (
[int] $colorNumber,
[int] $r,
[int] $g,
[int] $b
)
write-host ("$([char]27)]4;{0};rgb:{1:X2}/{2:X2}/{3:X2}$([char]7)" -f $colorNumber, $r, $g, $b)
}
define-color 0 0 39 49 // dark black
define-color 1 208 27 36 // dark red
define-color 2 114 137 5 // dark green
define-color 3 165 119 5 // dark yellow
define-color 4 32 117 199 // dark blue
define-color 5 198 27 110 // dark magenta
define-color 6 37 145 133 // dark cyan
define-color 7 233 226 203 // dark white
define-color 8 0 77 100 // bright black
define-color 9 189 54 18 // bright red
define-color 10 70 90 97 // bright green
define-color 11 82 103 111 // bright yellow
define-color 12 112 129 131 // bright blue
define-color 13 88 86 185 // bright magenta
define-color 14 129 144 143 // bright cyan
define-color 15 252 244 220 // bright white
Demonstration: Emit all 6-bit RGB colors with a script.
The following shell script uses the 38;5 escape function to create 216 background colors (The red, green and blue components are each in the range 0 … 5)
#
# vim: ft=sh
#
ansi_color_5() {
local r=$1 # 0 .. 5
local g=$2 # 0 .. 5
local b=$3 # 0 .. 5
echo -e $(( 16+ $r*36 + $g*6 + $b))
}
ansi_color_bg_5 () {
echo -e "\e[38;5;$(ansi_color_5 $1 $2 $3)m"
}
ansi_color_fg_5 () {
echo -e "\e[48;5;$(ansi_color_5 $1 $2 $3)m"
}
ansi_reset() {
echo -e "\e[0m"
}
printf "\n"
for r in {0..5}; do
for g in {0..5}; do
for b in {0..5}; do
printf " "
if [[ $r > 3 || $g > 3 ]]; then
bg_color='0 0 0'
else
bg_color='5 5 5'
fi
printf "$(ansi_color_fg_5 $r $g $b)$(ansi_color_bg_5 $bg_color) %3d " $(ansi_color_5 $r $g $b)
done
printf $(ansi_reset)
printf "\n"
done
printf "\n"
done