尝试通过 bash 脚本显示不同的颜色

Trying to display different colors through bash script

我是脚本编写的新手,并决定通过我给自己布置的作业来学习。
这个脚本的目标很简单:

除 Black0~3 和 Gray0 外,所有颜色都正确显示。
这 5 个选项显示为青色和绿色。 (请查看截图)
尝试谷歌搜索但无济于事。任何帮助将不胜感激。
提前致谢。
(我不知道这个信息是否有帮助,但是 OS: Linux, Terminal Emualtor: ST, Shell: zsh)

#!/bin/sh

# grab hex code
function fromhex() {
  hex=
  if [[ $hex == "#"* ]]; then
    hex=$(echo  | awk '{print substr([=11=],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

这是它的截图 screenshot

看看这个gist

您可以将其调用为

colors --256

并检查您想要的黑色和灰色值,然后将映射函数设置为 return。

由于您使用的是支持真彩色的现代终端,​​因此您可以直接使用十六进制颜色:

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"
}

并将 $(tput setaf $<color>) 替换为 ${<color>} (sed -E 's/$\(tput setaf $([^)]*)\)/${}/g)。