Skip to content

Instantly share code, notes, and snippets.

@markwu
Forked from atav32/convert_itermcolors.py
Last active February 16, 2021 16:51
Show Gist options
  • Save markwu/3c59ba6fb02ee565b923bbd3c676cd9b to your computer and use it in GitHub Desktop.
Save markwu/3c59ba6fb02ee565b923bbd3c676cd9b to your computer and use it in GitHub Desktop.
Convert .itermcolors file to html hex & rgb
#!/usr/bin/env python
#
# Convert .itermcolors files to hex colors for html
import sys
import xml.etree.ElementTree as ET
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
# MAIN
def main():
if len(sys.argv) < 2:
print("usage: ./convert_itermcolors.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)):
components = ["Red Component", "Green Component", "Blue Component"]
colours = {}
for x in range(len(dicts[i])):
component = dicts[i][x].text
if component in components:
value = dicts[i][x + 1].text
colours[component] = value
r = int(float(colours["Red Component"]) * 255.0)
g = int(float(colours["Green Component"]) * 255.0)
b = int(float(colours["Blue Component"]) * 255.0)
print(rgb_to_hex((r, g, b)) + " rgb({}, {}, {})".format(r, g, b) + " //" + keys[i].text)
if __name__ == '__main__':
main()
$> ./convert_itermcolors.py seoul256.itermcolors
#4e4e4e rgb(78, 78, 78) //Ansi 0 Color
#d68787 rgb(214, 135, 135) //Ansi 1 Color
#5f865f rgb(95, 134, 95) //Ansi 2 Color
#d8af5f rgb(216, 175, 95) //Ansi 3 Color
#85add4 rgb(133, 173, 212) //Ansi 4 Color
#d7afaf rgb(215, 175, 175) //Ansi 5 Color
#87afaf rgb(135, 175, 175) //Ansi 6 Color
#d0d0d0 rgb(208, 208, 208) //Ansi 7 Color
#626262 rgb(98, 98, 98) //Ansi 8 Color
#d75f87 rgb(215, 95, 135) //Ansi 9 Color
#87af87 rgb(135, 175, 135) //Ansi 10 Color
#ffd787 rgb(255, 215, 135) //Ansi 11 Color
#add4fb rgb(173, 212, 251) //Ansi 12 Color
#ffafaf rgb(255, 175, 175) //Ansi 13 Color
#87d7d7 rgb(135, 215, 215) //Ansi 14 Color
#e4e4e4 rgb(228, 228, 228) //Ansi 15 Color
#d0d0d0 rgb(208, 208, 208) //Foreground Color
#3a3a3a rgb(58, 58, 58) //Background Color
#e4e4e4 rgb(228, 228, 228) //Bold Color
#d0d0d0 rgb(208, 208, 208) //Cursor Color
#3a3a3a rgb(58, 58, 58) //Cursor Text Color
#d0d0d0 rgb(208, 208, 208) //Selected Text Color
#005f5f rgb(0, 95, 95) //Selection Color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment