Skip to content

Commit bb0a47e

Browse files
committed
simplify xterm color table precalc
1 parent 0755def commit bb0a47e

1 file changed

Lines changed: 33 additions & 39 deletions

File tree

after/syntax/css.vim

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,51 +38,45 @@ function! s:SetMatcher(clr,pat)
3838
endif
3939
endfunction
4040

41-
" the 6 value iterations in the xterm color cube
42-
let s:valuerange = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
41+
" xterm color table
42+
" 16 vt100 colors preset
43+
let s:colortable=[
44+
\ [ 0x00, 0x00, 0x00 ],
45+
\ [ 0xCD, 0x00, 0x00 ],
46+
\ [ 0x00, 0xCD, 0x00 ],
47+
\ [ 0xCD, 0xCD, 0x00 ],
48+
\ [ 0x00, 0x00, 0xEE ],
49+
\ [ 0xCD, 0x00, 0xCD ],
50+
\ [ 0x00, 0xCD, 0xCD ],
51+
\ [ 0xE5, 0xE5, 0xE5 ],
52+
\ [ 0x7F, 0x7F, 0x7F ],
53+
\ [ 0xFF, 0x00, 0x00 ],
54+
\ [ 0x00, 0xFF, 0x00 ],
55+
\ [ 0xFF, 0xFF, 0x00 ],
56+
\ [ 0x5C, 0x5C, 0xFF ],
57+
\ [ 0xFF, 0x00, 0xFF ],
58+
\ [ 0x00, 0xFF, 0xFF ],
59+
\ [ 0xFF, 0xFF, 0xFF ]]
4360

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-
58-
" color cube color
59-
if a:color>=16 && a:color<=232
60-
let color=a:color-16
61-
let r = s:valuerange[(color/36)%6]
62-
let g = s:valuerange[(color/6)%6]
63-
let b = s:valuerange[color%6]
64-
endif
61+
" xterm color cube
62+
let valuerange = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ] " the 6 values used
63+
for c in range(0, 215)
64+
let r = valuerange[ ( c / 36 ) % 6 ]
65+
let g = valuerange[ ( c / 6 ) % 6 ]
66+
let b = valuerange[ c % 6 ]
67+
let s:colortable += [[r,g,b]]
68+
endfor
6569

66-
" gray tone
67-
if a:color>=233 && a:color<=253
68-
let r=8+(a:color-232)*0x0a
69-
let g=r
70-
let b=r
71-
endif
72-
let rgb=[r,g,b]
73-
return rgb
74-
endfunction
70+
" grayscale ramp
71+
for c in range(0, 23)
72+
let value = 8 + c * 0x0a
73+
let s:colortable += [[value, value, value]]
74+
endfor
7575

7676
function! s:square(x)
7777
return a:x * a:x
7878
endfunction
7979

80-
let s:colortable=[]
81-
for c in range(0, 254)
82-
let color = s:Xterm2rgb(c)
83-
call add(s:colortable, color)
84-
endfor
85-
8680
" selects the nearest xterm color for a rgb value like #FF0000
8781
function! s:Rgb2xterm(color)
8882
let best_match=0
@@ -91,7 +85,7 @@ function! s:Rgb2xterm(color)
9185
let r = s:hex[color[1:2]]
9286
let g = s:hex[color[3:4]]
9387
let b = s:hex[color[5:6]]
94-
for c in range(0,254)
88+
for c in range(0,255)
9589
let d = s:square(s:colortable[c][0]-r) + s:square(s:colortable[c][1]-g) + s:square(s:colortable[c][2]-b)
9690
if d<smallest_distance
9791
let smallest_distance = d

0 commit comments

Comments
 (0)