我是腳本新手,并決定通過我給自己的這個家庭作業來學習。
這個腳本的目標很簡單:
- 以彩色顯示可用顏色
- 選擇一個數字來復制顏色方案的十六進制顏色值。
除 Black0~3 和 Gray0 外,所有顏色均正確顯示。
這 5 個選項以青色和綠色顯示。(請查看螢屏截圖)
嘗試谷歌搜索但無濟于事。任何幫助,將不勝感激。
提前致謝。
(我不知道這些資訊是否有幫助,但作業系統:Linux,終端仿真器: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 "d" "$(((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
這是一個截圖,它看起來像 截圖
uj5u.com熱心網友回復:
看看這個要點。
您可以將其呼叫為
colors --256

并檢查您想要的黑色和灰色值,然后讓您的映射函式回傳這些值。
uj5u.com熱心網友回復:
由于您使用的是支持真彩色的現代終端,??因此您可以直接使用十六進制顏色:
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 \$([^)]*)\)/${\1}/g)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474236.html
上一篇:bash中多個檔案的標準偏差
下一篇:Bash-檢查存盤庫是否存在
