当前位置: 首页>>技术问答>>正文


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

, ,

问题描述

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

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

最佳解决方案

256色测试图案

要获得以下图像,请使用:

curl -s https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/ | bash

command-line,colors,ubuntu

要点bash /zsh代码是shellcheck清洁,并且还支持“Look Ma,没有子进程!”。


或者,对于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,一个带有多个output formats572-line script

你也可以print a true color (24-bit) test pattern

次佳解决方案

我找到了由Justin Abrahms编写的nice Python script for that on GitHub,它还打印了颜色的十六进制代码。

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

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

给它执行权限

chmod +x colortest.py

运行:

./colortest.py

command-line,colors,ubuntu

在link-rot的情况下,这是完整的脚本:

#!/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, 

第三种解决方案

虽然不是”test pattern”,但我有xterm-color-chooser

command-line,colors,ubuntu

第四种方案

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

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

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

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

command-line,colors,ubuntu

第五种方案

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

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

command-line,colors,ubuntu

#!/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='')

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/1841.html,未经允许,请勿转载。