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


Answers:


94

256色测试图案

要获得下图,请使用:

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

256色测试图案

要点bash/ zsh代码shellcheck干净的,并且还支持“妈妈快看,没有子进程!”。


另外,为了快速bash

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位)测试图案


7
我喜欢您在脚本GitHub页面上对灰度的评论-“ #Not 50,但有24个灰色阴影”
MadisonCooper 2016年


要运行terminal-colors,请做curl -s https://raw.githubusercontent.com/eikenb/terminal-colors/master/terminal-colors | python
masterxilo

@masterxilo是什么terminal-colors,它与我建议的选项相比如何?
汤姆·黑尔'18

为文本而非背景着色时,printf模式会是什么样?
ianstarz

35

Justin Abrahms的GitHub上找到了一个不错的Python脚本,该脚本还打印了颜色的十六进制代码。

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

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, 


7

我编写的另一个脚本位于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的输出


2
curl -s -L https://git.gnome.org/browse/vte/plain/perf/256test.sh?h=vte-0-38 | bash
masterxilo

6

也许是多余的,但我编写了一个版本,该版本使用自动外壳宽度检测功能在背景上打印256种颜色,因此颜色更容易看到。

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='')

2
如果有人想在一个脚本中运行此脚本,请运行curl https://gist.githubusercontent.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3/raw/250eb2e3f2acca1c51aa52adf611ec0380291e8a/colortest.py | python3
Tommaso Thea Cioni,

我建议curl -s https://gist.githubusercontent.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3/raw/colortest.py | python3
masterxilo

3

一线

背景颜色

for i in {0..255}; do printf '\e[48;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done

前景色

for i in {0..255}; do printf '\e[38;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.