-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow256color.sh
executable file
·156 lines (149 loc) · 4.93 KB
/
show256color.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/bash
# File Name: show256color.sh
# Author: philosophos<[email protected]>
# GitHub: https://github.com/philosophos/show256color
# Create Time: 2017 Feb 08
# Last modified: 2017 Feb 20
# show 256color in terminal
################################################################################
setColor()
{
#[[ $BF == "b" ]] && tput setab $i && tput setaf 0
#[[ $BF == "f" ]] && tput setaf $i && tput setab 0
if [ $BF == "b" ];then tput setab $i
[[ $i -eq 0 || $i -eq 16 || $i -ge 232 && $i -le 243 ]]\
&& tput setaf 15 || tput setaf 0
fi
if [ $BF == "f" ];then tput setaf $i
[[ $i -eq 0 || $i -eq 16 || $i -ge 232 && $i -le 243 ]]\
&& tput setab 15 || tput setab 0
fi
}
showColor()
{
setColor $BF $i
if [ $HEX == 0 ]&&[ $RGB == 0 ];then
printf ' %3d ' $i
elif [ $HEX == 0 ]&&[ $RGB == 1 ];then
printf ' %3d ' $i
elif [ $HEX == 1 ]&&[ $RGB == 0 ];then
printf ' %3d #%02x%02x%02x ' $i $R $G $B
else
printf ' %3d #%02x%02x%02x ' $i $R $G $B
fi
if [ $RGB == 1 ];then
printf "\e[0m\v\e[14D"
setColor $BF $i
printf " R%03dG%03dB%03d \e[A" $R $G $B
fi
tput sgr0
}
256color()
{
#color 0~15
#array_r=(00 80 00 80 00 80 00 c0 80 ff 00 ff 00 ff 00 ff)
#array_g=(00 00 80 80 00 00 80 c0 80 00 ff ff 00 00 ff ff)
#array_b=(00 00 00 00 80 80 80 c0 80 00 00 00 ff ff ff ff)
array_r=(000 128 000 128 000 128 000 192 128 255 000 255 000 255 000 255)
array_g=(000 000 128 128 000 000 128 192 128 000 255 255 000 000 255 255)
array_b=(000 000 000 000 128 128 128 192 128 000 000 000 255 255 255 255)
for i in {0..15};do
R=${array_r[i]}
G=${array_g[i]}
B=${array_b[i]}
showColor $BF $i $HEX $RGB
if [ $(((i+1)%8)) -eq 0 ];then
[[ $RGB -eq 0 ]]&& echo '' || echo -e '\n'
fi
done
#color 16~231
R=0;G=0;B=0;step=51
for blk in {0..2};do
for ((line=blk*12;line<blk*12+6;line++));do
for ((i=line*6+16;i<line*6+22;i++));do
showColor $BF $i $HEX $RGB;B=$((B+step))
done
B=0; R=$((R+step))
for ((i=line*6+52;i<line*6+58;i++));do
showColor $BF $i $HEX $RGB;B=$((B+step))
done
B=0; R=$((R-step));G=$((G+step))
[[ $RGB -eq 0 ]]&& echo '' || echo -e '\n'
done
G=0; R=$((R+step*2))
done
#color 232~255
R=8;G=8;B=8;step=10
for i in {232..255};do
showColor $BF $i $HEX $RGB
if [ $(((i-15)%12)) -eq 0 ];then
[[ $RGB -eq 0 ]]&& echo '' || echo -e '\n'
fi
R=$((R+step));G=$((G+step));B=$((B+step))
done
}
HELP="
The script show 256color in terminal\n
Helpful for configuring terminal program colorscheme, like vim,tmux,bash,zsh,etc.\n
Note that if color 0~15 was changed in terminal by configuration file or\n
CLI option, the corresponding hex & RGB value will be shown falsely.\n
Author: philosophos<[email protected]> \n
GitHub: https://github.com/philosophos/show256color \n
Usage:\n
\t./show256color [option]\n
without option,show 256color in background.\n
options:\n
\t[b|-b|bg|-bg|--background]\tshow 256color in background(default)\n
\t[f|-f|fg|-fg|--foregroung]\tshow 256color in foreground\n
\t[x|-x|hex|--hex]\t\talso show color value in hexadecimal\n
\t[r|-r|rgb|--rgb]\t\talso show color value in RGB\n
\t[h|-h|help|--help]\t\tshow the help\n
"
[[ $(tput colors)==256 ]]||\
echo -e "\e[33m The terminal does NOT support 256color :("
BF='b';HEX=0;RGB=0
if [ $# -gt 0 ];then
ARGS=`getopt -o "bfxrh" -l "background,foreground,bg,fg,hex,rgb,help"\
-n "show256color.sh" -- "$@"`
eval set -- "${ARGS}"
for opt in "$@"; do
case $opt in
b|-b|bg|--bg|--background)BF='b';shift;;
f|-f|fg|--fg|--foreground)BF='f';shift;;
x|-x|hex|--hex)HEX=1;shift;;
r|-r|rgb|--rgb)RGB=1;shift;;
h|-h|help|--help)echo -e $HELP;shift;exit;;
--)shift;;
*)echo -e "\e[33m Parameter ERROR!\e[0m For more details see
help.";exit 1;;
esac
done
fi
256color $BF $HEX $RGB
###############################################################################
#0~7 8~15
#from vim help cterm-colors
# NR-16 NR-8 COLOR NAME ~
# 0 0 Black
# 1 4 DarkBlue
# 2 2 DarkGreen
# 3 6 DarkCyan
# 4 1 DarkRed
# 5 5 DarkMagenta
# 6 3 Brown, DarkYellow
# 7 7 LightGray, LightGrey, Gray, Grey
# 8 0* DarkGray, DarkGrey
# 9 4* Blue, LightBlue
# 10 2* Green, LightGreen
# 11 6* Cyan, LightCyan
# 12 1* Red, LightRed
# 13 5* Magenta, LightMagenta
# 14 3* Yellow, LightYellow
# 15 7* White
#16~231
# 216 colors
# r,g,b=0~5
# $d=15+r*36+g*6+b
#232~255
# 24 grey levels,from black to white
#