Created
April 23, 2018 12:55
-
-
Save alfredodeza/a4150e688bd990b5b30ed38e9e5b6cbe to your computer and use it in GitHub Desktop.
read an itermcolors export file and spit out Vim 8's ansi color var
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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