在终端中打印256色测试图案

如何在终端中打印256色测试图案?

我想检查我的终端是否正确支持256色。

256色测试图案

256色测试图案

对于上面的内容,您可以使用我的gist bash / zsh代码 ,它是shellcheck clean,并且还支持“Look Ma,no shellcheck !”

或者对于bash quicky:

 for i in {0..255} ; do printf "\x1b[48;5;%sm%3d\e[0m " "$i" "$i" if (( i == 15 )) || (( i > 15 )) && (( (i-15) % 6 == 0 )); then printf "\n"; fi done 

对于总矫枉过正,该地段的爷爷是terminal-colors ,一个572行的脚本,具有多种输出格式 。

您还可以打印真彩色(24位)测试图案 。

我在 Justin Abrahms编写的GitHub上找到了一个很好的Python脚本,它也打印了颜色的hex代码。

将脚本下载到当前工作目录

 wget https://gist.githubusercontent.com/justinabrahms/1047767/raw/a79218b6ca8c1c04856968d2d202510a4f7ec215/colortest.py 

给它执行权限

 chmod +x colortest.py 

运行:

 ./colortest.py 

在链接腐烂的情况下,这是完整的脚本:

 #!/usr/bin/env python # Ported to Python from http://www.vim.org/scripts/script.php?script_id=1349 print "Color indexes should be drawn in bold text of the same color." print colored = [0] + [0x5f + 40 * n for n in range(0, 5)] colored_palette = [ "%02x/%02x/%02x" % (r, g, b) for r in colored for g in colored for b in colored ] grayscale = [0x08 + 10 * n for n in range(0, 24)] grayscale_palette = [ "%02x/%02x/%02x" % (a, a, a) for a in grayscale ] normal = "\033[38;5;%sm" bold = "\033[1;38;5;%sm" reset = "\033[0m" for (i, color) in enumerate(colored_palette + grayscale_palette, 16): index = (bold + "%4s" + reset) % (i, str(i) + ':') hex = (normal + "%s" + reset) % (i, color) newline = '\n' if i % 6 == 3 else '' print index, hex, newline, 

虽然不是一个“测试模式”,但我有xterm-color-chooser :

截图

另一个由我编写的脚本位于VTE存储库中: https : //git.gnome.org/browse/vte/plain/perf/256test.sh? h = vte-0-38。

它需要一个120列或更多列的窗口,但可以很好地和紧凑地排列6x6x6立方体的颜色。 索引的第一个数字被剥离以便紧凑,您可以轻松找出它们。 垂直条使您能够检查前景色的精确RGB而无需使用抗锯齿(就像在数字处一样)。

输出的顶部(未在下面的屏幕截图中显示)展示了大胆与明亮模糊的疯狂,即粗体转义序列与前景的传统8色转换序列之一相结合也转换为明亮的对应颜色,而新的风格(256色能力)转义序列不再是这种情况,即使是前8种颜色也是如此。 至少这就是xterm和VTE(GNOME终端等)的行为方式。

此屏幕截图显示了大约一半的输出:

在GNOME终端中输出256test.sh

也许是多余的,但我写了一个版本,使用背景打印256色,自动shell宽度检测,因此颜色更容易看到。

https://gist.github.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3

256色测试演示

 #!/usr/bin/env python from __future__ import print_function import os import shutil import subprocess def get_width(default=80): '''Attempt to detect console width and default to 80''' try: columns, rows = shutil.get_terminal_size() except AttributeError: try: _, columns = subprocess.check_output(['stty', 'size']).split() except OSError: columns = os.environ.get('COLUMNS', default) columns = int(columns) - 77 # Since we have 6 columns with 1 space on each side, we can increment the # size for every 12 extra columns return max(0, columns / 12) # Loosely based on https://gist.github.com/justinabrahms/1047767 colored = [0] + list(range(95, 256, 40)) colored_palette = [ (r, g, b) for r in colored for g in colored for b in colored ] grayscale_palette = [(g, g, g) for g in range(8, 240, 10)] esc = '\033[' # Reset all colors sequence reset = esc + '0m' # Regular color normal = esc + '38;5;{i}m' # Bold color bold = esc + '1;' + normal # Background color background = esc + '48;5;{i}m' pattern = ( '{normal}{background}{padding:^{width}}{i:^3d} ' # pad the background '{r:02X}/{g:02X}/{b:02X}' # show the hex rgb code '{padding:^{width}}' # pad the background on the other side '{reset}' # reset again ) base_context = dict(reset=reset, padding='', width=get_width()) for i, (r, g, b) in enumerate(colored_palette + grayscale_palette, 16): context = dict(i=i, r=r, g=g, b=b, color=r + g + b, **base_context) context.update(bold=bold.format(**context)) context.update(background=background.format(**context)) # Change text color from black to white when it might become unreadable if max(r, g, b) > 0xCC: context.update(normal=normal.format(i=0)) else: context.update(normal=normal.format(i=255)) print(pattern.format(**context), end='') # Print newlines when needed if i % 6 == 3: print() else: print(' ', end='')