dotfiles/bin/colors
Fernando Schauenburg 542f82d267 bin: fix colors on Debian
On Debian, /bin/sh is /bin/dash, which doesn't support substring
expansions. Use `awk` instead.
2023-07-24 21:32:41 +02:00

144 lines
3 KiB
Bash
Executable file

#!/bin/sh
fg_16() {
case $1 in
0|1|2|3|4|5|6|7) printf '\e[3%sm' "$1";;
8|9|10|11|12|13|14|15) printf '\e[9%sm' "$(($1 - 8))";;
*) ;;
esac
}
bg_16() {
case $1 in
0|1|2|3|4|5|6|7) printf '\e[4%sm' "$1";;
8|9|10|11|12|13|14|15) printf '\e[10%sm' "$(($1 - 8))";;
*) ;;
esac
}
fg_256() { printf '\e[38;5;%sm' "$1"; }
bg_256() { printf '\e[48;5;%sm' "$1"; }
fg_rgb() { printf '\e[38;2;%s;%s;%sm' "$1" "$2" "$3"; }
bg_rgb() { printf '\e[48;2;%s;%s;%sm' "$1" "$2" "$3"; }
rgb_from_hex() {
echo "$1" | awk '{
r="0x"substr($0, 2, 2)
g="0x"substr($0, 4, 2)
b="0x"substr($0, 6, 2)
printf "%d %d %d", r, g, b
}'
}
# shellcheck disable=SC2046 # word splitting is intentional here
fg_hex() { fg_rgb $(rgb_from_hex "$1"); }
# shellcheck disable=SC2046 # word slitting is intentional here
bg_hex() { bg_rgb $(rgb_from_hex "$1"); }
reset() { printf '\e[0m'; }
fg_reset() { printf '\e[39m'; }
bg_reset() { printf '\e[49m'; }
indent() { printf ' '; }
newline() { printf '%b\n' '\e[0m'; }
indexed_16() {
echo "16 indexed colors:"
for i in 0 1; do
indent
for j in $(seq 0 7); do
bg_16 $((8*i + j)); printf '%4s'
done
newline
done
}
indexed_256() {
echo "256 indexed colors:"
# 16 standard colors
for i in 0 1; do
indent
for j in $(seq 0 7); do
bg_256 $((8*i + j)); printf '%9s'
done
newline
done
# 216 colors
for i in 0 18; do
for j in $(seq 0 5); do
indent
for k in $(seq $i $(( i + 17 ))); do
bg_256 $((16 + 36*j + k)); printf '%4s'
done
newline
done
done
# 24 grayscale colors
for i in 0 1; do
indent
for j in $(seq 0 11); do
bg_256 $((232 + 12*i + j)); printf '%6s'
done
newline
done
}
hex_theme() {
for i in 0 1; do
indent
for j in $(seq 0 7); do
[ $# -gt 0 ] && { bg_hex "$1"; printf '%4s'; shift; }
done
newline
done
}
gruvbox_dark() {
echo "RGB Gruvbox Dark:"
hex_theme \
'#282828' '#cc241d' '#98971a' '#d79921' '#458588' '#b16286' '#689d6a' '#a89984' \
'#928374' '#fb4934' '#b8bb26' '#fabd2f' '#84a598' '#d3869b' '#8ec07c' '#ebdbb2'
}
gruvbox_light() {
echo "RGB Gruvbox Light:"
hex_theme \
'#fbf1c7' '#cc241d' '#98971a' '#d79921' '#458588' '#b16286' '#689d6a' '#7c6f64' \
'#928374' '#9d0006' '#79740e' '#b57614' '#076678' '#8f3f71' '#427b58' '#3c3836'
}
solarized() {
echo "RGB Solarized:"
hex_theme \
'#073642' '#dc322f' '#859900' '#b58900' '#268bd2' '#d33682' '#2aa198' '#eee8d5' \
'#002b36' '#cb4b16' '#586e75' '#657b83' '#839496' '#6c71c4' '#93a1a1' '#fdf6e3'
}
nord() {
echo "Nord:"
hex_theme \
'#2e3440' '#bf616a' '#a3be8c' '#ebcb8b' '#5e81ac' '#b48ead' '#88c0d0' '#e5e9f0' \
'#434c5e' '#d08770' '#3b4252' '#4c566a' '#81a1c1' '#d8dee9' '#8fbcbb' '#eceff4'
}
main() {
indexed_16
echo
indexed_256
echo
gruvbox_dark
echo
gruvbox_light
echo
solarized
echo
nord
}
main