Skip to content

Instantly share code, notes, and snippets.

@glektarssza
Created September 12, 2025 16:02
Show Gist options
  • Select an option

  • Save glektarssza/16099bcbfa7139afaa26de47887877a9 to your computer and use it in GitHub Desktop.

Select an option

Save glektarssza/16099bcbfa7139afaa26de47887877a9 to your computer and use it in GitHub Desktop.
Dump the full range of 8-bit terminal colors in a nice table format!
#!/usr/bin/env bash
# Dump the full range of 8-bit colors available in the terminal!
# RAINBOWS!
function dump_8bit_colors() {
# -- Forward declare variables!
local FG;
for i in {0..255}; do
FG="15";
if [[ $(( ("$i" - 16) % 36 )) -eq 0 ]]; then
# -- Newline between each block of 36 colors except the first 16 and
# -- gray-scale which are on their own line.
echo;
fi
# -- Pick an appropriate foreground (text) color.
# -- This is kind of hardcoded because the first 16 colors are special...
# -- The rest use a generic equation where the second half of each 36
# -- block of colors use a block foreground color and the first half
# -- white.
if [[ $i -eq 3 || ($i -ge 7 && $i -le 15) || ($(( ($i - 16) % 36 )) \
-ge 18 && $(( ($i - 16) % 36 )) -le 35) ]]; then
FG="0";
fi
# -- Print the color with its numerical color code using an appropriate
# -- text color.
# -- Color is the background color (first `\x1b[`).
# -- Then the foreground (text) color.
# -- Then the numerical color, prefixed by 0s, so the number fills 3
# -- digits to ensure uniform cell spacing.
printf "\x1b[48;5;%dm\x1b[38;5;%dm%03d\x1b[0m" "${i}" "${FG}" "$i";
if [[ "$i" -eq 255 ]];then
# -- Put gray-scale colors on their own line.
echo;
fi;
done
}
dump_8bit_colors;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment