Skip to content

Instantly share code, notes, and snippets.

@oppara
Created December 17, 2009 15:16
Show Gist options
  • Save oppara/258801 to your computer and use it in GitHub Desktop.
Save oppara/258801 to your computer and use it in GitHub Desktop.
Vimで#RRGGBBからrgb(r,g,b)に変換
" http://hail2u.net/blog/software/convert-hex-color-to-functional-color-with-vim.html
command! -range=% HexToFunc :silent!<line1>,<line2>s/#¥([0-9A-F]¥{3,6}¥)/¥=HexToFunc(submatch(1))/gi
function! HexToFunc(hex)
if strlen( a:hex ) == 6
let color = matchlist(a:hex, '¥([0-9A-F]¥{2¥}¥)¥([0-9A-F]¥{2¥}¥)¥([0-9A-F]¥{2¥}¥)')
return s:ToRgbFunc(color[1], color[2], color[3])
endif
let color = split(a:hex, '¥zs')
return s:ToRgbFunc(repeat(color[0], 2), repeat(color[1], 2), repeat(color[2], 2))
endfunction
function! s:ToRgbFunc(r, g, b)
return 'rgb(' . printf('%d', '0x' . a:r) . ', ' . printf('%d', '0x' . a:g) . ', ' . printf('%d', '0x' . a:b) . ')'
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment