Debian Codegraph @

lua/lualine/utils/color_utils.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local M = {}
4
5 -- stylua: ignore start
6 -- color conversion
7 local color_table = {
8 -- lookup table for cterm colors
9 -- format {'color_code', {r,g,b}}
10
11 -- Primary 3-bit (8 colors). Unique representation!
12 {'00', { 0, 0, 0 }},
13 {'01', { 128, 0, 0 }},
14 {'02', { 0, 128, 0 }},
15 {'03', { 128, 128, 0 }},
16 {'04', { 0, 0, 128 }},
17 {'05', { 128, 0, 128 }},
18 {'06', { 0, 128, 128 }},
19 {'07', { 192, 192, 192 }},
20
21 -- equivalent "bright" versions of original 8 colors.
22 {'08', { 128, 128, 128 }},
23 {'09', { 255, 0, 0 }},
24 {'10', { 0, 255, 0 }},
25 {'11', { 255, 255, 0 }},
26 {'12', { 0, 0, 255 }},
27 {'13', { 255, 0, 255 }},
28 {'14', { 0, 255, 255 }},
29 {'15', { 255, 255, 255 }},
30
31 -- Strictly ascending.
32 {'16', { 0, 0, 0 }},
33 {'17', { 0, 0, 95 }},
34 {'18', { 0, 0, 135 }},
35 {'19', { 0, 0, 175 }},
36 {'20', { 0, 0, 215 }},
37 {'21', { 0, 0, 255 }},
38 {'22', { 0, 95, 0 }},
39 {'23', { 0, 95, 95 }},
40 {'24', { 0, 95, 135 }},
41 {'25', { 0, 95, 175 }},
42 {'26', { 0, 95, 215 }},
43 {'27', { 0, 95, 255 }},
44 {'28', { 0, 135, 0 }},
45 {'29', { 0, 135, 95 }},
46 {'30', { 0, 135, 135 }},
47 {'31', { 0, 135, 175 }},
48 {'32', { 0, 135, 215 }},
49 {'33', { 0, 135, 255 }},
50 {'34', { 0, 175, 0 }},
51 {'35', { 0, 175, 95 }},
52 {'36', { 0, 175, 135 }},
53 {'37', { 0, 175, 175 }},
54 {'38', { 0, 175, 215 }},
55 {'39', { 0, 175, 255 }},
56 {'40', { 0, 215, 0 }},
57 {'41', { 0, 215, 95 }},
58 {'42', { 0, 215, 135 }},
59 {'43', { 0, 215, 175 }},
60 {'44', { 0, 215, 215 }},
61 {'45', { 0, 215, 255 }},
62 {'46', { 0, 255, 0 }},
63 {'47', { 0, 255, 95 }},
64 {'48', { 0, 255, 135 }},
65 {'49', { 0, 255, 175 }},
66 {'50', { 0, 255, 215 }},
67 {'51', { 0, 255, 255 }},
68 {'52', { 95, 0, 0 }},
69 {'53', { 95, 0, 95 }},
70 {'54', { 95, 0, 135 }},
71 {'55', { 95, 0, 175 }},
72 {'56', { 95, 0, 215 }},
73 {'57', { 95, 0, 255 }},
74 {'58', { 95, 95, 0 }},
75 {'59', { 95, 95, 95 }},
76 {'60', { 95, 95, 135 }},
77 {'61', { 95, 95, 175 }},
78 {'62', { 95, 95, 215 }},
79 {'63', { 95, 95, 255 }},
80 {'64', { 95, 135, 0 }},
81 {'65', { 95, 135, 95 }},
82 {'66', { 95, 135, 135 }},
83 {'67', { 95, 135, 175 }},
84 {'68', { 95, 135, 215 }},
85 {'69', { 95, 135, 255 }},
86 {'70', { 95, 175, 0 }},
87 {'71', { 95, 175, 95 }},
88 {'72', { 95, 175, 135 }},
89 {'73', { 95, 175, 175 }},
90 {'74', { 95, 175, 215 }},
91 {'75', { 95, 175, 255 }},
92 {'76', { 95, 215, 0 }},
93 {'77', { 95, 215, 95 }},
94 {'78', { 95, 215, 135 }},
95 {'79', { 95, 215, 175 }},
96 {'80', { 95, 215, 215 }},
97 {'81', { 95, 215, 255 }},
98 {'82', { 95, 255, 0 }},
99 {'83', { 95, 255, 95 }},
100 {'84', { 95, 255, 135 }},
101 {'85', { 95, 255, 175 }},
102 {'86', { 95, 255, 215 }},
103 {'87', { 95, 255, 255 }},
104 {'88', { 135, 0, 0 }},
105 {'89', { 135, 0, 95 }},
106 {'90', { 135, 0, 135 }},
107 {'91', { 135, 0, 175 }},
108 {'92', { 135, 0, 215 }},
109 {'93', { 135, 0, 255 }},
110 {'94', { 135, 95, 0 }},
111 {'95', { 135, 95, 95 }},
112 {'96', { 135, 95, 135 }},
113 {'97', { 135, 95, 175 }},
114 {'98', { 135, 95, 215 }},
115 {'99', { 135, 95, 255 }},
116 {'100', { 135, 135, 0 }},
117 {'101', { 135, 135, 95 }},
118 {'102', { 135, 135, 135 }},
119 {'103', { 135, 135, 175 }},
120 {'104', { 135, 135, 215 }},
121 {'105', { 135, 135, 255 }},
122 {'106', { 135, 175, 0 }},
123 {'107', { 135, 175, 95 }},
124 {'108', { 135, 175, 135 }},
125 {'109', { 135, 175, 175 }},
126 {'110', { 135, 175, 215 }},
127 {'111', { 135, 175, 255 }},
128 {'112', { 135, 215, 0 }},
129 {'113', { 135, 215, 95 }},
130 {'114', { 135, 215, 135 }},
131 {'115', { 135, 215, 175 }},
132 {'116', { 135, 215, 215 }},
133 {'117', { 135, 215, 255 }},
134 {'118', { 135, 255, 0 }},
135 {'119', { 135, 255, 95 }},
136 {'120', { 135, 255, 135 }},
137 {'121', { 135, 255, 175 }},
138 {'122', { 135, 255, 215 }},
139 {'123', { 135, 255, 255 }},
140 {'124', { 175, 0, 0 }},
141 {'125', { 175, 0, 95 }},
142 {'126', { 175, 0, 135 }},
143 {'127', { 175, 0, 175 }},
144 {'128', { 175, 0, 215 }},
145 {'129', { 175, 0, 255 }},
146 {'130', { 175, 95, 0 }},
147 {'131', { 175, 95, 95 }},
148 {'132', { 175, 95, 135 }},
149 {'133', { 175, 95, 175 }},
150 {'134', { 175, 95, 215 }},
151 {'135', { 175, 95, 255 }},
152 {'136', { 175, 135, 0 }},
153 {'137', { 175, 135, 95 }},
154 {'138', { 175, 135, 135 }},
155 {'139', { 175, 135, 175 }},
156 {'140', { 175, 135, 215 }},
157 {'141', { 175, 135, 255 }},
158 {'142', { 175, 175, 0 }},
159 {'143', { 175, 175, 95 }},
160 {'144', { 175, 175, 135 }},
161 {'145', { 175, 175, 175 }},
162 {'146', { 175, 175, 215 }},
163 {'147', { 175, 175, 255 }},
164 {'148', { 175, 215, 0 }},
165 {'149', { 175, 215, 95 }},
166 {'150', { 175, 215, 135 }},
167 {'151', { 175, 215, 175 }},
168 {'152', { 175, 215, 215 }},
169 {'153', { 175, 215, 255 }},
170 {'154', { 175, 255, 0 }},
171 {'155', { 175, 255, 95 }},
172 {'156', { 175, 255, 135 }},
173 {'157', { 175, 255, 175 }},
174 {'158', { 175, 255, 215 }},
175 {'159', { 175, 255, 255 }},
176 {'160', { 215, 0, 0 }},
177 {'161', { 215, 0, 95 }},
178 {'162', { 215, 0, 135 }},
179 {'163', { 215, 0, 175 }},
180 {'164', { 215, 0, 215 }},
181 {'165', { 215, 0, 255 }},
182 {'166', { 215, 95, 0 }},
183 {'167', { 215, 95, 95 }},
184 {'168', { 215, 95, 135 }},
185 {'169', { 215, 95, 175 }},
186 {'170', { 215, 95, 215 }},
187 {'171', { 215, 95, 255 }},
188 {'172', { 215, 135, 0 }},
189 {'173', { 215, 135, 95 }},
190 {'174', { 215, 135, 135 }},
191 {'175', { 215, 135, 175 }},
192 {'176', { 215, 135, 215 }},
193 {'177', { 215, 135, 255 }},
194 {'178', { 215, 175, 0 }},
195 {'179', { 215, 175, 95 }},
196 {'180', { 215, 175, 135 }},
197 {'181', { 215, 175, 175 }},
198 {'182', { 215, 175, 215 }},
199 {'183', { 215, 175, 255 }},
200 {'184', { 215, 215, 0 }},
201 {'185', { 215, 215, 95 }},
202 {'186', { 215, 215, 135 }},
203 {'187', { 215, 215, 175 }},
204 {'188', { 215, 215, 215 }},
205 {'189', { 215, 215, 255 }},
206 {'190', { 215, 255, 0 }},
207 {'191', { 215, 255, 95 }},
208 {'192', { 215, 255, 135 }},
209 {'193', { 215, 255, 175 }},
210 {'194', { 215, 255, 215 }},
211 {'195', { 215, 255, 255 }},
212 {'196', { 255, 0, 0 }},
213 {'197', { 255, 0, 95 }},
214 {'198', { 255, 0, 135 }},
215 {'199', { 255, 0, 175 }},
216 {'200', { 255, 0, 215 }},
217 {'201', { 255, 0, 255 }},
218 {'202', { 255, 95, 0 }},
219 {'203', { 255, 95, 95 }},
220 {'204', { 255, 95, 135 }},
221 {'205', { 255, 95, 175 }},
222 {'206', { 255, 95, 215 }},
223 {'207', { 255, 95, 255 }},
224 {'208', { 255, 135, 0 }},
225 {'209', { 255, 135, 95 }},
226 {'210', { 255, 135, 135 }},
227 {'211', { 255, 135, 175 }},
228 {'212', { 255, 135, 215 }},
229 {'213', { 255, 135, 255 }},
230 {'214', { 255, 175, 0 }},
231 {'215', { 255, 175, 95 }},
232 {'216', { 255, 175, 135 }},
233 {'217', { 255, 175, 175 }},
234 {'218', { 255, 175, 215 }},
235 {'219', { 255, 175, 255 }},
236 {'220', { 255, 215, 0 }},
237 {'221', { 255, 215, 95 }},
238 {'222', { 255, 215, 135 }},
239 {'223', { 255, 215, 175 }},
240 {'224', { 255, 215, 215 }},
241 {'225', { 255, 215, 255 }},
242 {'226', { 255, 255, 0 }},
243 {'227', { 255, 255, 95 }},
244 {'228', { 255, 255, 135 }},
245 {'229', { 255, 255, 175 }},
246 {'230', { 255, 255, 215 }},
247 {'231', { 255, 255, 255 }},
248
249 -- Gray-scale range.
250 {'232', { 8, 8, 8 }},
251 {'233', { 18, 18, 18 }},
252 {'234', { 28, 28, 28 }},
253 {'235', { 38, 38, 38 }},
254 {'236', { 48, 48, 48 }},
255 {'237', { 58, 58, 58 }},
256 {'238', { 68, 68, 68 }},
257 {'239', { 78, 78, 78 }},
258 {'240', { 88, 88, 88 }},
259 {'241', { 98, 98, 98 }},
260 {'242', { 108, 108, 108 }},
261 {'243', { 118, 118, 118 }},
262 {'244', { 128, 128, 128 }},
263 {'245', { 138, 138, 138 }},
264 {'246', { 148, 148, 148 }},
265 {'247', { 158, 158, 158 }},
266 {'248', { 168, 168, 168 }},
267 {'249', { 178, 178, 178 }},
268 {'250', { 188, 188, 188 }},
269 {'251', { 198, 198, 198 }},
270 {'252', { 208, 208, 208 }},
271 {'253', { 218, 218, 218 }},
272 {'254', { 228, 228, 228 }},
273 {'255', { 238, 238, 238 }},
274 }
275 -- stylua: ignore end
276
277 ---converts #rrggbb formatted color to cterm ('0'-'255') color
278 ---@param hex_color string
279 ---@return string
280 function M.rgb2cterm(hex_color)
281 if hex_color == 'None' then
282 return 'None'
283 end
284 local function get_color_distance(color1, color2)
285 -- returns how much color2 deviates from color1
286 local dr = math.abs(color1[1] - color2[1]) / (color1[1] + 1) * 100
287 local dg = math.abs(color1[2] - color2[2]) / (color1[2] + 1) * 100
288 local db = math.abs(color1[3] - color2[3]) / (color1[3] + 1) * 100
289 return (dr + dg + db)
290 end
291
292 local r = tonumber(hex_color:sub(2, 3), 16)
293 local g = tonumber(hex_color:sub(4, 5), 16)
294 local b = tonumber(hex_color:sub(6, 7), 16)
295
296 -- check which cterm color is closest to hex colors in terms of rgb values
297 local closest_cterm_color = 0
298 local min_distance = 10000
299 for _, color in ipairs(color_table) do
300 local current_distance = get_color_distance(color[2], { r, g, b })
301 if current_distance < min_distance then
302 min_distance = current_distance
303 closest_cterm_color = color[1]
304 end
305 end
306 return closest_cterm_color
307 end
308
309 ---converts color name (only ones supported by neovim) formatted colors to #rrggbb
310 ---@param name string like red,green,grey
311 ---@return string
312 function M.color_name2rgb(name)
313 local color_val = vim.api.nvim_get_color_by_name(name)
314 if color_val == -1 then
315 return '#' .. name -- Assuming it's 'rrggbb' without # not rad instead of red
316 end
317 return string.format('#%06x', color_val)
318 end
319
320 ---converts cterm(0-255) to #rrggbb
321 ---@param color number
322 ---@return string
323 function M.cterm2rgb(color)
324 local color_data = color_table[color + 1]
325 if color_data ~= nil then
326 color_data = color_data[2]
327 return string.format('#%02x%02x%02x', color_data[1], color_data[2], color_data[3])
328 end
329 end
330
331 ---converts cterm, color_name type colors to #rrggbb format
332 ---@param color string|number
333 ---@return string
334 function M.sanitize_color(color)
335 if color == nil or color == '' or (type(color) == 'string' and color:lower() == 'none') then
336 return 'None'
337 end
338 if type(color) == 'string' then
339 if color:sub(1, 1) == '#' then
340 return color
341 end -- RGB value
342 return M.color_name2rgb(color)
343 elseif type(color) == 'number' then
344 if color > 255 then
345 error("What's this it can't be higher then 255 and you've given " .. color)
346 end
347 return M.cterm2rgb(color)
348 end
349 end
350
351 ---converts color_name type colors to cterm format and let cterm color pass through
352 ---@param color string|number
353 ---@return string
354 function M.sanitize_color_for_cterm(color)
355 if type(color) == 'number' then
356 if color > 255 then
357 error("What's this it can't be higher then 255 and you've given " .. color)
358 end
359 return color
360 end
361 return M.rgb2cterm(M.sanitize_color(color))
362 end
363
364 return M