bash Ansi 扩展颜色索引中颜色的 RGB 值 (17-255)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27159322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RGB values of the colors in the Ansi extended colors index (17-255)
提问by Jim Knecht
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).
我的问题是使用 ansi 颜色的一般 shell 脚本,但作为参考,我使用的是 Apple Mac OS X 10.9 Mavericks。我使用“iTerm”终端应用程序作为我的默认终端,但也使用内置的“终端”应用程序进行检查。我使用 ZSH (5.0.7) 作为我的默认 shell,但也检查了 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.
我一直试图找出是否有 256 种颜色索引的扩展前景/背景 Ansi 转义码的 RGB 值列表,这些值可以使用 esc[38;5;xm 和 esc[48;5;xm,其中 x 是一个从 0 到 255 的数字。我发现了一些将颜色打印为块(使用索引)的脚本,但我想知道索引中每种颜色的 rgb 值。
Here is an example of the ansi codes in use:
这是正在使用的 ansi 代码的示例:
printf '\e[38;5;222;48;5;238m Hi \e[m\n'
(\e can be replaced by \033 or \x1b)
(\e 可以替换为 \033 或 \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.
所以基本上我想知道是否有这些索引颜色的列表或商定的设置?例如 232-255 似乎总是一个灰色渐变。我发现的每个引用扩展颜色的网站几乎都只是说明了如何使用它,并没有列出任何特定的颜色。
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?
我发现了一些对 X11 和 rgb.txt 文件的引用,起初看起来这正是我要找的,但它们似乎与索引号不匹配。我发现的文件中有 752 种颜色(大多数是重复的,所以说 376 仍然是 256)。还有 50 种灰色阴影(如果计算重复则为 100 种),但 Ansi 索引颜色有 23 种,因此看起来不一样。如果这些以某种方式确实包含 ansi 扩展颜色索引中的颜色,是否有人有哪些名称在哪个索引中的列表?
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.
附注。我知道 esc[38;2;r;g:bm 可以使用 rgb 值设置颜色,但我不能让它在我的 Mac 上工作,我对索引颜色的默认值更感兴趣。
Some sites: (can only post 2 due to low rep? But I checked a lot of sites)
一些网站:(由于低代表只能发布2个?但我检查了很多网站)
This one has the rgb for the standard colors but not the extended ones.
回答by Adaephon
The 256 color table and its partitioning
256色表及其分区
The color range of a 256 color terminal consists of 4 parts, often 5, in which case you actually get 258 colors:
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
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'
).
颜色编号 0 到 7 是默认的终端颜色,其实际 RGB 值没有标准化,通常可以配置。
颜色编号 8 到 15 是“明亮”的颜色。大多数情况下,这些是索引为 8 的较浅的颜色。它们也不是标准化的,通常可以配置。根据终端和外壳,它们经常代替粗体字体或与粗体字体结合使用。
颜色编号 16 到 231 是 RGB 颜色。这 216 种颜色由三个 RGB 轴中每个轴上的 6 个值定义。也就是说,每种颜色的范围不是 0 - 255,而是 0 - 5。
然后像这样计算颜色数:
number = 16 + 36 * r + 6 * g + b
与
r
,g
并且b
在 0 - 5 范围内。色号 232 到 255 是灰度,从深到浅有 24 种灰度。
前景和背景的默认颜色。在许多终端中,它们可以独立于 256 种索引颜色进行配置,从而提供额外的两种可配置颜色。当不设置任何其他颜色或禁用其他颜色(即
print '\e[m'
)时,您会得到它们。
Some sources:
一些来源:
urxvt
manpage:In addition to the default foreground and background colours, urxvt can display up to 88/256 colours: 8 ANSI colours plus high-intensity (potentially bold/blink) versions of the same, and 72 (or 240 in 256 colour mode) colours arranged in an 4x4x4 (or 6x6x6) colour RGB cube plus a 8 (24) colour greyscale ramp.
xterm
manpage:These specify the colors for the 256-color extension. The default resource values are for colors 16 through 231 to make a 6x6x6 color cube, and colors 232 through 255 to make a grayscale ramp.
Wikipedia article on ANSI escape codes(which in turn itself is lacking a citation on the topic)
urxvt
联机帮助页:除了默认的前景色和背景色之外,urxvt 最多可以显示 88/256 种颜色:8 种 ANSI 颜色加上相同的高强度(可能是粗体/闪烁)版本,以及 72 种(或 256 色模式下的 240 种)颜色排列在 4x4x4(或 6x6x6)彩色 RGB 立方体加上 8(24)色灰度渐变中。
xterm
联机帮助页:这些指定了 256 色扩展的颜色。默认资源值用于颜色 16 到 231 以生成 6x6x6 颜色立方体,颜色 232 到 255 用于生成灰度渐变。
关于 ANSI 转义码的维基百科文章(这本身又缺乏对该主题的引用)
Default RGB values
默认 RGB 值
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:
理论上,为了获得均匀分布的颜色范围,可以这样计算 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
Butit 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))
我测试过的任何终端模拟器似乎都遵循 XTerm 并将[0, 1, 2, 3, 4, 5]
红色、绿色和蓝色的值映射到[0, 95, 135, 175, 215, 255]
RGB 色轴上的值。(我使用 XTerm (297) URxvt (v9.19)、ROXTerm (2.8.1)、gnome-terminal (3.6.2) 和 xfce4-terminal (0.6.3) 进行了测试)
The RGB values for a given index can be calculated with this algorithm:
可以使用以下算法计算给定索引的 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
The grayscale seems to follow this simple formula:
灰度似乎遵循这个简单的公式:
rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
256colres.pl
in the root of the XTerm sources (version 313)uses a similar algorithm to generate 256colres.h
, which contains the color definitions for 256 color mode:
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";
# colors 16-231 are a 6x6x6 color cube
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));
}
}
}
# colors 232-255 are a grayscale ramp, intentionally leaving out
# black and white
$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);
}
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):
这是在 256 色终端上打印所有颜色的 zsh 函数(如果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
}
Changing RGB values during runtime
在运行时更改 RGB 值
In some terminals (at least xterm
, gnome-terminal
, termite
and urxvt
) all those colors can be changed during runtime by sending one of the following XTerm Control Sequences:
在一些端子(至少xterm
,gnome-terminal
,termite
和urxvt
)所有这些颜色可以在运行时通过发送下列之一被改变的XTerm控制序列:
OSC 4; c ; spec BEL
OSC 4; c ; spec ST
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\\
OSC
是转义字符(\e
或\033
)后跟]
c
是颜色编号 (0 - 255)spec
是颜色规格(例如red
,#ff0000
,rgb:ff/00/00
,rgbi:1/0/0
-什么实际工作可能取决于在终端上)BEL
是铃字符(\a
或\007
)ST
是字符串终止符\e\\
或\033\\
These control sequences can be sent by simply printing them with echo
:
这些控制序列可以通过简单地打印它们来发送echo
:
echo -en "\e]4;COLOR;SPEC\a"
echo -en "\e]4;COLOR;SPEC\a"
For example, in order to set color number 5 (usually some shade of magenta) to red, either of these should work:
例如,为了将颜色编号 5(通常是某种洋红色)设置为红色,以下任一方法都应起作用:
echo -en "\e]4;5;red\a"
echo -en "\e]4;5;#ff0000\e\"
echo -en "3]4;5;rgb:ff/00/00OSC 104 ; c BEL
OSC 104 ; c ST
7"
Those colors can be reset to their (configured) default with one of the control sequences
可以使用控制序列之一将这些颜色重置为其(配置的)默认值
for c in {0..255}; do
echo -en "\e]104;$c\a"
done
So the following loop will reset all colors from 0 to 255 to their configured or default value:
因此,以下循环会将所有颜色从 0 到 255 重置为其配置或默认值:
echo -en "\e]10;red\a"
echo -en "\e]11;green\a"
For the default foreground and background colors the control sequences are OSC 10 ; spec BEL
and OSC 11 ; spec BEL
, respectively. For example:
对于默认的前景色和背景色,控制序列分别是OSC 10 ; spec BEL
和OSC 11 ; spec BEL
。例如:
echo -en "\e]110\a"
echo -en "\e]111\a"
Those can be reset with OSC 110 BEL
and OSC 111 BEL
respectively:
这些可以分别用OSC 110 BEL
和重置OSC 111 BEL
:
rgbtohex () {
# usage) `rgbtohex 17 0 26` ==> 1001A
# usage) `rgbtohex -h 17 0 26` ==> #1001A
addleadingzero () { awk '{if(length(##代码##)<2){printf "0";} print ##代码##;}';}
if [[ == "-h" ]]; then
r=; g=; b=;h='#';
else
r=; g=; b=;h='';
fi
r=`echo "obase=16; ${r}" | bc | addleadingzero`
g=`echo "obase=16; ${g}" | bc | addleadingzero`
b=`echo "obase=16; ${b}" | bc | addleadingzero`
echo "${h}${r}${g}${b}"
}
rgbto256 () {
# usage: `rgbto256 0 95, 135` ==> 22
echo "define trunc(x){auto os;os=scale;scale=0;x/=1;scale=os;return x;};" \
"16 + 36 * trunc(/51) + 6 * trunc(/51) +" \
" trunc(/51)" | bc
# XTerm Color Number = 16 + 36 * R + 6 * G + B | 0 <= R,G,B <= 5
}
hextorgb () {
# usage) `hexttorgb "11001A" ==> 17 0 26
# usage) `hexttorgb "#11001A" ==> 17 0 26
hexinput=`echo | tr '[:lower:]' '[:upper:]'` # uppercase-ing
hexinput=`echo ${hexinput} | tr -d '#'` # remove Hash if needed
a=`echo ${hexinput} | cut -c-2`
b=`echo ${hexinput} | cut -c3-4`
c=`echo ${hexinput} | cut -c5-6`
r=`echo "ibase=16; ${a}" | bc`
g=`echo "ibase=16; ${b}" | bc`
b=`echo "ibase=16; ${c}" | bc`
echo ${r} ${g} ${b}
}
trueHexPrint () {
# Generates Truecolor Escape Sequences from Hex Strings. (remove '\' to use)
# -fg Prints as a foreground color. (default)
# -bg Prints as a background color.
# usage) `trueHexPrint -fg "11001A" ==> '\e[38;2;17;0;26m'
# usage) `trueHexPrint -bg "11001A" ==> '\e[48;2;17;0;26m'
if [[ =~ "-fg" || =~ "-f" ]]; then
fgbg=38; hexinput=;
elif [[ =~ "-bg" || =~ "-b" ]]; then
fgbg=48; hexinput=;
else
fgbg=38; hexinput=
fi
hexinput=`echo ${hexinput} | tr '[:lower:]' '[:upper:]'` # uppercase-ing
hexinput=`echo ${hexinput} | tr -d '#'` # remove Hash if needed
a=`echo ${hexinput} | cut -c-2`
b=`echo ${hexinput} | cut -c3-4`
c=`echo ${hexinput} | cut -c5-6`
r=`echo "ibase=16; ${a}" | bc`
g=`echo "ibase=16; ${b}" | bc`
b=`echo "ibase=16; ${c}" | bc`
printf "\\e[${fgbg};2;${r};${g};${b}m" # Remove one set of '\' to utilize
}
XColorTable () {
i=16
for ((r = 0; r <= 255; r+=40)); do # Do Tricolor
for ((g = 0; g <= 255; g+=40)); do
for ((b = 0; b <= 255; b+=40)); do
echo "Color$((i++)) = (${r}, ${g}, ${b})"
if ((b == 0)); then b=55; fi
done
if ((b == 0)); then g=55; fi
done
if ((r == 0)); then r=55; fi
done
for ((m = 8; m <= 238; m+=10)); do # Do Monochrome
echo "Color$((i++)) = (${m}, ${m}, ${m})"
done
}
#vim: ft=sh
回答by Anthony Bourdain
Here are my color utilities. The most useful is probably the Hex->True, or RGB->256 converters. A lot of this I put together thanks to y'alls help <3
这是我的颜色实用程序。最有用的可能是 Hex->True 或 RGB->256 转换器。感谢你们的帮助,我整理了很多内容 <3
##代码##