Skip to content

Commit b475be4

Browse files
committed
simplify distance calculation code
1 parent 41f0025 commit b475be4

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

after/syntax/css.vim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ for c in range(0, 23)
7474
let s:colortable += [[value, value, value]]
7575
endfor
7676

77-
function! s:square(x)
78-
return a:x * a:x
77+
function! s:distance(c1,c2)
78+
let [r1,g1,b1] = a:c1
79+
let [r2,g2,b2] = a:c2
80+
let dr = r2 - r1
81+
let dg = g2 - g1
82+
let db = b2 - b1
83+
return dr*dr + dg*dg + db*db
7984
endfunction
8085

8186
" selects the nearest xterm color for a rgb value like #FF0000
@@ -86,8 +91,10 @@ function! s:Rgb2xterm(color)
8691
let r = s:hex[color[1:2]]
8792
let g = s:hex[color[3:4]]
8893
let b = s:hex[color[5:6]]
94+
unlet color
95+
let color = [r,g,b]
8996
for c in range(0,255)
90-
let d = s:square(s:colortable[c][0]-r) + s:square(s:colortable[c][1]-g) + s:square(s:colortable[c][2]-b)
97+
let d = s:distance(color, s:colortable[c])
9198
if d == 0 | return c | endif
9299
if d < smallest_distance
93100
let smallest_distance = d

0 commit comments

Comments
 (0)