'Trying to display different colors through bash script
I'm new to scripting and have decided to learn with this homework that I gave myself.
The goal of this script is simply:
- display available colors in color
- pick a number to copy the hex color value of a colorscheme.
All the colors are displaying correctly except Black0~3 and Gray0.
Those 5 choices display in cyan and green instead. (please check screenshot)
Tried googling but to no avail. Any help would be appreciated.
Thanks in advance.
(I don't know if this info will be any helpful but OS: Linux, Terminal Emualtor: ST, Shell: zsh)
#!/bin/sh
# grab hex code
function fromhex() {
hex=$1
if [[ $hex == "#"* ]]; then
hex=$(echo $1 | awk '{print substr($0,2)}')
fi
r=$(printf '0x%0.2s' "$hex")
g=$(printf '0x%0.2s' ${hex#??})
b=$(printf '0x%0.2s' ${hex#????})
echo -e `printf "%03d" "$(((r<75?0:(r-35)/40)*6*6+(g<75?0:(g-35)/40)*6+(b<75?0:(b-35)/40)+16))"`
}
##################################################
# color values
Flamingo=$(fromhex "#F2CDCD")
Mauve=$(fromhex "#DDB6F2")
Pink=$(fromhex "#F5C2E7")
Maroon=$(fromhex "#E8A2AF")
Red=$(fromhex "#F28FAD")
Peach=$(fromhex "#F8BD96")
Yellow=$(fromhex "#FAE3B0")
Green=$(fromhex "#ABE9B3")
Teal=$(fromhex "#B5E8E0")
Blue=$(fromhex "#96CDFB")
Sky=$(fromhex "#89DCEB")
Black0=$(fromhex "#161320")
Black1=$(fromhex "#1A1826")
Black2=$(fromhex "#1E1E2E")
Black3=$(fromhex "#302D41")
Black4=$(fromhex "#575268")
Gray0=$(fromhex "#6E6C7E")
Gray1=$(fromhex "#988BA2")
Gray2=$(fromhex "#C3BAC6")
White=$(fromhex "#D9E0EE")
Lavender=$(fromhex "#C9CBFF")
Rosewater=$(fromhex "#F5E0DC")
##################################################
# display, select, copy hex color codes
echo -e "\nCatppuccin color selector"
echo
echo "[01] $(tput setaf $Flamingo)Flamingo #F2CDCD$(tput sgr0) [12] $(tput setaf $Black0)Black0 #161320$(tput sgr0)"
echo "[02] $(tput setaf $Mauve)Mauve #DDB6F2 $(tput sgr0) [13] $(tput setaf $Black1)Black1 #1A1826$(tput sgr0)"
echo "[03] $(tput setaf $Pink)Pink #F5C2E7$(tput sgr0) [14] $(tput setaf $Black2)Black2 #1E1E2E$(tput sgr0)"
echo "[04] $(tput setaf $Maroon)Maroon #E8A2AF$(tput sgr0) [15] $(tput setaf $Black3)Black3 #302D41$(tput sgr0)"
echo "[05] $(tput setaf $Red)Red #F28FAD $(tput sgr0) [16] $(tput setaf $Black4)Black4 #575268$(tput sgr0)"
echo "[06] $(tput setaf $Peach)Peach #F8BD96$(tput sgr0) [17] $(tput setaf $Gray0)Gray0 #6E6C7E$(tput sgr0)"
echo "[07] $(tput setaf $Yellow)Yellow #FAE3B0 $(tput sgr0) [18] $(tput setaf $Gray1)Gray1 #988BA2$(tput sgr0)"
echo "[08] $(tput setaf $Green)Green #ABE9B3$(tput sgr0) [019 $(tput setaf $Gray2)Gray2 #C3BAC6$(tput sgr0)"
echo "[09] $(tput setaf $Teal)Teal #B5E8E0$(tput sgr0) [20] $(tput setaf $White)White #D9E0EE$(tput sgr0)"
echo "[10] $(tput setaf $Blue)Blue #96CDFB$(tput sgr0) [21] $(tput setaf $Lavender)Lavender #C9CBFF$(tput sgr0)"
echo "[11] $(tput setaf $Sky)Sky #89DCEB$(tput sgr0) [22] $(tput setaf $Rosewater)Rosewater #F5E0DC$(tput sgr0)"
echo
read -p "Pick number to copy hex code to clipboard [1~22]: " sel
if [ $sel = 1 ] ; then
echo "Copied $(tput setaf $Flamingo)Flamingo$(tput sgr0) to clipboard"
echo "#F2CDCD" | xclip -sel clip
elif [ $sel = 2 ] ; then
echo "Copied $(tput setaf $Mauve)Mauve$(tput sgr0) to clipboard"
echo "#DDB6F2" | xclip -sel clip
elif [ $sel = 3 ] ; then
echo "Copied $(tput setaf $Pink)Pink$(tput sgr0) to clipboard"
echo "#F5C2E7" | xclip -sel clip
elif [ $sel = 4 ] ; then
echo "Copied $(tput setaf $Maroon)Maroon$(tput sgr0) to clipboard"
echo "#E8A2AF" | xclip -sel clip
elif [ $sel = 5 ] ; then
echo "Copied $(tput setaf $Red)Red$(tput sgr0) to clipboard"
echo "#F28FAD" | xclip -sel clip
elif [ $sel = 6 ] ; then
echo "Copied $(tput setaf $Peach)Peach$(tput sgr0) to clipboard"
echo "#F8BD96" | xclip -sel clip
elif [ $sel = 7 ] ; then
echo "Copied $(tput setaf $Yellow)Yellow$(tput sgr0) to clipboard"
echo "#FAE3B0" | xclip -sel clip
elif [ $sel = 8 ] ; then
echo "Copied $(tput setaf $Green)Green$(tput sgr0) to clipboard"
echo "#ABE9B3" | xclip -sel clip
elif [ $sel = 9 ] ; then
echo "Copied $(tput setaf $Teal)Teal$(tput sgr0) to clipboard"
echo "#B5E8E0" | xclip -sel clip
elif [ $sel = 10 ] ; then
echo "Copied $(tput setaf $Blue)Blue$(tput sgr0) to clipboard"
echo "#96CDFB" | xclip -sel clip
elif [ $sel = 11 ] ; then
echo "Copied $(tput setaf $Sky)Sky$(tput sgr0) to clipboard"
echo "#89DCEB" | xclip -sel clip
elif [ $sel = 12 ] ; then
echo "Copied $(tput setaf $Black0)Black 0$(tput sgr0) to clipboard"
echo "#161320" | xclip -sel clip
elif [ $sel = 13 ] ; then
echo "Copied $(tput setaf $Black1)Black 1$(tput sgr0) to clipboard"
echo "#1A1826" | xclip -sel clip
elif [ $sel = 14 ] ; then
echo "Copied $(tput setaf $Black2)Black 2$(tput sgr0) to clipboard"
echo "#1E1E2E" | xclip -sel clip
elif [ $sel = 15 ] ; then
echo "Copied $(tput setaf $Black3)Black 3$(tput sgr0) to clipboard"
echo "#302D41" | xclip -sel clip
elif [ $sel = 16 ] ; then
echo "Copied $(tput setaf $Black4)Black 4$(tput sgr0) to clipboard"
echo "#575268" | xclip -sel clip
elif [ $sel = 17 ] ; then
echo "Copied $(tput setaf $Gray0)Gray 0$(tput sgr0) to clipboard"
echo "#6E6C7E" | xclip -sel clip
elif [ $sel = 18 ] ; then
echo "Copied $(tput setaf $Gray1)Gray 1$(tput sgr0) to clipboard"
echo "#988BA2" | xclip -sel clip
elif [ $sel = 19 ] ; then
echo "Copied $(tput setaf $Gray2)Gray 2$(tput sgr0) to clipboard"
echo "#C3BAC6" | xclip -sel clip
elif [ $sel = 20 ] ; then
echo "Copied $(tput setaf $White)White$(tput sgr0) to clipboard"
echo "#D9E0EE" | xclip -sel clip
elif [ $sel = 21 ] ; then
echo "Copied $(tput setaf $Lavender)Lavender$(tput sgr0) to clipboard"
echo "#C9CBFF" | xclip -sel clip
elif [ $sel = 22 ] ; then
echo "Copied $(tput setaf $Rosewater)Rosewater$(tput sgr0) to clipboard"
echo "#F5E0DC" | xclip -sel clip
else
echo "invalid choice"
fi
Here's a screenshot of what it looks like screenshot
Solution 1:[1]
Take a look at this gist.
You can invoke it as
colors --256
and check the values you want for black and gray and then make your mapping function return those.
Solution 2:[2]
Since you're using a modern terminal with truecolor support, you can use the hex colors directly:
function fromhex() {
hex=${1#\#}
r="0x${hex:0:2}"
g="0x${hex:2:2}"
b="0x${hex:4:2}"
printf '\x1b[38;2;%d;%d;%dm' "$r" "$g" "$b"
}
and replace $(tput setaf $<color>)
with ${<color>}
(sed -E 's/\$\(tput setaf \$([^)]*)\)/${\1}/g
).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Diego Torres Milano |
Solution 2 | yut23 |