Skip to content

Commit 1d77924

Browse files
committed
switch to precalculated hex table
1 parent fd36cab commit 1d77924

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

after/syntax/css.vim

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
" Licence: No Warranties. WTFPL. But please tell me!
55
" Version: 0.7.1
66

7-
function! s:hex(hex)
8-
return eval('0x'.a:hex)
9-
endfunction
7+
let s:hex={}
8+
for i in range(0, 255)
9+
let s:hex[ printf( '%02x', i ) ] = i
10+
endfor
1011

1112
function! s:FGforBG(bg)
1213
" takes a 6hex color code and returns a matching color that is visible
13-
let val = matchlist(a:bg,'^#\(\x\x\)\(\x\x\)\(\x\x\)$')
14-
let r = s:hex(val[1])
15-
let g = s:hex(val[2])
16-
let b = s:hex(val[3])
17-
if r*30 + g*59 + b*11 > 12000
18-
return '#000000'
19-
else
20-
return '#ffffff'
21-
end
14+
let rgb = matchlist(tolower(a:bg),'^#\(\x\x\)\(\x\x\)\(\x\x\)$')
15+
if len( rgb ) == 0 | return '#cccccc' | endif
16+
let [r,g,b] = rgb[1:3]
17+
return s:hex[r]*30 + s:hex[g]*59 + s:hex[b]*11 > 12000 ? '#000000' : '#ffffff'
2218
endfunction
2319

2420
function! s:SetMatcher(clr,pat)
@@ -94,9 +90,10 @@ endfor
9490
function! s:Rgb2xterm(color)
9591
let best_match=0
9692
let smallest_distance = 10000000000
97-
let r = s:hex(a:color[1].a:color[2])
98-
let g = s:hex(a:color[3].a:color[4])
99-
let b = s:hex(a:color[5].a:color[6])
93+
let color = tolower(a:color)
94+
let r = s:hex[color[1:2]]
95+
let g = s:hex[color[3:4]]
96+
let b = s:hex[color[5:6]]
10097
for c in range(0,254)
10198
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)
10299
if d<smallest_distance

0 commit comments

Comments
 (0)