Skip to content

Commit 32c27b1

Browse files
committed
refactor: generate icons from their glyph names
1 parent 69db92e commit 32c27b1

File tree

6 files changed

+336
-298
lines changed

6 files changed

+336
-298
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.lua
22
.luarocks
3+
/nerd-fonts/
34
/vim-colortemplate/

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
NERD_FONTS_VERSION = 3.1.1
12
VIM_COLORTEMPLATE_VERSION = 2.2.3
23

34
all: colors style-check lint
45

5-
colors: vim-colortemplate
6+
colors: nerd-fonts vim-colortemplate
67
nvim \
78
--clean \
89
--headless \
@@ -18,6 +19,9 @@ colors-check: colors
1819
vim-colortemplate:
1920
git clone --depth 1 -b v$(VIM_COLORTEMPLATE_VERSION) https://github.com/lifepillar/vim-colortemplate.git vim-colortemplate
2021

22+
nerd-fonts:
23+
git clone --depth 1 --filter blob:none --sparse -b v$(NERD_FONTS_VERSION) https://github.com/ryanoasis/nerd-fonts.git nerd-fonts
24+
2125
style-check:
2226
stylua . --check
2327

@@ -28,6 +32,6 @@ lint:
2832
luacheck lua
2933

3034
clean:
31-
rm -rf vim-colortemplate
35+
rm -rf nerd-fonts vim-colortemplate
3236

3337
.PHONY: all colors style-check style-fix lint

lua/nvim-web-devicons/_gen/color.lua

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
local M = {}
2+
3+
---@param rrggbb string
4+
---@return string
5+
function M.darken(rrggbb)
6+
local light78 = 255 * 7 / 8
7+
local light68 = 255 * 6 / 8
8+
local light58 = 255 * 5 / 8
9+
local light12 = 255 / 2
10+
local light13 = 255 / 3
11+
12+
local hex = bit.tohex ---@type fun(n: number): string
13+
14+
local r, g, b = rrggbb:match "%#(%x%x)(%x%x)(%x%x)"
15+
r, g, b = tonumber("0x" .. r), tonumber("0x" .. g), tonumber("0x" .. b)
16+
-- luminance formula: see https://stackoverflow.com/a/596243
17+
local lum = 0.299 * r + 0.587 * g + 0.114 * b
18+
if lum < light13 then -------------------- darkest tertile
19+
return rrggbb
20+
elseif lum < light12 then ---------------- second darkest quartile
21+
r = hex(r / 4 * 3):sub(-2)
22+
g = hex(g / 4 * 3):sub(-2)
23+
b = hex(b / 4 * 3):sub(-2)
24+
elseif lum < light58 then ---------------- lightest octiles: first
25+
r = hex(r / 3 * 2):sub(-2)
26+
g = hex(g / 3 * 2):sub(-2)
27+
b = hex(b / 3 * 2):sub(-2)
28+
elseif lum < light68 then ---------------- lightest octiles: second
29+
r = hex(r / 2):sub(-2)
30+
g = hex(g / 2):sub(-2)
31+
b = hex(b / 2):sub(-2)
32+
elseif lum < light78 then ---------------- lightest octiles: third
33+
r = hex(r / 3):sub(-2)
34+
g = hex(g / 3):sub(-2)
35+
b = hex(b / 3):sub(-2)
36+
else ------------------------------------- lightest octile
37+
r = hex(r / 5):sub(-2)
38+
g = hex(g / 5):sub(-2)
39+
b = hex(b / 5):sub(-2)
40+
end
41+
return string.format("#%s%s%s", r, g, b)
42+
end
43+
44+
return M

0 commit comments

Comments
 (0)