palette.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from argparse import ArgumentParser
  2. from svgwrite.shapes import Rect
  3. import svgwrite
  4. theme_keys = [
  5. "foreground", "background", "background_opacity", "dynamic_background_opacity", "dim_opacity",
  6. "selection_foreground", "selection_background", "color0", "color8", "color1", "color9", "color2", "color10",
  7. "color3", "color11", "color4", "color12", "color5", "color13", "color6", "color14", "color7", "color15"
  8. ]
  9. def is_valid(line):
  10. """
  11. Returns true if a line inside a configuration file is a valid theme configuration pair: is not a comment, is not
  12. empty and the key is correct.
  13. :param line: a line inside the configuration file
  14. :type line: str
  15. :return: true if is valid, false otherwise
  16. :rtype: bool
  17. """
  18. return (not line.lstrip().startswith("#") # is not a comment
  19. and len(line.strip()) != 0 # is not empty
  20. and line.split(maxsplit=1)[0] in theme_keys) # key is a valid one
  21. def extract_configuration_pair(line):
  22. """
  23. Extract a configuration pair by splitting on spaces and taking the first couple of values.
  24. :param line: a line inside the configuration file
  25. :type line: str
  26. :return: a key-value pair
  27. :rtype: bool
  28. """
  29. split = line.split(maxsplit=2)
  30. return split[0], split[1]
  31. def read_configuration(filename):
  32. """
  33. Read a kitty configuration file and extract only theme related keys and values.
  34. :param filename: path to the configuration file
  35. :type filename: str
  36. :return: a map with theme related configuration values
  37. :rtype: dict[str, str]
  38. """
  39. with open(filename, "r") as fp:
  40. lines = fp.readlines()
  41. print(filename)
  42. theme_config = dict([extract_configuration_pair(line) for line in lines if is_valid(line)])
  43. return theme_config
  44. def draw_theme_palette(theme_configuration, start_point, size, displacement):
  45. rects = []
  46. for k, v in theme_configuration.items():
  47. rgb = tuple(int(v[i + 1:i + 3], 16) for i in (0, 2, 4))
  48. rects.append(Rect(start_point, size, fill=svgwrite.utils.rgb(rgb[0], rgb[1], rgb[2])))
  49. start_point = (start_point[0] + displacement[0], start_point[1] + displacement[1])
  50. return rects
  51. def draw_all_palettes(themes):
  52. dwg = svgwrite.Drawing('test.svg', profile='tiny')
  53. y = 0
  54. palettes = []
  55. for theme in themes:
  56. palettes += draw_theme_palette(theme, (0, y), (10, 10), (10, 0))
  57. y += 10
  58. for rect in palettes:
  59. dwg.add(rect)
  60. dwg.save()
  61. def main():
  62. parser = ArgumentParser()
  63. parser.add_argument("theme", type=str, nargs="+")
  64. ns = parser.parse_args()
  65. theme_configurations = [read_configuration(theme) for theme in ns.theme]
  66. draw_all_palettes(theme_configurations)
  67. if __name__ == "__main__":
  68. main()