Skip to content

Instantly share code, notes, and snippets.

@atav32
Forked from MSylvia/convert_itermcolors.py
Last active February 18, 2024 01:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save atav32/7c1cfe3e06bd4c87349930995b61c011 to your computer and use it in GitHub Desktop.
Save atav32/7c1cfe3e06bd4c87349930995b61c011 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)):
r = int(float(dicts[i][1].text) * 255.0)
g = int(float(dicts[i][3].text) * 255.0)
b = int(float(dicts[i][5].text) * 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
@atav32
Copy link
Author

atav32 commented Feb 26, 2018

@atav32
Copy link
Author

atav32 commented Feb 26, 2018

Seoul256 iTerm theme: beautiful

@jhrr
Copy link

jhrr commented Jan 27, 2021

Thanks! However, it seems the order of components in dicts is non-deterministic so I made the following changes to remove hardcoded indexing and get things more flexible.

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)

@junhan-z
Copy link

junhan-z commented Aug 3, 2021

The conversion logic is correct.

However, with the number shown in iterm, it is not consistent, for example, Ansi 0 is actually 0x606060 in iTerm preference. Not sure how does iTerm converts 0-1 float to RGB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment