Skip to content

Instantly share code, notes, and snippets.

@pdarragh
Created April 28, 2020 03:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pdarragh/14549bbf4d1a2391b8672bcdf93ced8f to your computer and use it in GitHub Desktop.
Save pdarragh/14549bbf4d1a2391b8672bcdf93ced8f to your computer and use it in GitHub Desktop.
A simple script to extract colors from iTerm color profiles as hexadecimal values.
$ ./iterm2hex.py "Solarized Dark Higher Contrast.itermcolors"
#002731 // Ansi 0 Color
#D01B24 // Ansi 1 Color
#50EE84 // Ansi 10 Color
#B17E28 // Ansi 11 Color
#178DC7 // Ansi 12 Color
#E14D8E // Ansi 13 Color
#00B29E // Ansi 14 Color
#FCF4DC // Ansi 15 Color
#6BBE6C // Ansi 2 Color
#A57705 // Ansi 3 Color
#2075C7 // Ansi 4 Color
#C61B6E // Ansi 5 Color
#259185 // Ansi 6 Color
#E9E2CB // Ansi 7 Color
#006388 // Ansi 8 Color
#F4153B // Ansi 9 Color
#001E26 // Background Color
#B4D4D2 // Bold Color
#F34A00 // Cursor Color
#002731 // Cursor Text Color
#9BC1C2 // Foreground Color
#798F8E // Selected Text Color
#003747 // Selection Color
#!/usr/bin/env python3
#
# Convert .itermcolors plist files to hex colors for html.
#
# This file was originally based on the implementation here:
# https://gist.github.com/MSylvia/4e90860743f1a4de187d
# and then modified to use Python 3 and the `plistlib` library.
from pathlib import Path
from plistlib import load as load_plist
from typing import Dict, Iterator, Tuple
BLUE = "Blue Component"
GREEN = "Green Component"
RED = "Red Component"
def dict_from_plist_file(plistfile: Path) -> Dict:
with open(plistfile, 'rb') as pf:
return load_plist(pf)
def rgb_to_hex(r: int, g: int , b: int ) -> str:
return f"#{r:02X}{g:02X}{b:02X}"
def component_to_int(c: str) -> int:
return int(float(c) * 255.0)
def key_color_pairs(plistfile: Path) -> Iterator[Tuple[str, str]]:
for key, components in dict_from_plist_file(plistfile).items():
r = component_to_int(components[RED])
g = component_to_int(components[GREEN])
b = component_to_int(components[BLUE])
yield (key, rgb_to_hex(r, g, b))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('plistfile', type=Path)
args = parser.parse_args()
for key, color in key_color_pairs(args.plistfile):
print(f"{color} // {key}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment