Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created September 1, 2013 17:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/6405956 to your computer and use it in GitHub Desktop.
Save hhatto/6405956 to your computer and use it in GitHub Desktop.
convert from guifg/guibg to ctermfg/ctermbg for Vim Script
import sys
from grapefruit import Color
from fabulous.xterm256 import rgb_to_xterm
new_vim_color = []
def html2xterm256(color):
r, g, b = Color.HtmlToRgb(html_color)
r = int(r * 255)
g = int(g * 255)
b = int(b * 255)
return rgb_to_xterm(r, g, b)
for line in open(sys.argv[1]):
_tmp = line.split()
_tmp_line = []
for _t in _tmp:
if 'guifg=' in _t:
html_color = _t.split('guifg=')[1]
if html_color[0] != '#':
continue
c = html2xterm256(html_color)
_tmp_line.append("ctermfg=%d" % c)
if 'guibg=' in _t:
html_color = _t.split('guibg=')[1]
if html_color[0] != '#':
continue
c = html2xterm256(html_color)
_tmp_line.append("ctermbg=%d" % c)
if 'gui=' in _t:
_term = _t.split('gui=')[1]
_tmp_line.append("cterm=%s" % _term)
_tmp_line.append(_t)
new_vim_color.append(" ".join(_tmp_line))
print("\n".join(new_vim_color))
@97-109-107
Copy link

Thanks for this. And here's the other way round: https://gist.github.com/97-109-107/c111359c5792e14ab8e8

@LRDPRDX
Copy link

LRDPRDX commented Mar 27, 2020

html_color in html2xterm256 is undeclared

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