dotfiles/config/nvim/lua/kolir.lua (view raw)
| 1 | local function hex_to_rgb(hex) |
| 2 | hex = hex:gsub("#", "") |
| 3 | return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16) |
| 4 | end |
| 5 | |
| 6 | local function rgb_to_hex(r, g, b) |
| 7 | return string.format("#%02x%02x%02x", |
| 8 | math.min(255, math.max(0, r)), |
| 9 | math.min(255, math.max(0, g)), |
| 10 | math.min(255, math.max(0, b))) |
| 11 | end |
| 12 | |
| 13 | local function rgb_to_hsl(r, g, b) |
| 14 | r, g, b = r / 255, g / 255, b / 255 |
| 15 | local max, min = math.max(r, g, b), math.min(r, g, b) |
| 16 | local l = (max + min) / 2 |
| 17 | if max == min then return 0, 0, l end |
| 18 | |
| 19 | local d = max - min |
| 20 | local s = l > 0.5 and d / (2 - max - min) or d / (max + min) |
| 21 | local h |
| 22 | if max == r then |
| 23 | h = (g - b) / d + (g < b and 6 or 0) |
| 24 | elseif max == g then |
| 25 | h = (b - r) / d + 2 |
| 26 | else |
| 27 | h = (r - g) / d + 4 |
| 28 | end |
| 29 | return h / 6, s, l |
| 30 | end |
| 31 | |
| 32 | local function hsl_to_rgb(h, s, l) |
| 33 | if s == 0 then |
| 34 | local v = math.floor(l * 255) |
| 35 | return v, v, v |
| 36 | end |
| 37 | |
| 38 | local function f(p, q, t) |
| 39 | if t < 0 then t = t + 1 end |
| 40 | if t > 1 then t = t - 1 end |
| 41 | if t < 1 / 6 then return p + (q - p) * 6 * t end |
| 42 | if t < 1 / 2 then return q end |
| 43 | if t < 2 / 3 then return p + (q - p) * (2 / 3 - t) * 6 end |
| 44 | return p |
| 45 | end |
| 46 | |
| 47 | local q = l < 0.5 and l * (1 + s) or l + s - l * s |
| 48 | local p = 2 * l - q |
| 49 | return math.floor(f(p, q, h + 1 / 3) * 255), math.floor(f(p, q, h) * 255), math.floor(f(p, q, h - 1 / 3) * 255) |
| 50 | end |
| 51 | |
| 52 | local function adjust_hsl(hex, dh, ds, dl) |
| 53 | local h, s, l = rgb_to_hsl(hex_to_rgb(hex)) |
| 54 | return rgb_to_hex(hsl_to_rgb((h + (dh or 0)) % 1, math.min(1, math.max(0, s + (ds or 0))), |
| 55 | math.min(1, math.max(0, l + (dl or 0))))) |
| 56 | end |
| 57 | |
| 58 | -- Blend two colors in RGB space (semantic -> background-safe) |
| 59 | local function mix(hex_a, hex_b, t) |
| 60 | local ar, ag, ab = hex_to_rgb(hex_a) |
| 61 | local br, bg, bb = hex_to_rgb(hex_b) |
| 62 | return rgb_to_hex(math.floor(ar + (br - ar) * t), math.floor(ag + (bg - ag) * t), math.floor(ab + (bb - ab) * t)) |
| 63 | end |
| 64 | |
| 65 | local function hl(g, o) vim.api.nvim_set_hl(0, g, o) end |
| 66 | |
| 67 | return function(theme) |
| 68 | local is_dark = vim.o.background == "dark" |
| 69 | |
| 70 | -- Background shading |
| 71 | theme.bg_alt = adjust_hsl(theme.bg, 0, 0, is_dark and 0.070 or -0.050) |
| 72 | theme.bg_dim = adjust_hsl(theme.bg, 0, 0, is_dark and -0.024 or -0.032) |
| 73 | theme.fg_dim = mix(theme.fg, theme.bg, is_dark and 0.45 or 0.20) |
| 74 | |
| 75 | -- Semantic surfaces, Accent colors are *pulled toward background*, not crushed in lightness |
| 76 | theme.highlight_bg = mix(theme.primary, theme.bg, is_dark and 0.58 or 0.45) |
| 77 | theme.positive_bg = mix(theme.secondary, theme.bg, is_dark and 0.75 or 0.30) |
| 78 | theme.negative_bg = mix(theme.muted, theme.bg, is_dark and 0.75 or 0.35) |
| 79 | theme.danger_bg = mix(theme.danger, theme.bg, is_dark and 0.75 or 0.40) |
| 80 | |
| 81 | theme.black = mix(theme.bg, "#000000", is_dark and 0.20 or 0.50) |
| 82 | theme.fg_gutter = mix(mix(theme.fg, theme.primary, 0.30), theme.bg, is_dark and 0.60 or 0.55) |
| 83 | theme.nontext = mix(theme.fg, theme.bg, is_dark and 0.74 or 0.35) |
| 84 | |
| 85 | -- highlights |
| 86 | hl("Normal", { fg = theme.fg, bg = theme.bg }) |
| 87 | hl("NormalFloat", { fg = theme.fg, bg = theme.bg_dim }) |
| 88 | hl("FloatBorder", { bg = theme.bg_dim, fg = theme.primary }) |
| 89 | hl("EndOfBuffer", { fg = theme.bg }) |
| 90 | hl("Cursor", { reverse = true }) |
| 91 | hl("CursorLine", { bg = theme.bg_alt }) |
| 92 | hl("CursorLineNr", { bold = true, fg = theme.marker }) |
| 93 | hl("SignColumn", { bg = theme.bg, fg = theme.fg_dim }) |
| 94 | hl("LineNr", { fg = theme.fg_gutter }) |
| 95 | hl("FloatTitle", { bg = theme.bg_dim, fg = theme.primary, bold = true }) |
| 96 | hl("Title", { fg = theme.primary, bold = true }) |
| 97 | |
| 98 | hl("ErrorMsg", { fg = theme.danger }) |
| 99 | hl("WarningMsg", { fg = theme.marker }) |
| 100 | hl("ModeMsg", { bold = true, fg = theme.fg }) |
| 101 | hl("MoreMsg", { fg = theme.fg }) |
| 102 | hl("Question", { fg = theme.fg }) |
| 103 | |
| 104 | hl("Pmenu", { bg = theme.bg_dim, fg = theme.fg }) |
| 105 | hl("PmenuSel", { bg = theme.bg_alt, fg = theme.fg }) |
| 106 | hl("PmenuSbar", { bg = theme.bg_alt }) |
| 107 | hl("PmenuThumb", { bg = theme.fg_dim }) |
| 108 | hl("WildMenu", { bg = theme.bg_alt, fg = theme.fg }) |
| 109 | hl("WinSeparator", { bold = true, fg = theme.black }) |
| 110 | hl("VertSplit", { fg = theme.black }) |
| 111 | hl("Search", { bg = theme.highlight_bg, fg = theme.fg }) |
| 112 | hl("IncSearch", { bg = theme.marker, fg = theme.bg }) |
| 113 | hl("Visual", { bg = theme.negative_bg }) |
| 114 | |
| 115 | hl("MatchParen", { bold = true, fg = theme.marker }) |
| 116 | hl("Directory", { fg = theme.primary }) |
| 117 | hl("NonText", { fg = theme.nontext }) |
| 118 | hl("Folded", { bg = theme.bg_dim, fg = theme.fg_dim, italic = true }) |
| 119 | hl("SpecialKey", { link = "NonText" }) |
| 120 | hl("StatusLine", { bg = theme.bg_dim, fg = theme.fg }) |
| 121 | hl("StatusLineNC", { bg = theme.bg_dim, fg = theme.fg_dim }) |
| 122 | hl("TabLine", { bg = theme.bg_dim, fg = theme.fg_dim }) |
| 123 | hl("TabLineFill", { bg = theme.bg_dim, fg = theme.fg }) |
| 124 | hl("TabLineSel", { bg = theme.primary, fg = theme.bg }) |
| 125 | |
| 126 | hl("Comment", { fg = theme.marker, italic = true }) |
| 127 | hl("SpecialComment", { fg = theme.marker, bold = true }) |
| 128 | hl("Debug", { fg = theme.muted }) |
| 129 | hl("Exception", { fg = theme.muted }) |
| 130 | hl("Label", { fg = theme.muted }) |
| 131 | hl("Repeat", { fg = theme.muted }) |
| 132 | hl("Todo", { fg = theme.bg, bg = theme.marker, bold = true }) |
| 133 | |
| 134 | hl("Keyword", { fg = theme.muted, italic = true }) |
| 135 | hl("Conditional", { fg = theme.muted }) |
| 136 | hl("Statement", { fg = theme.muted }) |
| 137 | hl("Include", { fg = theme.muted }) |
| 138 | hl("PreCondit", { fg = theme.muted }) |
| 139 | hl("PreProc", { fg = theme.muted }) |
| 140 | hl("StorageClass", { fg = theme.muted }) |
| 141 | hl("Operator", { fg = theme.fg }) |
| 142 | hl("Delimiter", { fg = theme.fg_dim }) |
| 143 | hl("Identifier", { fg = theme.fg }) |
| 144 | hl("Error", { fg = theme.danger }) |
| 145 | hl("SpecialChar", { fg = theme.primary }) |
| 146 | hl("Special", { fg = theme.marker }) |
| 147 | |
| 148 | hl("Constant", { fg = theme.fg }) |
| 149 | hl("String", { fg = theme.secondary, italic = true }) |
| 150 | hl("Boolean", { fg = theme.marker }) |
| 151 | hl("Number", { fg = theme.marker }) |
| 152 | hl("Float", { fg = theme.marker }) |
| 153 | hl("Character", { fg = theme.muted }) |
| 154 | |
| 155 | hl("Function", { fg = theme.primary }) |
| 156 | hl("Type", { fg = theme.primary }) |
| 157 | hl("Structure", { fg = theme.primary }) |
| 158 | hl("Typedef", { fg = theme.primary }) |
| 159 | hl("Tag", { fg = theme.primary }) |
| 160 | hl("Define", { fg = theme.primary }) |
| 161 | hl("Macro", { fg = theme.primary }) |
| 162 | |
| 163 | hl("QuickFixLine", { bg = theme.bg_alt, bold = true }) |
| 164 | hl("qfFileName", { fg = theme.primary }) |
| 165 | hl("qfLineNr", { fg = theme.fg_gutter }) |
| 166 | |
| 167 | hl("DiffAdd", { bg = theme.positive_bg, fg = theme.secondary }) |
| 168 | hl("DiffChange", { bg = theme.highlight_bg, fg = theme.primary }) |
| 169 | hl("DiffDelete", { bg = theme.danger_bg, fg = theme.danger }) |
| 170 | hl("DiffText", { bg = theme.bg_alt, fg = theme.primary, bold = true }) |
| 171 | hl("diffAdded", { fg = theme.secondary }) |
| 172 | hl("diffChanged", { fg = theme.primary }) |
| 173 | hl("diffRemoved", { fg = theme.danger }) |
| 174 | |
| 175 | hl("DiagnosticSignError", { fg = theme.danger }) |
| 176 | hl("DiagnosticSignWarn", { fg = theme.marker }) |
| 177 | hl("DiagnosticSignInfo", { fg = theme.primary }) |
| 178 | hl("DiagnosticSignHint", { fg = theme.muted }) |
| 179 | hl("DiagnosticError", { link = "DiagnosticSignError" }) |
| 180 | hl("DiagnosticWarn", { link = "DiagnosticSignWarn" }) |
| 181 | hl("DiagnosticInfo", { link = "DiagnosticSignInfo" }) |
| 182 | hl("DiagnosticHint", { link = "DiagnosticSignHint" }) |
| 183 | |
| 184 | -- treesitter |
| 185 | hl("@module", { fg = theme.fg }) |
| 186 | hl("@module.builtin", { link = "@module" }) |
| 187 | hl("@variable", { fg = theme.fg }) |
| 188 | hl("@field", { link = "@variable" }) |
| 189 | hl("@property", { link = "@variable" }) |
| 190 | hl("@constant.builtin", { fg = theme.marker }) |
| 191 | hl("@keyword.return", { fg = theme.muted, italic = true }) |
| 192 | hl("@constructor", { link = "Function" }) |
| 193 | hl("@constructor.lua", { link = "@punctuation.bracket" }) |
| 194 | hl("@function.builtin", { link = "Function" }) |
| 195 | hl("@type.builtin", { link = "Type" }) |
| 196 | hl("@markup.italic", { italic = true }) |
| 197 | |
| 198 | hl("@punctuation.delimiter", { fg = theme.fg }) |
| 199 | hl("@punctuation.bracket", { fg = theme.fg_dim }) |
| 200 | hl("@punctuation.special", { fg = theme.fg_dim }) |
| 201 | |
| 202 | -- plugins |
| 203 | hl("SnacksPickerDir", { fg = theme.fg_gutter }) |
| 204 | hl("SnacksPickerPathHidden", { fg = theme.fg_gutter }) |
| 205 | hl("SnacksPickerPathIgnored", { fg = theme.fg_gutter }) |
| 206 | end |