24-bit-color.sh 1.8 KB

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