Merge branch 'obsd-master'
[tmux.git] / tools / 24-bit-color.sh
blob3e91da20bb55192e7585ef50c61bd4ae4548698f
1 #!/bin/bash
3 # This file echoes four gradients with 24-bit color codes
4 # to the terminal to demonstrate their functionality.
5 # The foreground escape sequence is ^[38;2;<r>;<g>;<b>m
6 # The background escape sequence is ^[48;2;<r>;<g>;<b>m
7 # <r> <g> <b> range from 0 to 255 inclusive.
8 # The escape sequence ^[0m returns output to default
11 # From
12 # https://github.com/gnachman/iTerm2/blob/master/tests/24-bit-color.sh
13 # and presumably covered by
14 # https://github.com/gnachman/iTerm2/blob/master/LICENSE
17 SEQ1=
18 if which gseq >/dev/null 2>&1; then
19 SEQ1=gseq
20 elif seq --version|grep -q GNU; then
21 SEQ1=seq
23 if [ -n "$SEQ1" ]; then
24 # GNU seq requires a -ve increment if going backwards
25 seq1()
27 if [ $1 -gt $2 ]; then
28 $SEQ1 $1 -1 $2
29 else
30 $SEQ1 $1 $2
33 SEQ=seq1
34 else
35 SEQ=seq
36 fi
37 SEPARATOR=':'
39 setBackgroundColor()
41 echo -en "\033[48${SEPARATOR}2${SEPARATOR}$1${SEPARATOR}$2${SEPARATOR}$3""m"
44 resetOutput()
46 echo -en "\033[0m\n"
49 # Gives a color $1/255 % along HSV
50 # Who knows what happens when $1 is outside 0-255
51 # Echoes "$red $green $blue" where
52 # $red $green and $blue are integers
53 # ranging between 0 and 255 inclusive
54 rainbowColor()
56 let h=$1/43
57 let f=$1-43*$h
58 let t=$f*255/43
59 let q=255-t
61 if [ $h -eq 0 ]
62 then
63 echo "255 $t 0"
64 elif [ $h -eq 1 ]
65 then
66 echo "$q 255 0"
67 elif [ $h -eq 2 ]
68 then
69 echo "0 255 $t"
70 elif [ $h -eq 3 ]
71 then
72 echo "0 $q 255"
73 elif [ $h -eq 4 ]
74 then
75 echo "$t 0 255"
76 elif [ $h -eq 5 ]
77 then
78 echo "255 0 $q"
79 else
80 # execution should never reach here
81 echo "0 0 0"
85 for i in `$SEQ 0 127`; do
86 setBackgroundColor $i 0 0
87 echo -en " "
88 done
89 resetOutput
90 for i in `$SEQ 255 128`; do
91 setBackgroundColor $i 0 0
92 echo -en " "
93 done
94 resetOutput
96 for i in `$SEQ 0 127`; do
97 setBackgroundColor 0 $i 0
98 echo -n " "
99 done
100 resetOutput
101 for i in `$SEQ 255 128`; do
102 setBackgroundColor 0 $i 0
103 echo -n " "
104 done
105 resetOutput
107 for i in `$SEQ 0 127`; do
108 setBackgroundColor 0 0 $i
109 echo -n " "
110 done
111 resetOutput
112 for i in `$SEQ 255 128`; do
113 setBackgroundColor 0 0 $i
114 echo -n " "
115 done
116 resetOutput
118 for i in `$SEQ 0 127`; do
119 setBackgroundColor `rainbowColor $i`
120 echo -n " "
121 done
122 resetOutput
123 for i in `$SEQ 255 128`; do
124 setBackgroundColor `rainbowColor $i`
125 echo -n " "
126 done
127 resetOutput