PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/bundle/vim-css-color/after/syntax/css.vim

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