css.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. " Language: Colored CSS Color Preview
  2. " Maintainer: Niklas Hofer <niklas+vim@lanpartei.de>
  3. " URL: svn://lanpartei.de/vimrc/after/syntax/css.vim
  4. " Last Change: 2008 Feb 12
  5. " Licence: No Warranties. Do whatever you want with this. But please tell me!
  6. " Version: 0.6
  7. function! s:FGforBG(bg)
  8. " takes a 6hex color code and returns a matching color that is visible
  9. let pure = substitute(a:bg,'^#','','')
  10. let r = eval('0x'.pure[0].pure[1])
  11. let g = eval('0x'.pure[2].pure[3])
  12. let b = eval('0x'.pure[4].pure[5])
  13. if r*30 + g*59 + b*11 > 12000
  14. return '#000000'
  15. else
  16. return '#ffffff'
  17. end
  18. endfunction
  19. function! s:SetMatcher(clr,pat)
  20. let group = 'cssColor'.substitute(a:clr,'^#','','')
  21. redir => s:currentmatch
  22. silent! exe 'syn list '.group
  23. redir END
  24. if s:currentmatch !~ a:pat.'\/'
  25. exe 'syn match '.group.' /'.a:pat.'\>/ contained'
  26. exe 'syn cluster cssColors add='.group
  27. if has('gui_running')
  28. exe 'hi '.group.' guifg='.s:FGforBG(a:clr)
  29. exe 'hi '.group.' guibg='.a:clr
  30. elseif &t_Co == 256
  31. exe 'hi '.group.' ctermfg='.s:Rgb2xterm(s:FGforBG(a:clr))
  32. exe 'hi '.group.' ctermbg='.s:Rgb2xterm(a:clr)
  33. endif
  34. return 1
  35. else
  36. return 0
  37. endif
  38. endfunction
  39. "" the 6 value iterations in the xterm color cube
  40. let s:valuerange = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
  41. "
  42. "" 16 basic colors
  43. let s:basic16 = [ [ 0x00, 0x00, 0x00 ], [ 0xCD, 0x00, 0x00 ], [ 0x00, 0xCD, 0x00 ], [ 0xCD, 0xCD, 0x00 ], [ 0x00, 0x00, 0xEE ], [ 0xCD, 0x00, 0xCD ], [ 0x00, 0xCD, 0xCD ], [ 0xE5, 0xE5, 0xE5 ], [ 0x7F, 0x7F, 0x7F ], [ 0xFF, 0x00, 0x00 ], [ 0x00, 0xFF, 0x00 ], [ 0xFF, 0xFF, 0x00 ], [ 0x5C, 0x5C, 0xFF ], [ 0xFF, 0x00, 0xFF ], [ 0x00, 0xFF, 0xFF ], [ 0xFF, 0xFF, 0xFF ] ]
  44. :
  45. function! s:Xterm2rgb(color)
  46. " 16 basic colors
  47. let r=0
  48. let g=0
  49. let b=0
  50. if a:color<16
  51. let r = s:basic16[a:color][0]
  52. let g = s:basic16[a:color][1]
  53. let b = s:basic16[a:color][2]
  54. endif
  55. " color cube color
  56. if a:color>=16 && a:color<=232
  57. let color=a:color-16
  58. let r = s:valuerange[(color/36)%6]
  59. let g = s:valuerange[(color/6)%6]
  60. let b = s:valuerange[color%6]
  61. endif
  62. " gray tone
  63. if a:color>=233 && a:color<=253
  64. let r=8+(a:color-232)*0x0a
  65. let g=r
  66. let b=r
  67. endif
  68. let rgb=[r,g,b]
  69. return rgb
  70. endfunction
  71. function! s:pow(x, n)
  72. let x = a:x
  73. for i in range(a:n-1)
  74. let x = x*a:x
  75. return x
  76. endfunction
  77. let s:colortable=[]
  78. for c in range(0, 254)
  79. let color = s:Xterm2rgb(c)
  80. call add(s:colortable, color)
  81. endfor
  82. " selects the nearest xterm color for a rgb value like #FF0000
  83. function! s:Rgb2xterm(color)
  84. let best_match=0
  85. let smallest_distance = 10000000000
  86. let r = eval('0x'.a:color[1].a:color[2])
  87. let g = eval('0x'.a:color[3].a:color[4])
  88. let b = eval('0x'.a:color[5].a:color[6])
  89. for c in range(0,254)
  90. let d = s:pow(s:colortable[c][0]-r,2) + s:pow(s:colortable[c][1]-g,2) + s:pow(s:colortable[c][2]-b,2)
  91. if d<smallest_distance
  92. let smallest_distance = d
  93. let best_match = c
  94. endif
  95. endfor
  96. return best_match
  97. endfunction
  98. function! s:SetNamedColor(clr,name)
  99. let group = 'cssColor'.substitute(a:clr,'^#','','')
  100. exe 'syn keyword '.group.' '.a:name.' contained'
  101. exe 'syn cluster cssColors add='.group
  102. if has('gui_running')
  103. exe 'hi '.group.' guifg='.s:FGforBG(a:clr)
  104. exe 'hi '.group.' guibg='.a:clr
  105. elseif &t_Co == 256
  106. exe 'hi '.group.' ctermfg='.s:Rgb2xterm(s:FGforBG(a:clr))
  107. exe 'hi '.group.' ctermbg='.s:Rgb2xterm(a:clr)
  108. endif
  109. return 23
  110. endfunction
  111. function! s:PreviewCSSColorInLine(where)
  112. " TODO use cssColor matchdata
  113. let foundcolor = matchstr( getline(a:where), '#[0-9A-Fa-f]\{3,6\}\>' )
  114. let color = ''
  115. if foundcolor != ''
  116. if foundcolor =~ '#\x\{6}$'
  117. let color = foundcolor
  118. elseif foundcolor =~ '#\x\{3}$'
  119. let color = substitute(foundcolor, '\(\x\)\(\x\)\(\x\)', '\1\1\2\2\3\3', '')
  120. else
  121. let color = ''
  122. endif
  123. if color != ''
  124. return s:SetMatcher(color,foundcolor)
  125. else
  126. return 0
  127. endif
  128. else
  129. return 0
  130. endif
  131. endfunction
  132. if has("gui_running") || &t_Co==256
  133. " HACK modify cssDefinition to add @cssColors to its contains
  134. redir => s:olddef
  135. silent! syn list cssDefinition
  136. redir END
  137. if s:olddef != ''
  138. let s:b = strridx(s:olddef,'matchgroup')
  139. if s:b != -1
  140. exe 'syn region cssDefinition '.strpart(s:olddef,s:b).',@cssColors'
  141. endif
  142. endif
  143. " w3c Colors
  144. let i = s:SetNamedColor('#800000', 'maroon')
  145. let i = s:SetNamedColor('#ff0000', 'red')
  146. let i = s:SetNamedColor('#ffA500', 'orange')
  147. let i = s:SetNamedColor('#ffff00', 'yellow')
  148. let i = s:SetNamedColor('#808000', 'olive')
  149. let i = s:SetNamedColor('#800080', 'purple')
  150. let i = s:SetNamedColor('#ff00ff', 'fuchsia')
  151. let i = s:SetNamedColor('#ffffff', 'white')
  152. let i = s:SetNamedColor('#00ff00', 'lime')
  153. let i = s:SetNamedColor('#008000', 'green')
  154. let i = s:SetNamedColor('#000080', 'navy')
  155. let i = s:SetNamedColor('#0000ff', 'blue')
  156. let i = s:SetNamedColor('#00ffff', 'aqua')
  157. let i = s:SetNamedColor('#008080', 'teal')
  158. let i = s:SetNamedColor('#000000', 'black')
  159. let i = s:SetNamedColor('#c0c0c0', 'silver')
  160. let i = s:SetNamedColor('#808080', 'gray')
  161. " extra colors
  162. let i = s:SetNamedColor('#F0F8FF','AliceBlue')
  163. let i = s:SetNamedColor('#FAEBD7','AntiqueWhite')
  164. let i = s:SetNamedColor('#7FFFD4','Aquamarine')
  165. let i = s:SetNamedColor('#F0FFFF','Azure')
  166. let i = s:SetNamedColor('#F5F5DC','Beige')
  167. let i = s:SetNamedColor('#FFE4C4','Bisque')
  168. let i = s:SetNamedColor('#FFEBCD','BlanchedAlmond')
  169. let i = s:SetNamedColor('#8A2BE2','BlueViolet')
  170. let i = s:SetNamedColor('#A52A2A','Brown')
  171. let i = s:SetNamedColor('#DEB887','BurlyWood')
  172. let i = s:SetNamedColor('#5F9EA0','CadetBlue')
  173. let i = s:SetNamedColor('#7FFF00','Chartreuse')
  174. let i = s:SetNamedColor('#D2691E','Chocolate')
  175. let i = s:SetNamedColor('#FF7F50','Coral')
  176. let i = s:SetNamedColor('#6495ED','CornflowerBlue')
  177. let i = s:SetNamedColor('#FFF8DC','Cornsilk')
  178. let i = s:SetNamedColor('#DC143C','Crimson')
  179. let i = s:SetNamedColor('#00FFFF','Cyan')
  180. let i = s:SetNamedColor('#00008B','DarkBlue')
  181. let i = s:SetNamedColor('#008B8B','DarkCyan')
  182. let i = s:SetNamedColor('#B8860B','DarkGoldenRod')
  183. let i = s:SetNamedColor('#A9A9A9','DarkGray')
  184. let i = s:SetNamedColor('#A9A9A9','DarkGrey')
  185. let i = s:SetNamedColor('#006400','DarkGreen')
  186. let i = s:SetNamedColor('#BDB76B','DarkKhaki')
  187. let i = s:SetNamedColor('#8B008B','DarkMagenta')
  188. let i = s:SetNamedColor('#556B2F','DarkOliveGreen')
  189. let i = s:SetNamedColor('#FF8C00','Darkorange')
  190. let i = s:SetNamedColor('#9932CC','DarkOrchid')
  191. let i = s:SetNamedColor('#8B0000','DarkRed')
  192. let i = s:SetNamedColor('#E9967A','DarkSalmon')
  193. let i = s:SetNamedColor('#8FBC8F','DarkSeaGreen')
  194. let i = s:SetNamedColor('#483D8B','DarkSlateBlue')
  195. let i = s:SetNamedColor('#2F4F4F','DarkSlateGray')
  196. let i = s:SetNamedColor('#2F4F4F','DarkSlateGrey')
  197. let i = s:SetNamedColor('#00CED1','DarkTurquoise')
  198. let i = s:SetNamedColor('#9400D3','DarkViolet')
  199. let i = s:SetNamedColor('#FF1493','DeepPink')
  200. let i = s:SetNamedColor('#00BFFF','DeepSkyBlue')
  201. let i = s:SetNamedColor('#696969','DimGray')
  202. let i = s:SetNamedColor('#696969','DimGrey')
  203. let i = s:SetNamedColor('#1E90FF','DodgerBlue')
  204. let i = s:SetNamedColor('#B22222','FireBrick')
  205. let i = s:SetNamedColor('#FFFAF0','FloralWhite')
  206. let i = s:SetNamedColor('#228B22','ForestGreen')
  207. let i = s:SetNamedColor('#DCDCDC','Gainsboro')
  208. let i = s:SetNamedColor('#F8F8FF','GhostWhite')
  209. let i = s:SetNamedColor('#FFD700','Gold')
  210. let i = s:SetNamedColor('#DAA520','GoldenRod')
  211. let i = s:SetNamedColor('#808080','Grey')
  212. let i = s:SetNamedColor('#ADFF2F','GreenYellow')
  213. let i = s:SetNamedColor('#F0FFF0','HoneyDew')
  214. let i = s:SetNamedColor('#FF69B4','HotPink')
  215. let i = s:SetNamedColor('#CD5C5C','IndianRed')
  216. let i = s:SetNamedColor('#4B0082','Indigo')
  217. let i = s:SetNamedColor('#FFFFF0','Ivory')
  218. let i = s:SetNamedColor('#F0E68C','Khaki')
  219. let i = s:SetNamedColor('#E6E6FA','Lavender')
  220. let i = s:SetNamedColor('#FFF0F5','LavenderBlush')
  221. let i = s:SetNamedColor('#7CFC00','LawnGreen')
  222. let i = s:SetNamedColor('#FFFACD','LemonChiffon')
  223. let i = s:SetNamedColor('#ADD8E6','LightBlue')
  224. let i = s:SetNamedColor('#F08080','LightCoral')
  225. let i = s:SetNamedColor('#E0FFFF','LightCyan')
  226. let i = s:SetNamedColor('#FAFAD2','LightGoldenRodYellow')
  227. let i = s:SetNamedColor('#D3D3D3','LightGray')
  228. let i = s:SetNamedColor('#D3D3D3','LightGrey')
  229. let i = s:SetNamedColor('#90EE90','LightGreen')
  230. let i = s:SetNamedColor('#FFB6C1','LightPink')
  231. let i = s:SetNamedColor('#FFA07A','LightSalmon')
  232. let i = s:SetNamedColor('#20B2AA','LightSeaGreen')
  233. let i = s:SetNamedColor('#87CEFA','LightSkyBlue')
  234. let i = s:SetNamedColor('#778899','LightSlateGray')
  235. let i = s:SetNamedColor('#778899','LightSlateGrey')
  236. let i = s:SetNamedColor('#B0C4DE','LightSteelBlue')
  237. let i = s:SetNamedColor('#FFFFE0','LightYellow')
  238. let i = s:SetNamedColor('#32CD32','LimeGreen')
  239. let i = s:SetNamedColor('#FAF0E6','Linen')
  240. let i = s:SetNamedColor('#FF00FF','Magenta')
  241. let i = s:SetNamedColor('#66CDAA','MediumAquaMarine')
  242. let i = s:SetNamedColor('#0000CD','MediumBlue')
  243. let i = s:SetNamedColor('#BA55D3','MediumOrchid')
  244. let i = s:SetNamedColor('#9370D8','MediumPurple')
  245. let i = s:SetNamedColor('#3CB371','MediumSeaGreen')
  246. let i = s:SetNamedColor('#7B68EE','MediumSlateBlue')
  247. let i = s:SetNamedColor('#00FA9A','MediumSpringGreen')
  248. let i = s:SetNamedColor('#48D1CC','MediumTurquoise')
  249. let i = s:SetNamedColor('#C71585','MediumVioletRed')
  250. let i = s:SetNamedColor('#191970','MidnightBlue')
  251. let i = s:SetNamedColor('#F5FFFA','MintCream')
  252. let i = s:SetNamedColor('#FFE4E1','MistyRose')
  253. let i = s:SetNamedColor('#FFE4B5','Moccasin')
  254. let i = s:SetNamedColor('#FFDEAD','NavajoWhite')
  255. let i = s:SetNamedColor('#FDF5E6','OldLace')
  256. let i = s:SetNamedColor('#6B8E23','OliveDrab')
  257. let i = s:SetNamedColor('#FF4500','OrangeRed')
  258. let i = s:SetNamedColor('#DA70D6','Orchid')
  259. let i = s:SetNamedColor('#EEE8AA','PaleGoldenRod')
  260. let i = s:SetNamedColor('#98FB98','PaleGreen')
  261. let i = s:SetNamedColor('#AFEEEE','PaleTurquoise')
  262. let i = s:SetNamedColor('#D87093','PaleVioletRed')
  263. let i = s:SetNamedColor('#FFEFD5','PapayaWhip')
  264. let i = s:SetNamedColor('#FFDAB9','PeachPuff')
  265. let i = s:SetNamedColor('#CD853F','Peru')
  266. let i = s:SetNamedColor('#FFC0CB','Pink')
  267. let i = s:SetNamedColor('#DDA0DD','Plum')
  268. let i = s:SetNamedColor('#B0E0E6','PowderBlue')
  269. let i = s:SetNamedColor('#BC8F8F','RosyBrown')
  270. let i = s:SetNamedColor('#4169E1','RoyalBlue')
  271. let i = s:SetNamedColor('#8B4513','SaddleBrown')
  272. let i = s:SetNamedColor('#FA8072','Salmon')
  273. let i = s:SetNamedColor('#F4A460','SandyBrown')
  274. let i = s:SetNamedColor('#2E8B57','SeaGreen')
  275. let i = s:SetNamedColor('#FFF5EE','SeaShell')
  276. let i = s:SetNamedColor('#A0522D','Sienna')
  277. let i = s:SetNamedColor('#87CEEB','SkyBlue')
  278. let i = s:SetNamedColor('#6A5ACD','SlateBlue')
  279. let i = s:SetNamedColor('#708090','SlateGray')
  280. let i = s:SetNamedColor('#708090','SlateGrey')
  281. let i = s:SetNamedColor('#FFFAFA','Snow')
  282. let i = s:SetNamedColor('#00FF7F','SpringGreen')
  283. let i = s:SetNamedColor('#4682B4','SteelBlue')
  284. let i = s:SetNamedColor('#D2B48C','Tan')
  285. let i = s:SetNamedColor('#D8BFD8','Thistle')
  286. let i = s:SetNamedColor('#FF6347','Tomato')
  287. let i = s:SetNamedColor('#40E0D0','Turquoise')
  288. let i = s:SetNamedColor('#EE82EE','Violet')
  289. let i = s:SetNamedColor('#F5DEB3','Wheat')
  290. let i = s:SetNamedColor('#F5F5F5','WhiteSmoke')
  291. let i = s:SetNamedColor('#9ACD32','YellowGreen')
  292. let i = 1
  293. while i <= line("$")
  294. call s:PreviewCSSColorInLine(i)
  295. let i = i+1
  296. endwhile
  297. unlet i
  298. autocmd CursorHold * silent call s:PreviewCSSColorInLine('.')
  299. autocmd CursorHoldI * silent call s:PreviewCSSColorInLine('.')
  300. set ut=100
  301. endif " has("gui_running")