@@ -65,10 +65,12 @@ endfunction
65
65
let s: RGB_HEX_RE = ' \v^#(\x{3}(\x{3})?)$'
66
66
let s: RGB_RE = ' \v^rgb\((\d+),\s*(\d+),\s*(\d+)\)$'
67
67
let s: HSL_RE = ' \v^hsl\((\d+),\s*(\d+)\%,\s*(\d+)\%\)$'
68
+ let s: VIM_RGB_FILE = expand (' $VIMRUNTIME/rgb.txt' )
68
69
function ! s: parse (str) abort
69
70
if type (a: str ) !=# type (' ' )
70
71
throw ' vital: Color: parse(): invalid format: ' . a: str
71
72
endif
73
+ " e.g. #FFFFFF
72
74
let m = matchlist (a: str , s: RGB_HEX_RE )
73
75
if ! empty (m )
74
76
if strlen (m [1 ]) == # 3
@@ -78,19 +80,53 @@ function! s:parse(str) abort
78
80
endif
79
81
return s: rgb (r , g , b )
80
82
endif
83
+ " e.g. rgb(255,255,255)
81
84
let m = matchlist (a: str , s: RGB_RE )
82
85
if ! empty (m )
83
86
let [r , g , b ] = [str2float (m [1 ]), str2float (m [2 ]), str2float (m [3 ])]
84
87
return s: rgb (r , g , b )
85
88
endif
89
+ " e.g. hsl(0,0%,100%)
86
90
let m = matchlist (a: str , s: HSL_RE )
87
91
if ! empty (m )
88
92
let [h , s , l ] = [str2float (m [1 ]), str2float (m [2 ]), str2float (m [3 ])]
89
93
return s: hsl (h , s , l )
90
94
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
91
104
throw ' vital: Color: parse(): invalid format: ' . a: str
92
105
endfunction
93
106
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
+
94
130
function ! s: rgb (r , g , b ) abort
95
131
if ! s: _check_rgb_range (a: r , a: g , a: b )
96
132
throw printf (' vital: Color: rgb(): invalid value: r = %s, g = %s, b = %s' ,
0 commit comments