|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This file echoes a bunch of 24-bit color codes |
| 4 | +# to the terminal to demonstrate its 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 |
| 9 | + |
| 10 | +setBackgroundColor() |
| 11 | +{ |
| 12 | + echo -en "\x1b[48;2;$1;$2;$3""m" |
| 13 | +} |
| 14 | + |
| 15 | +resetOutput() |
| 16 | +{ |
| 17 | + echo -en "\x1b[0m\n" |
| 18 | +} |
| 19 | + |
| 20 | +# Gives a color $1/255 % along HSV |
| 21 | +# Who knows what happens when $1 is outside 0-255 |
| 22 | +# Echoes "$red $green $blue" where |
| 23 | +# $red $green and $blue are integers |
| 24 | +# ranging between 0 and 255 inclusive |
| 25 | +rainbowColor() |
| 26 | +{ |
| 27 | + let h=$1/43 |
| 28 | + let f=$1-43*$h |
| 29 | + let t=$f*255/43 |
| 30 | + let q=255-t |
| 31 | + |
| 32 | + if [ $h -eq 0 ] |
| 33 | + then |
| 34 | + echo "255 $t 0" |
| 35 | + elif [ $h -eq 1 ] |
| 36 | + then |
| 37 | + echo "$q 255 0" |
| 38 | + elif [ $h -eq 2 ] |
| 39 | + then |
| 40 | + echo "0 255 $t" |
| 41 | + elif [ $h -eq 3 ] |
| 42 | + then |
| 43 | + echo "0 $q 255" |
| 44 | + elif [ $h -eq 4 ] |
| 45 | + then |
| 46 | + echo "$t 0 255" |
| 47 | + elif [ $h -eq 5 ] |
| 48 | + then |
| 49 | + echo "255 0 $q" |
| 50 | + else |
| 51 | + # execution should never reach here |
| 52 | + echo "0 0 0" |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +for i in `seq 0 127`; do |
| 57 | + setBackgroundColor $i 0 0 |
| 58 | + echo -en " " |
| 59 | +done |
| 60 | +resetOutput |
| 61 | +for i in `seq 255 128`; do |
| 62 | + setBackgroundColor $i 0 0 |
| 63 | + echo -en " " |
| 64 | +done |
| 65 | +resetOutput |
| 66 | + |
| 67 | +for i in `seq 0 127`; do |
| 68 | + setBackgroundColor 0 $i 0 |
| 69 | + echo -n " " |
| 70 | +done |
| 71 | +resetOutput |
| 72 | +for i in `seq 255 128`; do |
| 73 | + setBackgroundColor 0 $i 0 |
| 74 | + echo -n " " |
| 75 | +done |
| 76 | +resetOutput |
| 77 | + |
| 78 | +for i in `seq 0 127`; do |
| 79 | + setBackgroundColor 0 0 $i |
| 80 | + echo -n " " |
| 81 | +done |
| 82 | +resetOutput |
| 83 | +for i in `seq 255 128`; do |
| 84 | + setBackgroundColor 0 0 $i |
| 85 | + echo -n " " |
| 86 | +done |
| 87 | +resetOutput |
| 88 | + |
| 89 | +for i in `seq 0 127`; do |
| 90 | + setBackgroundColor `rainbowColor $i` |
| 91 | + echo -n " " |
| 92 | +done |
| 93 | +resetOutput |
| 94 | +for i in `seq 255 128`; do |
| 95 | + setBackgroundColor `rainbowColor $i` |
| 96 | + echo -n " " |
| 97 | +done |
| 98 | +resetOutput |
0 commit comments