Skip to content

Commit

Permalink
convert to 256color
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Oct 13, 2018
1 parent af73657 commit 68ac082
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A Vim colorscheme based on Github's syntax highlighting as of 2018.

I wasn't happy with [endel/vim-github-colorscheme](https://github.com/endel/vim-github-colorscheme), as it was out of date.

I haven't tried it non-gui mode, so contributions are welcome on that front. The infrastructure is there, you just have to choose cterms for the color map.
It degrades gracefully in non-gui mode (ie ctermbg/ctermfg/256colors).

## Install

Expand Down
54 changes: 27 additions & 27 deletions colors/github.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ endfunction

let s:colors = {}

let s:colors.base0 = { 'gui': '#24292e', 'cterm': 0 } " github text fg
let s:colors.base1 = { 'gui': '#41484f', 'cterm': 61 } " lightened from 0
let s:colors.base2 = { 'gui': '#6a737d', 'cterm': 246 } " comment
let s:colors.base3 = { 'gui': '#76787b', 'cterm': 246 } " github linenr darker
let s:colors.base0 = { 'gui': '#24292e', 'cterm': 235 } " github text fg
let s:colors.base1 = { 'gui': '#41484f', 'cterm': 238 } " lightened from 0
let s:colors.base2 = { 'gui': '#6a737d', 'cterm': 243 } " comment
let s:colors.base3 = { 'gui': '#76787b', 'cterm': 243 } " github linenr darker
let s:colors.base_35 = { 'gui': '#979797', 'cterm': 246 } " github linenr darker
let s:colors.base4 = { 'gui': '#babbbc', 'cterm': 246 } " github linenr lighter
let s:colors.base5 = { 'gui': '#dddddd', 'cterm': 246 } "
let s:colors.base6 = { 'gui': '#ebeced', 'cterm': 246 } "
let s:colors.base7 = { 'gui': '#f6f8fa', 'cterm': 255 } " github inline code block bg
let s:colors.base8 = { 'gui': '#fafbfc', 'cterm': 246 } " github generic light
let s:colors.white = { 'gui': '#ffffff', 'cterm': 255 } "
let s:colors.base4 = { 'gui': '#babbbc', 'cterm': 250 } " github linenr lighter
let s:colors.base5 = { 'gui': '#dddddd', 'cterm': 253 } "
let s:colors.base6 = { 'gui': '#ebeced', 'cterm': 255 } "
let s:colors.base7 = { 'gui': '#f6f8fa', 'cterm': 231 } " github inline code block bg
let s:colors.base8 = { 'gui': '#fafbfc', 'cterm': 231 } " github generic light
let s:colors.white = { 'gui': '#ffffff', 'cterm': 231 } "

if g:github_colors_soft == 1
let s:colors.bg = s:colors.base7
Expand All @@ -108,23 +108,23 @@ else
let s:colors.gutterfg = s:colors.base4
endif

let s:colors.red = { 'gui': '#d73a49', 'cterm': 1 } " github syntax
let s:colors.darkred = { 'gui': '#b31d28', 'cterm': 1 } " github syntax
let s:colors.orange = { 'gui': '#e36209', 'cterm': 9 } " github syntax
let s:colors.purple = { 'gui': '#6f42c1', 'cterm': 5 } " github syntax
let s:colors.darkpurple = { 'gui': '#352650', 'cterm': 5 } " ^- darkened
let s:colors.blue = { 'gui': '#005cc5', 'cterm': 4 } " github syntax
let s:colors.darkblue = { 'gui': '#032f62', 'cterm': 2 } " ^- darkened
let s:colors.yellow = { 'gui': '#ffffc5', 'cterm': 228 } " github search
let s:colors.green = { 'gui': '#22863a', 'cterm': 2 } " github syntax (html)
let s:colors.lightgreen = { 'gui': '#e6ffed', 'cterm': 123 } " github diff
let s:colors.lightred = { 'gui': '#ffeef0', 'cterm': 123 } " github diff
let s:colors.lightorange_nr = { 'gui': '#fff5b1', 'cterm': 123 } " github selected line number column
let s:colors.lightorange = { 'gui': '#fffbdd', 'cterm': 123 } " github selected line
let s:colors.difftext = { 'gui': '#f2e496', 'cterm': 123 } " ^- darkened
let s:colors.lightblue = { 'gui': '#f1f8ff', 'cterm': 123 } " github diff folds
let s:colors.visualblue = { 'gui': '#e4effb', 'cterm': 123 } " ^- darkened
let s:colors.medblue = { 'gui': '#b2ceec', 'cterm': 123 } " ^- darkened
let s:colors.red = { 'gui': '#d73a49', 'cterm': 167 } " github syntax
let s:colors.darkred = { 'gui': '#b31d28', 'cterm': 124 } " github syntax
let s:colors.orange = { 'gui': '#e36209', 'cterm': 166 } " github syntax
let s:colors.purple = { 'gui': '#6f42c1', 'cterm': 61 } " github syntax
let s:colors.darkpurple = { 'gui': '#352650', 'cterm': 237 } " ^- darkened
let s:colors.blue = { 'gui': '#005cc5', 'cterm': 26 } " github syntax
let s:colors.darkblue = { 'gui': '#032f62', 'cterm': 17 } " ^- darkened
let s:colors.yellow = { 'gui': '#ffffc5', 'cterm': 230 } " github search
let s:colors.green = { 'gui': '#22863a', 'cterm': 29 } " github syntax (html)
let s:colors.lightgreen = { 'gui': '#e6ffed', 'cterm': 85 } " github diff
let s:colors.lightred = { 'gui': '#ffeef0', 'cterm': 167 } " github diff
let s:colors.lightorange_nr = { 'gui': '#fff5b1', 'cterm': 229 } " github selected line number column
let s:colors.lightorange = { 'gui': '#fffbdd', 'cterm': 230 } " github selected line
let s:colors.difftext = { 'gui': '#f2e496', 'cterm': 222 } " ^- darkened
let s:colors.lightblue = { 'gui': '#f1f8ff', 'cterm': 231 } " github diff folds
let s:colors.visualblue = { 'gui': '#e4effb', 'cterm': 255 } " ^- darkened
let s:colors.medblue = { 'gui': '#b2ceec', 'cterm': 153 } " ^- darkened

