256色表及其分区
256色终端的颜色范围由4个部分组成,通常是5个部分,这种情况下实际上会得到258种颜色:
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:
number = 16 + 36 * r + 6 * g + b
with r
, g
and b
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'
).
一些来源:
默认RGB值
理论上,为了获得均匀分布的颜色范围,可以按照如下计算方法获取范围16-231内颜色的RGB值:
# example in Python: // is integer divison, % is modulo
rgb_R = ((number - 16) // 36) * 51
rgb_G = (((number - 16) % 36) // 6) * 51
rgb_B = ((number - 16) % 6) * 51
但是事实上,实际方法似乎是不同的:
我测试过的任何终端仿真器似乎都遵循XTerm并将红色、绿色和蓝色的值[0, 1, 2, 3, 4, 5]
映射到RGB颜色轴上的值[0, 95, 135, 175, 215, 255]
。(我测试了XTerm(297)URxvt(v9.19),ROXTerm(2.8.1),gnome-terminal(3.6.2)和xfce4-terminal(0.6.3))
给定索引的RGB值可以使用以下算法计算:
# example in Python: 'a = b if c else d' is 'a = (c) ? b : d` in C, Perl, etc.
index_R = ((number - 16) // 36)
rgb_R = 55 + index_R * 40 if index_R > 0 else 0
index_G = (((number - 16) % 36) // 6)
rgb_G = 55 + index_G * 40 if index_G > 0 else 0
index_B = ((number - 16) % 6)
rgb_B = 55 + index_B * 40 if index_B > 0 else 0
灰度似乎遵循这个简单的公式:
rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
256colres.pl
位于XTerm源代码(版本313)的根目录中,使用类似的算法生成256colres.h
,其中包含256色模式的颜色定义:
$line1="COLOR_RES(\"%d\",";
$line2="\tscreen.Acolors[%d],";
$line3="\tDFT_COLOR(\"rgb:%2.2x/%2.2x/%2.2x\")),\n";
for ($red = 0; $red < 6; $red++) {
for ($green = 0; $green < 6; $green++) {
for ($blue = 0; $blue < 6; $blue++) {
$code = 16 + ($red * 36) + ($green * 6) + $blue;
printf($line1, $code);
printf($line2, $code);
printf($line3,
($red ? ($red * 40 + 55) : 0),
($green ? ($green * 40 + 55) : 0),
($blue ? ($blue * 40 + 55) : 0));
}
}
}
$code=232;
for ($gray = 0; $gray < 24; $gray++) {
$level = ($gray * 10) + 8;
$code = 232 + $gray;
printf($line1, $code);
printf($line2, $code);
printf($line3,
$level, $level, $level);
}
在终端中显示可用颜色
这是一个zsh函数,可以在256色终端上打印所有颜色(如果TERM
设置为256色值):
function termcolors ()
{
print TERM
print -P "Foreground: >█<"
print -P "Background: >%S█%s<\n"
print " 0 1 2 3 4 5 6 7"
for b (0 1)
do
printf "%d %2d " $b $(( 8 * b ))
for r (0 1 2 3 4 5 6 7)
do
c=$(( 8 * b + r ))
print -nP "%K{$c} %k"
done
printf " %2d\n" $(( 8 * b + 7 ))
done
print
print RGB
for r (0 1 2 3 4 5)
do
print "$r $(( 16 + 36 * r )) - $(( 16 + 36 * r + 35 ))\n 0 1 2 3 4 5"
for g (0 1 2 3 4 5)
do
printf "%d %3d " $g $(( 16 + 36 * r + 6 * g ))
for b (0 1 2 3 4 5)
do
c=$(( 16 + 36 * r + 6 * g + b ))
print -nP "%K{$c} %k"
done
printf " %3d\n" $(( 16 + 36 * r + 6 * g + 5))
done
print
done
print
print GRAY
for g in $(seq 0 23)
do
c=$(( 232 + g ))
printf "%2d %3d " $g $c
print -P "%K{$c} %k"
done
}
运行时更改RGB值
在某些终端(至少包括xterm
,gnome-terminal
,termite
和urxvt
),可以通过发送以下XTerm控制序列之一来更改所有这些颜色:
OSC 4
OSC 4
其中:
OSC
是转义字符 (\e
或 \033
) 后跟随 ]
c
是颜色编号 (0 - 255)
spec
是颜色规范 (例如 red
, #ff0000
, rgb:ff/00/00
, rgbi:1/0/0
- 实际可行性可能取决于终端)
BEL
是响铃字符 (\a
或 \007
)
ST
是字符串终止符 \e\\
或 \033\\
可以通过简单地使用 echo
命令来发送这些控制序列:
echo -en "\e]4;COLOR;SPEC\a"
echo -en "\e]4;COLOR;SPEC\a"
例如,为了将颜色编号5(通常是品红色调)设置为红色,以下任意一种方法都可以:
echo -en "\e]4;5;red\a"
echo -en "\e]4;5;#ff0000\e\\"
echo -en "\033]4;5;rgb:ff/00/00\007"
这些颜色可以通过其中一个控制序列重置为它们的(配置的)默认值。
OSC 104 ; c BEL
OSC 104 ; c ST
因此,以下循环将重置所有颜色从0到255的配置或默认值:
for c in {0..255}; do
echo -en "\e]104;$c\a"
done
默认的前景色和背景色的控制序列分别为
OSC 10 ; spec BEL
和
OSC 11 ; spec BEL
。例如:
echo -en "\e]10;red\a"
echo -en "\e]11;green\a"
这些可以通过分别使用
OSC 110 BEL
和
OSC 111 BEL
来重置:
echo -en "\e]110\a"
echo -en "\e]111\a"