From 2f539bbe1ff33a574638a8f4056c9b06e37d81de Mon Sep 17 00:00:00 2001 From: Nate Kane Date: Mon, 6 Dec 2010 21:30:58 +1000 Subject: [PATCH] Added methods for converting between rgb and hex color formats --- plugin/indent-guides.vim | 86 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/plugin/indent-guides.vim b/plugin/indent-guides.vim index d7f1abc..3253072 100644 --- a/plugin/indent-guides.vim +++ b/plugin/indent-guides.vim @@ -1,6 +1,8 @@ " Author: Nate Kane " Homepage: http://github.com/nathanaelkane/vim-indent-guides +let s:hex_color_pattern = '#[0-9A-Fa-f]\{6\}' + function! s:IndentGuidesToggle() let g:IndentGuides_matches = \ exists('g:IndentGuides_matches') ? g:IndentGuides_matches : [] @@ -43,8 +45,8 @@ function! s:IndentGuidesDisable() endfunction function! s:IndentGuidesHighlightColors() - hi IndentLevelOdd guibg=#252525 - hi IndentLevelEven guibg=#303030 + hi IndentLevelOdd guibg=#FFFFFF + hi IndentLevelEven guibg=#FBFBFB endfunction " Commands @@ -59,3 +61,83 @@ let g:IndentGuides_debug = 0 " Default mapping nmap ig :IndentGuidesToggle +" +" Return hex string equivalent to given decimal string or number. +" +" Example: DecToHex(255, 2) +" Returns: 'FF' +" +" Example: DecToHex(255, 5) +" Returns: '000FF' +" +function! DecToHex(arg, padding) + return toupper(printf('%0' . a:padding . 'x', a:arg + 0)) +endfunction + +" +" Return number equivalent to given hex string ('0x' is optional). +" +" Example: HexToDec('FF') +" Returns: 255 +" +" Example: HexToDec('88') +" Returns: 136 +" +" Example: HexToDec('00') +" Returns: 0 +" +function! HexToDec(arg) + return (a:arg =~? '^0x') ? a:arg + 0 : ('0x'.a:arg) + 0 +endfunction + +" Example: HexColorToRGB('#0088FF') +" Returns: [0, 136, 255] +function! HexColorToRGB(hex_color) + let l:rgb = [] + if matchstr(a:hex_color, s:hex_color_pattern) == a:hex_color + let l:red = HexToDec(strpart(a:hex_color, 1, 2)) + let l:green = HexToDec(strpart(a:hex_color, 3, 2)) + let l:blue = HexToDec(strpart(a:hex_color, 5, 2)) + let l:rgb = [l:red, l:green, l:blue] + end + return l:rgb +endfunction + +" +" Example: RGBColorToHex([0, 136, 255]) +" Returns: '#0088FF' +" +function! RGBColorToHex(rgb_color) + let l:hex_color = '#' + let l:hex_color .= DecToHex(a:rgb_color[0], 2) " red + let l:hex_color .= DecToHex(a:rgb_color[1], 2) " green + let l:hex_color .= DecToHex(a:rgb_color[2], 2) " blue + return l:hex_color +endfunction + +" +" Example: HexColorBrighten('#000000', 0.10) +" Returns: '#191919' +" +function! HexColorBrighten(color, percent) + let l:rgb = HexColorToRGB(a:color) + let l:rgb_brightened = [] + for decimal in l:rgb + call add(l:rgb_brightened, float2nr((255 - decimal) * a:percent)) + endfor + return RGBColorToHex(l:rgb_brightened) +endfunction + +" +" Example: HexColorDarken('#FFFFFF', 0.10) +" Returns: '#E5E5E5' +" +function! HexColorDarken(color, percent) + let l:rgb = HexColorToRGB(a:color) + let l:rgb_darkened = [] + for decimal in l:rgb + call add(l:rgb_darkened, float2nr(decimal * (1 - a:percent))) + endfor + return RGBColorToHex(l:rgb_darkened) +endfunction +