Files
dotfiles/local/bin/x-term-colors
Ismo Vuorinen 7c9096d666 fix: standardize shebangs, error handling, and minor issues in x-* scripts
Normalize shebangs to #!/usr/bin/env bash (x-env-list, x-localip).
Use XDG_CONFIG_HOME in x-change-alacritty-theme. Remove unused
VERBOSE variable in x-multi-ping. Add set -euo pipefail to x-when-down
and x-when-up. Add usage header to x-term-colors. Fix notify-call
to notify-send.sh in x-record.
2026-02-05 22:07:03 +02:00

97 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Display 24-bit terminal color test.
#
# Usage: x-term-colors
#
# The foreground escape sequence is ^[38;2;<r>;<g>;<b>m
# The background escape sequence is ^[48;2;<r>;<g>;<b>m
# <r> <g> <b> range from 0 to 255 inclusive.
# The escape sequence ^[0m returns output to default
setBackgroundColor()
{
echo -en "\x1b[48;2;$1;$2;$3""m"
}
resetOutput()
{
echo -en "\x1b[0m\n"
}
# Gives a color $1/255 % along HSV
# Who knows what happens when $1 is outside 0-255
# Echoes "$red $green $blue" where
# $red $green and $blue are integers
# ranging between 0 and 255 inclusive
rainbowColor()
{
((h = $1 / 43))
((f = $1 - 43 * h))
((t = f * 255 / 43))
((q = 255 - t))
if [ $h -eq 0 ]; then
echo "255 $t 0"
elif [ $h -eq 1 ]; then
echo "$q 255 0"
elif [ $h -eq 2 ]; then
echo "0 255 $t"
elif [ $h -eq 3 ]; then
echo "0 $q 255"
elif [ $h -eq 4 ]; then
echo "$t 0 255"
elif [ $h -eq 5 ]; then
echo "255 0 $q"
else
# execution should never reach here
echo "0 0 0"
fi
}
for i in $(seq 0 127); do
setBackgroundColor "$i" 0 0
echo -en " "
done
resetOutput
for i in $(seq 255 128); do
setBackgroundColor "$i" 0 0
echo -en " "
done
resetOutput
for i in $(seq 0 127); do
setBackgroundColor 0 "$i" 0
echo -n " "
done
resetOutput
for i in $(seq 255 128); do
setBackgroundColor 0 "$i" 0
echo -n " "
done
resetOutput
for i in $(seq 0 127); do
setBackgroundColor 0 0 "$i"
echo -n " "
done
resetOutput
for i in $(seq 255 128); do
setBackgroundColor 0 0 "$i"
echo -n " "
done
resetOutput
for i in $(seq 0 127); do
# shellcheck disable=SC2046 # Intentional word splitting to pass 3 RGB values as separate args
setBackgroundColor $(rainbowColor "$i")
echo -n " "
done
resetOutput
for i in $(seq 255 128); do
# shellcheck disable=SC2046 # Intentional word splitting to pass 3 RGB values as separate args
setBackgroundColor $(rainbowColor "$i")
echo -n " "
done
resetOutput