Skip to content

Instantly share code, notes, and snippets.

@kwoods
Forked from pdarragh/Example usage
Created August 10, 2020 02:35
Show Gist options
  • Save kwoods/ac56acb3afd41ea23475a192c4a4cde1 to your computer and use it in GitHub Desktop.
Save kwoods/ac56acb3afd41ea23475a192c4a4cde1 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