" to link to
call s:Col('ghGreen', 'green')
Expand Down
100 changes: 100 additions & 0 deletions convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# https://stackoverflow.com/questions/11765623/convert-hex-to-closest-x11-color-number
import re
import math

colors = {
'000000': '16', '00005f': '17', '000087': '18', '0000af': '19', '0000d7': '20',
'0000ff': '21', '005f00': '22', '005f5f': '23', '005f87': '24', '005faf': '25',
'005fd7': '26', '005fff': '27', '008700': '28', '00875f': '29', '008787': '30',
'0087af': '31', '0087d7': '32', '0087ff': '33', '00af00': '34', '00af5f': '35',
'00af87': '36', '00afaf': '37', '00afd7': '38', '00afff': '39', '00d700': '40',
'00d75f': '41', '00d787': '42', '00d7af': '43', '00d7d7': '44', '00d7ff': '45',
'00ff00': '46', '00ff5f': '47', '00ff87': '48', '00ffaf': '49', '00ffd7': '50',
'00ffff': '51', '5f0000': '52', '5f005f': '53', '5f0087': '54', '5f00af': '55',
'5f00d7': '56', '5f00ff': '57', '5f5f00': '58', '5f5f5f': '59', '5f5f87': '60',
'5f5faf': '61', '5f5fd7': '62', '5f5fff': '63', '5f8700': '64', '5f875f': '65',
'5f8787': '66', '5f87af': '67', '5f87d7': '68', '5f87ff': '69', '5faf00': '70',
'5faf5f': '71', '5faf87': '72', '5fafaf': '73', '5fafd7': '74', '5fafff': '75',
'5fd700': '76', '5fd75f': '77', '5fd787': '78', '5fd7af': '79', '5fd7d7': '80',
'5fd7ff': '81', '5fff00': '82', '5fff5f': '83', '5fff87': '84', '5fffaf': '85',
'5fffd7': '86', '5fffff': '87', '870000': '88', '87005f': '89', '870087': '90',
'8700af': '91', '8700d7': '92', '8700ff': '93', '875f00': '94', '875f5f': '95',
'875f87': '96', '875faf': '97', '875fd7': '98', '875fff': '99', '878700': '100',
'87875f': '101', '878787': '102', '8787af': '103', '8787d7': '104', '8787ff': '105',
'87af00': '106', '87af5f': '107', '87af87': '108', '87afaf': '109', '87afd7': '110',
'87afff': '111', '87d700': '112', '87d75f': '113', '87d787': '114', '87d7af': '115',
'87d7d7': '116', '87d7ff': '117', '87ff00': '118', '87ff5f': '119', '87ff87': '120',
'87ffaf': '121', '87ffd7': '122', '87ffff': '123', 'af0000': '124', 'af005f': '125',
'af0087': '126', 'af00af': '127', 'af00d7': '128', 'af00ff': '129', 'af5f00': '130',
'af5f5f': '131', 'af5f87': '132', 'af5faf': '133', 'af5fd7': '134', 'af5fff': '135',
'af8700': '136', 'af875f': '137', 'af8787': '138', 'af87af': '139', 'af87d7': '140',
'af87ff': '141', 'afaf00': '142', 'afaf5f': '143', 'afaf87': '144', 'afafaf': '145',
'afafd7': '146', 'afafff': '147', 'afd700': '148', 'afd75f': '149', 'afd787': '150',
'afd7af': '151', 'afd7d7': '152', 'afd7ff': '153', 'afff00': '154', 'afff5f': '155',
'afff87': '156', 'afffaf': '157', 'afffd7': '158', 'afffff': '159', 'd70000': '160',
'd7005f': '161', 'd70087': '162', 'd700af': '163', 'd700d7': '164', 'd700ff': '165',
'd75f00': '166', 'd75f5f': '167', 'd75f87': '168', 'd75faf': '169', 'd75fd7': '170',
'd75fff': '171', 'd78700': '172', 'd7875f': '173', 'd78787': '174', 'd787af': '175',
'd787d7': '176', 'd787ff': '177', 'd7af00': '178', 'd7af5f': '179', 'd7af87': '180',
'd7afaf': '181', 'd7afd7': '182', 'd7afff': '183', 'd7d700': '184', 'd7d75f': '185',
'd7d787': '186', 'd7d7af': '187', 'd7d7d7': '188', 'd7d7ff': '189', 'd7ff00': '190',
'd7ff5f': '191', 'd7ff87': '192', 'd7ffaf': '193', 'd7ffd7': '194', 'd7ffff': '195',
'ff0000': '196', 'ff005f': '197', 'ff0087': '198', 'ff00af': '199', 'ff00d7': '200',
'ff00ff': '201', 'ff5f00': '202', 'ff5f5f': '203', 'ff5f87': '204', 'ff5faf': '205',
'ff5fd7': '206', 'ff5fff': '207', 'ff8700': '208', 'ff875f': '209', 'ff8787': '210',
'ff87af': '211', 'ff87d7': '212', 'ff87ff': '213', 'ffaf00': '214', 'ffaf5f': '215',
'ffaf87': '216', 'ffafaf': '217', 'ffafd7': '218', 'ffafff': '219', 'ffd700': '220',
'ffd75f': '221', 'ffd787': '222', 'ffd7af': '223', 'ffd7d7': '224', 'ffd7ff': '225',
'ffff00': '226', 'ffff5f': '227', 'ffff87': '228', 'ffffaf': '229', 'ffffd7': '230',
'ffffff': '231', '080808': '232', '121212': '233', '1c1c1c': '234', '262626': '235',
'303030': '236', '3a3a3a': '237', '444444': '238', '4e4e4e': '239', '585858': '240',
'626262': '241', '6c6c6c': '242', '767676': '243', '808080': '244', '8a8a8a': '245',
'949494': '246', '9e9e9e': '247', 'a8a8a8': '248', 'b2b2b2': '249', 'bcbcbc': '250',
'c6c6c6': '251', 'd0d0d0': '252', 'dadada': '253', 'e4e4e4': '254', 'eeeeee': '255',
}

