How do I print a 256-colour test pattern in my terminal?
I want to check that my terminal correctly supports 256 colours.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityTo get the below image, use:
curl -s https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/e50a28ec54188d2413518788de6c6367ffcea4f7/print256colours.sh | bash
The gist bash
/zsh
code is shellcheck
clean, and also supports "Look Ma, no subprocesses!".
Alternatively, for a 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
For total overkill, the granddaddy of the lot is terminal-colors
, a 572-line script with multiple output formats.
You can also print a true color (24-bit) test pattern.
terminal-colors
, do curl -s https://raw.githubusercontent.com/eikenb/terminal-colors/master/terminal-colors | python
Dec 5, 2018 at 18:22
terminal-colors
and how does it compare to the options I suggested?
I found a nice Python script for that on GitHub written by Justin Abrahms which also prints the hex codes of the colours.
Download the script to current working directory
wget https://gist.githubusercontent.com/justinabrahms/1047767/raw/a79218b6ca8c1c04856968d2d202510a4f7ec215/colortest.py
give it execute permission
chmod +x colortest.py
Run it:
./colortest.py
Here's the script in full in case of 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,
curl https://gist.githubusercontent.com/justinabrahms/1047767/raw/a79218b6ca8c1c04856968d2d202510a4f7ec215/colortest.py | python
Nov 13, 2021 at 19:21
While not quite a "test pattern", I have xterm-color-chooser:
curl -s https://raw.githubusercontent.com/grawity/code/master/term/xterm-color-chooser | python3
Dec 5, 2018 at 18:24
Yet another script, written by me, is located in the VTE repository: https://git.gnome.org/browse/vte/plain/perf/256test.sh?h=vte-0-38.
It requires a window of 120-ish or more columns, but arranges the colors of the 6x6x6 cube nicely and compactly. The first digits of the indices are stripped for compactness, you can easily figure them out. The vertical bars provide you the ability to examine the exact RGB of the foreground color without antialiasing kicking in (as it does at the digits).
The top of the output (not shown in the screenshot below) demonstrates the craziness that goes around with the bold vs. bright ambiguity, namely that the boldness escape sequence combined with one of the legacy 8 colors' escape sequence for the foreground also switches to the bright counterpart color, whereas with the new style (256-color capable) escape sequences this is no longer the case, not even for the first 8 colors. At least that's how xterm and VTE (GNOME Terminal etc.) behave.
This screenshot shows about half of the output:
curl -s -L https://git.gnome.org/browse/vte/plain/perf/256test.sh?h=vte-0-38 | bash
Dec 5, 2018 at 18:25
A one-liner
background color
for i in {0..255}; do printf '\e[48;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done
foreground color
for i in {0..255}; do printf '\e[38;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done
Perhaps superfluous but I've written a version that prints the 256 colors using the background with automatic shell width detection so the colors are more easily visible.
https://gist.github.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3
#!/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='')
curl https://gist.githubusercontent.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3/raw/250eb2e3f2acca1c51aa52adf611ec0380291e8a/colortest.py | python3
Jul 26, 2018 at 17:21
curl -s https://gist.githubusercontent.com/WoLpH/8b6f697ecc06318004728b8c0127d9b3/raw/colortest.py | python3
Dec 5, 2018 at 18:27
I liked @Tom's answer, but depending on the use case I like to be able to see black and white on the color, and the color on white and black. So I modified it a bit to:
#!/bin/bash
for i in {0..255} ; do
# Black FG on color BG
printf "\e[30;48;5;%sm%4d " "$i" "$i"
# White FG on color BG
printf "\e[97m%4d " "$i"
# Color FG on black BG
printf "\e[40;38;5;%sm%4d " "$i" "$i"
# Color FG on white BG
printf "\e[107m%4d " "$i"
# Check whether to print new line
[ $(( ($i + 1) % 4 )) == 0 ] && set1=1 || set1=0
[ $(( ($i - 15) % 6 )) == 0 ] && set2=1 || set2=0
if ( (( set1 == 1 )) && (( i <= 15 )) ) || ( (( set2 == 1 )) && (( i > 15 )) ); then
printf "\e[0m\n";
fi
done
It's an old question, but here's a Python 3 / bash one-liner:
python -c "print('\t'.join(f'\u001b[1;38;5;{s}m{s.ljust(4)}' + ('\n' if not int(s) % 8 else '') for s in (str(i) for i in range(256))) + '\u001b[0m')"
/cubes
in irssi (source)