mktheme 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. precision=3 # digits after the '.'
  3. grabber="$(which grabc 2>/dev/null)"
  4. me="$(basename "$0")"
  5. precision=3
  6. outpath="$HOME/.config/xmountaincolors"
  7. function usage {
  8. echo " $1
  9. $me [theme name]
  10. interactively creates a theme for xmountaincolors by converting five 6-digit
  11. hex colors \"#nnnnnn\" (must use quotes), each color to 3 rgb values between
  12. 0 and 1, and outputs them to a file that is understood by xmountaincolors.
  13. grabc is currently supported as a color grabber.
  14. theme name is to be given on the command line; the rest is interactive.
  15. the theme is saved to $outpath/[theme name].conf,
  16. because that is where xmountaincolors is looking for them.
  17. "
  18. exit 1
  19. }
  20. function get_and_convert {
  21. # gets a hex color and outputs a string in the form of
  22. # { red = n.nnn; green = n.nnn; blue = n.nnn; }
  23. until [[ "$col" =~ ^#[0-9A-Fa-f]{6}$ ]]
  24. do
  25. read -n 1 x
  26. case $x in
  27. '')
  28. [[ "x$grabber" != "x" ]] && echo " grabbing... "; col="$($grabber 2>/dev/null)"
  29. ;;
  30. '#')
  31. read col
  32. col="#$col"
  33. ;;
  34. *)
  35. sleep 0.1
  36. echo
  37. ;;
  38. esac
  39. [[ "$col" =~ ^#[0-9A-Fa-f]{6}$ ]] || echo -n " Try again: "
  40. done
  41. red="$((16#${col:1:2}))"
  42. grn="$((16#${col:3:2}))"
  43. blu="$((16#${col:5:2}))"
  44. col=''
  45. echo -n "{ red = " >> "$file"
  46. printf "%${precision}.${precision}f " $(bc<<<"scale=$precision;$red/255") >> "$file"
  47. echo -n "; green = " >> "$file"
  48. printf "%${precision}.${precision}f " $(bc<<<"scale=$precision;$grn/255") >> "$file"
  49. echo -n "; blue = " >> "$file"
  50. printf "%${precision}.${precision}f" $(bc<<<"scale=$precision;$blu/255") >> "$file"
  51. #~ bc<<<"scale=$precision;x=$red/255;if(x>0 && x<1) print 0;print x"
  52. #~ echo -n "; green = "
  53. #~ bc<<<"scale=$precision;x=$grn/255;if(x>0 && x<1) print 0;print x"
  54. #~ echo -n "; blue = "
  55. #~ bc<<<"scale=$precision;x=$blu/255;if(x>0 && x<1) print 0;print x"
  56. echo -n "; }" >> "$file"
  57. }
  58. which bc >/dev/null 2>&1 || usage "command line calculator bc not found in \$PATH. Cannot proceed."
  59. [[ $# != 1 ]] && usage "Please provide a theme name."
  60. if [[ ! -d "$outpath" ]]; then
  61. read -n1 -p "
  62. Directory $outpath does not exist.
  63. Do you want to create it [y]? " x
  64. echo
  65. [[ "$x" == "y" ]] && mkdir -p "$outpath" || usage "You have chosen not to create $outpath.
  66. Cannot continue."
  67. fi
  68. file="$outpath/${1}.conf"
  69. [[ -e "$file" ]] && usage "File $file already exists.
  70. Remove file or choose a different name."
  71. touch "$file" || usage "Cannot open $file for writing."
  72. read -p "
  73. Theme name and/or comment: " name
  74. if [[ "x$name" != "x" ]]; then
  75. name="${name//\"/\\\"}"
  76. # i think doublequotes are the only characters libconfig chokes on, so escaping them here
  77. fi
  78. echo -n "name = \"$name\"
  79. colors = {
  80. base = ( " >> "$file"
  81. echo "
  82. 3 basecolors, from bottom (low altitudes) to top (high altitudes)
  83. "
  84. for altitude in low medium high; do
  85. echo -n " Enter base color for $altitude altitudes"
  86. [[ "x$grabber" != "x" ]] && echo -n ", or <Enter> to grab with $(basename "$grabber")"
  87. echo -n ": "
  88. get_and_convert
  89. [[ "$altitude" != "high" ]] && echo -n ", " >> "$file" || echo " );" >> "$file"
  90. done
  91. for color in black white sky sea; do
  92. echo -n "$color = ( " >> "$file"
  93. echo -n " Enter color for $color"
  94. [[ "x$grabber" != "x" ]] && echo -n ", or <Enter> to grab with $(basename "$grabber")"
  95. echo -n ": "
  96. get_and_convert
  97. echo " );" >> "$file"
  98. done
  99. echo " };" >> "$file"
  100. read -n1 -p " Do you want to see the config file $file now [y]? " x
  101. echo
  102. [[ "$x" == "y" ]] && cat "$file"