random script dump
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print256colours.sh 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # Tom Hale, 2016. MIT Licence.
  3. # Print out 256 colours, with each number printed in its corresponding colour
  4. # See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163
  5. set -eu # Fail on errors or undeclared variables
  6. printable_colours=256
  7. # Return a colour that contrasts with the given colour
  8. # Bash only does integer division, so keep it integral
  9. function contrast_colour {
  10. colour="$1"
  11. if (( colour < 16 )); then # Initial 16 ANSI colours
  12. (( colour == 0 || colour == 8 )) && printf "15" || printf "0"
  13. return
  14. fi
  15. # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
  16. if (( colour > 231 )); then # Greyscale ramp
  17. (( colour < 244 )) && printf "15" || printf "0"
  18. return
  19. fi
  20. # All other colours:
  21. # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
  22. # See http://stackoverflow.com/a/27165165/5353461
  23. # r=$(( (colour-16) / 36 ))
  24. g=$(( ((colour-16) % 36) / 6 ))
  25. # b=$(( (colour-16) % 6 ))
  26. # If luminance is bright, print number in black, white otherwise.
  27. # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601
  28. (( g > 2)) && printf "0" || printf "15"
  29. return
  30. # Uncomment the below for more precise luminance calculations
  31. # # Calculate percieved brightness
  32. # # See https://www.w3.org/TR/AERT#color-contrast
  33. # # and http://www.itu.int/rec/R-REC-BT.601
  34. # # Luminance is in range 0..5000 as each value is 0..5
  35. # luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
  36. # (( $luminance > 2500 )) && printf "0" || printf "15"
  37. }
  38. # Print a coloured block with the number of that colour
  39. function print_colour {
  40. local colour="$1" contrast
  41. contrast=$(contrast_colour "$1")
  42. printf "\\e[38;5;%s;48;5;%sm%3d\\e[0m " "$contrast" "$colour" "$colour"
  43. }
  44. # Starting at $1, print a run of $2 colours
  45. function print_run {
  46. local i
  47. for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
  48. print_colour "$i"
  49. done
  50. printf " "
  51. }
  52. # Print blocks of colours
  53. function print_blocks {
  54. local start="$1" i
  55. local end="$2" # inclusive
  56. local block_cols="$3"
  57. local block_rows="$4"
  58. local blocks_per_line="$5"
  59. local block_length=$((block_cols * block_rows))
  60. # Print sets of blocks
  61. for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do
  62. printf "\\n" # Space before each set of blocks
  63. # For each block row
  64. for (( row = 0; row < block_rows; row++ )) do
  65. # Print block columns for all blocks on the line
  66. for (( block = 0; block < blocks_per_line; block++ )) do
  67. print_run $(( i + (block * block_length) )) "$block_cols"
  68. done
  69. (( i += block_cols )) # Prepare to print the next row
  70. printf "\\n"
  71. done
  72. done
  73. }
  74. print_blocks 0 15 8 2 1 # The first 16 colours are spread over the whole spectrum
  75. printf "\\n"
  76. print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive
  77. print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey