Skip to content

Commit

Permalink
refactor: generate icons from their glyph names
Browse files Browse the repository at this point in the history
  • Loading branch information
futsuuu committed Mar 15, 2024
1 parent 69db92e commit 32c27b1
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 298 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.lua
.luarocks
/nerd-fonts/
/vim-colortemplate/
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
NERD_FONTS_VERSION = 3.1.1
VIM_COLORTEMPLATE_VERSION = 2.2.3

all: colors style-check lint

colors: vim-colortemplate
colors: nerd-fonts vim-colortemplate
nvim \
--clean \
--headless \
Expand All @@ -18,6 +19,9 @@ colors-check: colors
vim-colortemplate:
git clone --depth 1 -b v$(VIM_COLORTEMPLATE_VERSION) https://github.com/lifepillar/vim-colortemplate.git vim-colortemplate

nerd-fonts:
git clone --depth 1 --filter blob:none --sparse -b v$(NERD_FONTS_VERSION) https://github.com/ryanoasis/nerd-fonts.git nerd-fonts

style-check:
stylua . --check

Expand All @@ -28,6 +32,6 @@ lint:
luacheck lua

clean:
rm -rf vim-colortemplate
rm -rf nerd-fonts vim-colortemplate

.PHONY: all colors style-check style-fix lint
44 changes: 44 additions & 0 deletions lua/nvim-web-devicons/_gen/color.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local M = {}

---@param rrggbb string
---@return string
function M.darken(rrggbb)
local light78 = 255 * 7 / 8
local light68 = 255 * 6 / 8
local light58 = 255 * 5 / 8
local light12 = 255 / 2
local light13 = 255 / 3

local hex = bit.tohex ---@type fun(n: number): string

local r, g, b = rrggbb:match "%#(%x%x)(%x%x)(%x%x)"
r, g, b = tonumber("0x" .. r), tonumber("0x" .. g), tonumber("0x" .. b)
-- luminance formula: see https://stackoverflow.com/a/596243
local lum = 0.299 * r + 0.587 * g + 0.114 * b
if lum < light13 then -------------------- darkest tertile
return rrggbb
elseif lum < light12 then ---------------- second darkest quartile
r = hex(r / 4 * 3):sub(-2)
g = hex(g / 4 * 3):sub(-2)
b = hex(b / 4 * 3):sub(-2)
elseif lum < light58 then ---------------- lightest octiles: first
r = hex(r / 3 * 2):sub(-2)
g = hex(g / 3 * 2):sub(-2)
b = hex(b / 3 * 2):sub(-2)
elseif lum < light68 then ---------------- lightest octiles: second
r = hex(r / 2):sub(-2)
g = hex(g / 2):sub(-2)
b = hex(b / 2):sub(-2)
elseif lum < light78 then ---------------- lightest octiles: third
r = hex(r / 3):sub(-2)
g = hex(g / 3):sub(-2)
b = hex(b / 3):sub(-2)
else ------------------------------------- lightest octile
r = hex(r / 5):sub(-2)
g = hex(g / 5):sub(-2)
b = hex(b / 5):sub(-2)
end
return string.format("#%s%s%s", r, g, b)
end

return M
Loading

0 comments on commit 32c27b1

Please sign in to comment.