## Example line from a vim colorscheme file
##hi Normal ctermfg=NONE ctermbg=NONE gui=NONE guifg=#b7af9f guibg=#202020

def Decompose(hexval):
return float(int(hexval[0:2], 16)), float(int(hexval[2:4], 16)), float(int(hexval[4:6], 16))

def Normalize(r, g, b):
magsqr = r*r + g*g + b*b
if magsqr < 0.0001:
return 0.0, 0.0, 0.0
n = 1.0 / math.sqrt(magsqr)
return r*n, g*n, b*n

def ColorDist(c1, c2):
c1r, c1g, c1b = Decompose(c1)
c2r, c2g, c2b = Decompose(c2)

dr = c1r - c2r
dg = c1g - c2g
db = c1b - c2b
return dr*dr + dg*dg + db*db

def BestMatch(hexval):
best = None
bestdist = 0.0
for key in colors.keys():
dist = ColorDist(hexval, key)
if best is None or dist < bestdist:
best = colors[key]
bestdist = dist
return best

## 1 2 3 4 5 6
fg = re.compile('^(.*)#([^ ]{6})(.*)$')

with open(r'/dev/stdin', 'r') as f:
with open(r'/dev/stdout', 'w') as fout:
for line in f.readlines():
fgm = fg.match(line)
if fgm is not None:
line = (fgm.group(1) + BestMatch(fgm.group(2))
+ fgm.group(3) + "\n")

## -- print the line
fout.write(line)

0 comments on commit 68ac082

Please sign in to comment.