1 " guicolorscheme.vim: Convert GUI only color schems
3 " Maintainer: Aaron Griffin <aaron@archlinux.org>
4 " Last Modified: Mon Feb 26 22:52:34 UTC 2007
6 " URL: http://www.vim.org/script.php?script_id=39
8 " Convert a GUI-only colorscheme to support 88 and 256 color terminals
9 " This should also work on the GUI, so using it 100% of the time, assuming
10 " you always have a non 8/16 color terminal should work fine
12 " conversion functions {{{
13 " canibalized from desert256.vim
15 function! s:greynum(x) "{{{
42 let l:n = (a:x - 8) / 10
43 let l:m = (a:x - 8) % 10
52 function! s:greylvl(n) "{{{
83 function! s:grey(n) "{{{
103 function! s:rgbnum(x) "{{{
118 let l:n = (a:x - 55) / 40
119 let l:m = (a:x - 55) % 40
128 function! s:rgblvl(n) "{{{
143 return 55 + (a:n * 40)
147 function! s:rgb(r, g, b) "{{{
149 return 16 + (a:r * 16) + (a:g * 4) + a:b
151 return 16 + (a:r * 36) + (a:g * 6) + a:b
155 function! s:color(r, g, b) "{{{
156 " get the closest grey
157 let l:gx = s:greynum(a:r)
158 let l:gy = s:greynum(a:g)
159 let l:gz = s:greynum(a:b)
161 " get the closest color
162 let l:x = s:rgbnum(a:r)
163 let l:y = s:rgbnum(a:g)
164 let l:z = s:rgbnum(a:b)
166 let l:level = (a:r * a:r) + (a:g * a:g) + (a:b * a:b)
167 if l:gx == l:gy && l:gy == l:gz
168 " there are two possibilities
169 let l:dgr = s:greylvl(l:gx)
170 let l:dgg = s:greylvl(l:gy)
171 let l:dgb = s:greylvl(l:gz)
172 let l:dgrey = (l:dgr * l:dgr) + (l:dgg * l:dgg) + (l:dgb * l:dgb) - l:level
174 let l:dr = s:rgblvl(l:gx)
175 let l:dg = s:rgblvl(l:gy)
176 let l:db = s:rgblvl(l:gz)
177 let l:drgb = (l:dr * l:dr) + (l:dg * l:dg) + (l:db * l:db) - l:level
184 return s:rgb(l:x, l:y, l:z)
187 " only one possibility
188 return s:rgb(l:x, l:y, l:z)
191 function! s:cindex(rgb) "{{{
192 "convert RRGGBB to a terminal (numeric) index
193 let l:r = ("0x" . strpart(a:rgb, 0, 2)) + 0
194 let l:g = ("0x" . strpart(a:rgb, 2, 2)) + 0
195 let l:b = ("0x" . strpart(a:rgb, 4, 2)) + 0
197 return s:color(l:r, l:g, l:b)
200 function! s:HL(group, fg, bg, attr) "{{{
201 "The main highlight (HL) function
202 exec "hi clear " . a:group
204 exec "hi " . a:group . " guifg=#" . a:fg . " ctermfg=" . s:cindex(a:fg)
207 exec "hi " . a:group . " guibg=#" . a:bg . " ctermbg=" . s:cindex(a:bg)
210 exec "hi " . a:group . " gui=" . a:attr . " cterm=" . a:attr
216 " Completion Helpers {{{
217 function! s:Colorscheme_Complete(A,L,P)
218 let l:files = split(globpath(&runtimepath, "colors/" . a:A . "*"), "\n")
221 let l:basename = substitute(file, ".*/", "", "")
222 let l:basename = substitute(l:basename, "\.vim", "", "")
223 call add(l:retlist, l:basename)
228 function! s:GetColorschemeFile(fname)
229 let l:files = split(globpath(&runtimepath, "colors/" . a:fname . ".vim"), "\n")
231 echoerr "Colorscheme ".a:fname." not found"
238 command! -complete=customlist,s:Colorscheme_Complete -nargs=1 GuiColorScheme :call s:GuiColorScheme("<args>")
241 function! s:GuiColorScheme(fname)
242 let l:file = s:GetColorschemeFile(a:fname)
247 if has("gui_running")
248 exec "colorscheme " . l:file
252 for line in readfile(l:file)
260 let l:start = match(line, "hi")
261 let l:end = match(line, "[ \t]", l:start)
262 let l:start = l:end +1
263 let l:end = match(line, "[ \t]", l:start)
264 let l:name = strpart(line, l:start, l:end - l:start)
266 " strip foreground color
267 let l:start = match(line, "guifg=")
269 let l:start = l:start + 6 "strlen(guifg=)
270 let l:end = match(line, "[ \t]", l:start)
272 let l:fg = strpart(line, l:start)
274 let l:fg = strpart(line, l:start, l:end - l:start)
276 if strpart(l:fg, 0, 1) == "#"
277 let l:fg = strpart(l:fg, 1)
281 " strip background color
282 let l:start = match(line, "guibg=")
284 let l:start = l:start + 6 "strlen(guibg=)
285 let l:end = match(line, "[ \t]", l:start)
287 let l:bg = strpart(line, l:start)
289 let l:bg = strpart(line, l:start, l:end - l:start)
291 if strpart(l:bg, 0, 1) == "#"
292 let l:bg = strpart(l:bg, 1)
297 let l:start = match(line, "gui=")
299 let l:start = l:start + 4 "strlen(gui=)
300 let l:end = match(line, "[ \t]", l:start)
302 let l:attr = strpart(line, l:start)
304 let l:attr = strpart(line, l:start, l:end - l:start)
308 call s:HL(l:name, l:fg, l:bg, l:attr)
313 " vim:ft=vim:fdl=0:fdm=marker:ts=4:sw=4