Skip to content

Instantly share code, notes, and snippets.

@amancevice
Last active September 16, 2021 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amancevice/eef0837944f85bc7c8e64c37209a202f to your computer and use it in GitHub Desktop.
Save amancevice/eef0837944f85bc7c8e64c37209a202f to your computer and use it in GitHub Desktop.
Prints the color codes for 256 colors
#!/bin/bash
rainbow_base_colors() {
for y in {0..1} ; do
for x in {0..7} ; do
c=$(( $x + (8 * $y) ))
tput $t $c
printf " %3s " $c
done
printf '\e[0m\n'
done
printf '\e[0m'
}
rainbow_ansii_colors() {
a=16
b=16
c=16
for z in {0..5} ; do
for y in {0..5} ; do
for x in {0..5} ; do
tput $t $a
printf " %3s " $a
a=$(( $a + 1 ))
done
for x in {0..5} ; do
tput $t $b
printf " %3s " $b
b=$(( $b + 36 ))
done
for x in {0..5} ; do
tput $t $c
printf " %3s " $c
c=$(( $c + 1 ))
done
printf '\e[0m\n'
b=$(( $b - 210 ))
c=$(( $c + 30 ))
done
b=$(( $b - 35 ))
c=$(( $c - 210 ))
printf '\e[0m'
done
}
rainbow_b_and_w_colors() {
for y in {0..2} ; do
for x in {0..7} ; do
c=$(( 232 + $x + (8 * $y) ))
tput $t $c
printf " %3s " $c
done
printf '\e[0m\n'
done
}
rainbow_help() {
cat <<-HELP
Usage: rainbow [ --background | --foreground | -b | -f ] [CODE...]
HELP
}
main() {
local args=() t=setab
while (( $# )) ; do
case "$1" in
-h | --help) rainbow_help ; return ;;
-b | --background) t=setab ; shift ;;
-f | --foreground) t=setaf ; shift ;;
*) args+=($1) ; shift ;;
esac
done
if [ "${#args}" -eq 0 ] ; then
rainbow_base_colors
rainbow_ansii_colors
rainbow_b_and_w_colors
printf '\e[0m'
else
for c in ${args[@]} ; do
tput $t $c
printf " %3s " $c
done
printf '\e[0m\n'
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment