Guest User

map hex rgb to terminal colour numbers in vim

a guest
Apr 13th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.39 KB | None | 0 0
  1. " This script allows for a colour scheme to specify colours as #RRGGBB
  2. " and have them mapped to the nearest colours in the Xterm-style cube.
  3. " Xterms of 8, 16, 88, and 256 colours are supported.
  4.  
  5. if &t_Co == 256
  6.     function! High#Map(r, g, b)
  7.         if (a:r == a:g) && (a:g == a:b)
  8.             if (a:r >= 250)
  9.                 return 231
  10.             else
  11.                 return 232 + (10 * a:r) / 104
  12.             endif
  13.         else
  14.             return 16 + 36 * (a:r / 43) + 6 * (a:g / 43) + (a:b / 43)
  15.         endif
  16.     endfunction
  17. elseif &t_Co == 88
  18.     function! High#Map(r, g, b)
  19.         if (a:r == a:g) && (a:g == a:b)
  20.             if (a:r >= 250)
  21.                 return 79
  22.             else
  23.                 return 80 + a:r / 32
  24.             endif
  25.         else
  26.             return 16 + 16 * (a:r / 77) + 4 * (a:g / 77) + (a:b / 77)
  27.         endif
  28.     endfunction
  29. else
  30.     function! High#Map(r, g, b)
  31.         if (a:r == a:g) && (a:g == a:b)
  32.             let n = a:r / 16
  33.             let s = "088888888888777F"
  34.         else
  35.             let n = 16 * (a:r / 77) + 4 * (a:g / 77) + (a:b / 77)
  36.             let s = "044C266E266EAEEE155D384C326EBAEE155D315D337CBBAE9DDDB9DDBB9DBBBF"
  37.         endif
  38.         let c = str2nr(strpart(s, n, 1), 16)
  39.         if &t_Co < 16
  40.             let c = c % 8
  41.         endif
  42.         return c
  43.     endfunction
  44. endif
  45.  
  46. function! High#Color(c)
  47.     if a:c !~ "^#"
  48.         if a:c == "bg"
  49.             return "none"
  50.         else
  51.             return a:c
  52.         endif
  53.     endif
  54.     let r = str2nr(strpart(a:c, 1, 2), 16)
  55.     let g = str2nr(strpart(a:c, 3, 2), 16)
  56.     let b = str2nr(strpart(a:c, 5, 2), 16)
  57.     return High#Map(r, g, b)
  58. endfunction
  59.  
  60. function! High#GuiColor(c, d)
  61.     if a:c !~ "^#"
  62.         if a:c == "none"
  63.             return a:d
  64.         else
  65.             return a:c
  66.         endif
  67.     endif
  68.     return a:c
  69. endfunction
  70.  
  71. function! High#Light(what, how, bg, fg)
  72.     let cbg = High#Color(a:bg)
  73.     let cfg = High#Color(a:fg)
  74.     let gbg = High#GuiColor(a:bg, "bg")
  75.     let gfg = High#GuiColor(a:fg, "fg")
  76.     execute "highlight " . a:what . " cterm="   . a:how
  77.     execute "highlight " . a:what . " ctermbg=" . cbg
  78.     execute "highlight " . a:what . " ctermfg=" . cfg
  79.     execute "highlight " . a:what . " gui="     . a:how
  80.     execute "highlight " . a:what . " guibg="   . gbg
  81.     execute "highlight " . a:what . " guifg="   . gfg
  82. endfunction
Advertisement
Add Comment
Please, Sign In to add comment