Skip to content

Commit 8d079ca

Browse files
committed
Color: parse(): support color name
Read color name/RGB mappings from $VIMRUNTIME/rgb.txt
1 parent c3d5c7b commit 8d079ca

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

autoload/vital/__vital__/Color.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ endfunction
6565
let s:RGB_HEX_RE = '\v^#(\x{3}(\x{3})?)$'
6666
let s:RGB_RE = '\v^rgb\((\d+),\s*(\d+),\s*(\d+)\)$'
6767
let s:HSL_RE = '\v^hsl\((\d+),\s*(\d+)\%,\s*(\d+)\%\)$'
68+
let s:VIM_RGB_FILE = expand('$VIMRUNTIME/rgb.txt')
6869
function! s:parse(str) abort
6970
if type(a:str) !=# type('')
7071
throw 'vital: Color: parse(): invalid format: ' . a:str
7172
endif
73+
" e.g. #FFFFFF
7274
let m = matchlist(a:str, s:RGB_HEX_RE)
7375
if !empty(m)
7476
if strlen(m[1]) ==# 3
@@ -78,19 +80,53 @@ function! s:parse(str) abort
7880
endif
7981
return s:rgb(r, g, b)
8082
endif
83+
" e.g. rgb(255,255,255)
8184
let m = matchlist(a:str, s:RGB_RE)
8285
if !empty(m)
8386
let [r, g, b] = [str2float(m[1]), str2float(m[2]), str2float(m[3])]
8487
return s:rgb(r, g, b)
8588
endif
89+
" e.g. hsl(0,0%,100%)
8690
let m = matchlist(a:str, s:HSL_RE)
8791
if !empty(m)
8892
let [h, s, l] = [str2float(m[1]), str2float(m[2]), str2float(m[3])]
8993
return s:hsl(h, s, l)
9094
endif
95+
" e.g. DarkGray
96+
if filereadable(s:VIM_RGB_FILE)
97+
let color_map = s:_parse_rgb_file(s:VIM_RGB_FILE)
98+
let name = s:_normalize_color_name(a:str)
99+
if has_key(color_map, name)
100+
let [r, g, b] = color_map[name]
101+
return s:rgb(r, g, b)
102+
endif
103+
endif
91104
throw 'vital: Color: parse(): invalid format: ' . a:str
92105
endfunction
93106

107+
let s:RGB_FILE_RE = '\v^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.+)$'
108+
function! s:_parse_rgb_file(file) abort
109+
let color_map = {}
110+
for line in readfile(a:file)
111+
let m = matchlist(line, s:RGB_FILE_RE)
112+
if empty(m)
113+
continue
114+
endif
115+
let [r, g, b] = map(m[1:3], 'str2float(v:val)')
116+
if !s:_check_rgb_range(r, g, b)
117+
continue
118+
endif
119+
let color_map[s:_normalize_color_name(m[4])] = [r, g, b]
120+
endfor
121+
return color_map
122+
endfunction
123+
124+
function! s:_normalize_color_name(str) abort
125+
let str = substitute(a:str, '\s\+$', '', '')
126+
let str = substitute(str, '^\s\+', '', '')
127+
return tolower(str)
128+
endfunction
129+
94130
function! s:rgb(r, g, b) abort
95131
if !s:_check_rgb_range(a:r, a:g, a:b)
96132
throw printf('vital: Color: rgb(): invalid value: r = %s, g = %s, b = %s',

test/Color.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function! s:suite.rgb()
2222
call s:assert.equals(s:C.rgb(0, 0, 0).as_rgb_str(), 'rgb('.0x00.','.0x00.','.0x00.')')
2323
endfunction
2424

25+
function! s:suite.color_name()
26+
call s:assert.equals(s:C.parse('Yellow').as_rgb_hex(), '#FFFF00')
27+
call s:assert.equals(s:C.parse('ForestGreen').as_rgb(), [34.0, 139.0, 34.0])
28+
call s:assert.equals(s:C.parse('Forest Green').as_rgb(), [34.0, 139.0, 34.0])
29+
call s:assert.equals(s:C.parse('Snow').as_rgb(), [255.0, 250.0, 250.0])
30+
endfunction
31+
2532
function! s:suite.hsl()
2633
call s:assert.equals(s:C.parse('hsl(210,68%,80%)').as_hsl_str(), 'hsl(210,68%,80%)')
2734
call s:assert.equals(s:C.parse('hsl(210,100%,7%)').as_hsl_str(), 'hsl(210,100%,7%)')

0 commit comments

Comments
 (0)