aboutsummaryrefslogtreecommitdiff
path: root/pb_colour
blob: c7ee3214486c94d0a0e20b823ac33e423b6e016d (plain) (blame)
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
#!/usr/bin/env bash

#--- colour functions
# tested on 16/88/256/truecolor TERM definitions
# limited to 256 colours
# usage:
#   source ./pb
#   printf %b "$(pb_bg 196)$(pb_fg 254) RED $(pb_nc)\n"

pb_ctest() {

  if [ -z "$TERM" ] ; then
    return 1
  fi
  
  case $1 in '' | *[!0-9]* ) return 1;; esac

  local x
  x=$(tput colors)

  if [ -z "$1" ] || (("$1" >= "$x")) ; then
    return 1
  fi
  
  return 0
}

pb_terminfo() {
  local x
  x=$(tput colors)
  echo "TERM = $TERM : $x colours supported"	
}

pb_nc() {

    printf '\033[0;0m'
    return 0
    
}

pb_fg() {

  if pb_ctest $1; then
    printf %s "\033[38;5;$1m"
    return 0
  else
    return 1
  fi

}

pb_bg() {

  if pb_ctest $1; then
    printf %s "\033[48;5;$1m"
    return 0
  else
    return 1
  fi

}    

pb_ef() {

	case $1 in

	  "bold")
	    printf %s "\033[1m"
	    return 0
	    ;;

	  "dim")
	    printf %s "\033[2m"
	    return 0
	    ;;

	  "italic")
	    printf %s "\033[3m"
	    return 0
	    ;;
	    
	  "underline")
	    printf %s "\033[4m"
	    return 0
	    ;;

	  "blink")
	    printf %s "\033[5m"
	    return 0
	    ;;
	    
	  "reverse")
	    printf %s "\033[7m"
	    return 0
	    ;;

	  "hidden")
	    printf %s "\033[8m"
	    return 0
	    ;;
	    
	   "strikethrough")
	    printf %s "\033[9m"
	    return 0
	    ;;

	  *)
	    return 1
	    ;;
esac

}	

# end 256 colour functions