Advertisement
Guest User

Untitled

a guest
Apr 5th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.28 KB | None | 0 0
  1. #!/bin/bash
  2. # convert a Gimp colour palette (.gpl) to a list of CSS variables
  3. # example:
  4. #   ./gpl-to-css.sh my-palette.gpl > my-palette.css
  5. # converts
  6. # ----- my-palette.gpl -----
  7. # GIMP Palette
  8. # Name: My Palette
  9. # Columns: 3
  10. # 255 0 0 Red
  11. # 0 255 0 Green
  12. # 0 0 255 Blue
  13. # ---------------------------
  14. # to
  15. # ----- my-palette.css -----
  16. # :root {
  17. #   --colour-Red: #ff0000;
  18. #   --colour-Green: #00ff00;
  19. #   --colour-Blue: #0000ff;
  20. # }
  21. # ---------------------------
  22. # only supports 3-column colours
  23. # you could edit the lines below to enable 4 colours by:
  24. # - add another match to the regex
  25. # - shift the indexes of the column items to add a fourth alpha column
  26. # - add an A colour to the print statement
  27.  
  28. regex='^Columns: ([0-9]+)$'
  29. if [[ $(grep -E "$regex" $1) =~ $regex ]];
  30. then
  31.   columns=${BASH_REMATCH[1]}
  32. else
  33.   echo "Error: could not find columns in $1" >&2
  34.   exit 1
  35. fi
  36.  
  37. if [[ $columns -ne 3 ]];
  38. then
  39.   echo "Error: only 3-column palettes are supported" >&2
  40.   exit 1
  41. fi
  42.  
  43. cat $1 | awk -F ' ' '
  44. BEGIN {
  45.  printf ":root {\n"
  46. } /^([0-9]{1,3})\s+([0-9]{1,3})\s+([0-9]{1,3})\s+(.*)$/ {
  47.  colourname = ""
  48.  for (i=4;i<=NF;i++) {
  49.    colourname = colourname "-" $i
  50.  }
  51.  printf "  --colour%s: #%02x%02x%02x;\n", colourname, $1, $2, $3
  52. } END {
  53.  printf "}\n"
  54. }'
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement