show256color.sh 5.0 KB

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