Skip to content

Instantly share code, notes, and snippets.

@ntk148v
Forked from alfredodeza/iterm-to-hex.py
Created April 8, 2022 07:35
Show Gist options
  • Save ntk148v/8f6d6dbcd9986746301a616e46742f26 to your computer and use it in GitHub Desktop.
Save ntk148v/8f6d6dbcd9986746301a616e46742f26 to your computer and use it in GitHub Desktop.
read an itermcolors export file and spit out Vim 8's ansi color var
#!/usr/bin/env python
#
# Convert .itermcolors files to hex colors
import sys
import xml.etree.ElementTree as ET
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def main():
mapping = {}
if len(sys.argv) < 2:
print("usage: ./iterm-to-hex.py file.itermcolors")
exit()
tree = ET.parse(sys.argv[1])
root = tree.getroot()
keys = root.findall("./dict/key")
dicts = root.findall("./dict/dict")
for i in range(len(keys)):
for count, child in enumerate(dicts[i]):
if "Blue" in child.text:
b = int(float(dicts[i][count+1].text) * 255.0)
if "Green" in child.text:
g = int(float(dicts[i][count+1].text) * 255.0)
if "Red" in child.text:
r = int(float(dicts[i][count+1].text) * 255.0)
mapping[keys[i].text.split()[1].strip()] = rgb_to_hex((r, g, b))
print(rgb_to_hex((r, g, b)) + " rgb({}, {}, {})".format(r, g, b) + " //" + keys[i].text)
vim_var = "let g:terminal_ansi_colors = ["
for index in range(0, 16):
if index == 15:
vim_var += '"%s"]' % mapping[str(index)]
else:
vim_var += '"%s",' % mapping[str(index)]
print vim_var
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment