diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9760027..c4a495c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,30 +6,76 @@ on: pull_request: branches: [main] +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + jobs: check: runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + neovim: [v0.11.7, v0.12.6] steps: - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 with: neovim: true - version: v0.12.0 - - name: Validate config loads without errors - run: nvim --headless -c "lua vim.cmd('q!')" -c "qa!" + version: ${{ matrix.neovim }} + + - name: Use the repository config + run: | + mkdir -p "$HOME/.config" + ln -sfn "$GITHUB_WORKSPACE" "$HOME/.config/nvim" + + - name: Check palettes and contrast + run: nvim --headless -u NONE --cmd "set rtp+=." -l scripts/check_colors.lua + + - name: Load core config modules + run: | + nvim --headless -u NONE --cmd "set rtp+=." \ + -c "lua for _, m in ipairs({ 'config.options', 'config.locale', 'config.keymaps', 'config.autocmds', 'config.theme' }) do require(m) end" \ + -c "qa!" + - name: Check Lua syntax run: | - for f in $(find lua colors -name '*.lua' -not -path '*/lazy-lock.json'); do - nvim --headless -c "lua local ok, err = loadfile('$f'); if not ok then io.stderr:write(err..'\n'); vim.cmd('cquit!') else vim.cmd('q!') end" 2>/dev/null || exit 1 + for f in $(find lua colors scripts -name '*.lua' -not -path '*/lazy-lock.json'); do + nvim --headless -u NONE -c "lua local ok, err = loadfile('$f'); if not ok then io.stderr:write(err..'\n'); vim.cmd('cquit!') else vim.cmd('q!') end" 2>/dev/null || exit 1 done + + - name: Install plugins and validate startup + run: | + timeout 300 nvim --headless "+Lazy! sync" +qa + nvim --headless -c "lua vim.cmd('q!')" -c "qa!" + lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Install stylua run: | curl -sL https://github.com/JohnnyMorganz/StyLua/releases/download/v2.5.2/stylua-linux-x86_64.zip -o stylua.zip - unzip -o stylua.zip && rm stylua.zip - chmod +x stylua && sudo mv stylua /usr/local/bin/ + python3 -c "import zipfile; zipfile.ZipFile('stylua.zip').extractall('.')" + rm stylua.zip + chmod +x stylua + sudo mv stylua /usr/local/bin/ + - name: Check Lua formatting - run: stylua --check lua/ + run: stylua --check lua/ colors/ scripts/ + + - name: Install selene + run: | + curl -sL https://github.com/Kampfkarren/selene/releases/download/0.32.0/selene-0.32.0-linux.zip -o selene.zip + python3 -c "import zipfile; zipfile.ZipFile('selene.zip').extractall('.')" + rm selene.zip + chmod +x selene + + - name: Lint with selene + run: ./selene . diff --git a/README.md b/README.md index 5dd3886..dc95cea 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ vim.g.langmap_en = "" Dev & -github +Github & X diff --git a/lua/themes/colors.lua b/lua/themes/colors.lua new file mode 100644 index 0000000..2567165 --- /dev/null +++ b/lua/themes/colors.lua @@ -0,0 +1,85 @@ +local M = {} + +function M.strip_alpha(hex) + if #hex == 9 then + return hex:sub(1, 7) + end + return hex +end + +local function hex_to_rgb(hex) + hex = M.strip_alpha(hex):gsub("#", "") + return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16) +end + +function M.luminance(hex) + local r, g, b = hex_to_rgb(hex) + local function channel(value) + value = value / 255 + if value <= 0.03928 then + return value / 12.92 + end + return ((value + 0.055) / 1.055) ^ 2.4 + end + return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b) +end + +function M.is_light(hex) + return M.luminance(hex) > 0.6 +end + +function M.contrast(a, b) + local left = M.luminance(a) + local right = M.luminance(b) + local lighter = math.max(left, right) + local darker = math.min(left, right) + return (lighter + 0.05) / (darker + 0.05) +end + +function M.blend(from, to, percent) + local fr, fg, fb = hex_to_rgb(from) + local tr, tg, tb = hex_to_rgb(to) + local function channel(a, b) + return math.floor(a + (b - a) * percent + 0.5) + end + return string.format("#%02x%02x%02x", channel(fr, tr), channel(fg, tg), channel(fb, tb)) +end + +-- Raise a colour's contrast against the background until it reaches `minimum`. +-- This keeps the palette identity for colours that are already readable and +-- only nudges the ones that would otherwise disappear (for example Paris +-- comments, whose color7 is the same as its background). +function M.readable(colour, background, minimum) + if M.contrast(colour, background) >= minimum then + return M.strip_alpha(colour) + end + + local target = M.is_light(background) and "#000000" or "#ffffff" + for percent = 0.1, 1.0, 0.1 do + local candidate = M.blend(colour, target, percent) + if M.contrast(candidate, background) >= minimum then + return candidate + end + end + return target +end + +function M.surface(palette) + return M.blend(palette.background, palette.foreground, 0.1) +end + +function M.comment(palette) + local background = palette.background + local candidates = { palette.color7, palette.color8, palette.foreground } + if palette.comment then + table.insert(candidates, 1, palette.comment) + end + for _, candidate in ipairs(candidates) do + if candidate and M.contrast(candidate, background) >= 3.0 then + return M.strip_alpha(candidate) + end + end + return M.readable(palette.color7 or palette.foreground, background, 3.0) +end + +return M diff --git a/lua/themes/init.lua b/lua/themes/init.lua index f07beb9..62fd15c 100644 --- a/lua/themes/init.lua +++ b/lua/themes/init.lua @@ -1,26 +1,16 @@ +local colors = require("themes.colors") local palettes = require("themes.palettes") local M = {} -local function hex_to_rgb(hex) - hex = palettes.strip_alpha(hex):gsub("#", "") - return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16) -end - -local function is_light(hex) - local r, g, b = hex_to_rgb(hex) - local luma = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255 - return luma > 0.6 -end - local function set_hl(group, opts) vim.api.nvim_set_hl(0, group, opts) end function M.apply(name) local p = palettes[name] - if not p then - return + if type(p) ~= "table" then + return false end if vim.g.colors_name then @@ -30,28 +20,35 @@ function M.apply(name) vim.cmd("syntax reset") vim.o.termguicolors = true - vim.o.background = is_light(p.background) and "light" or "dark" + vim.o.background = colors.is_light(p.background) and "light" or "dark" vim.g.colors_name = name + local bg = colors.strip_alpha(p.background) + local fg = colors.strip_alpha(p.foreground) + local function readable(colour) + return colors.readable(colour, bg, 4.5) + end + local c = { - bg = palettes.strip_alpha(p.background), - fg = palettes.strip_alpha(p.foreground), - bg_alt = palettes.strip_alpha(p.color8), - fg_alt = palettes.strip_alpha(p.color15), - comment = palettes.strip_alpha(p.color7), - red = palettes.strip_alpha(p.color1), - green = palettes.strip_alpha(p.color2), - yellow = palettes.strip_alpha(p.color3), - orange = palettes.strip_alpha(p.color4), - purple = palettes.strip_alpha(p.color5), - blue = palettes.strip_alpha(p.color6), - cyan = palettes.strip_alpha(p.color6), - bright_red = palettes.strip_alpha(p.color9), - bright_green = palettes.strip_alpha(p.color10), - bright_yellow = palettes.strip_alpha(p.color11), - bright_orange = palettes.strip_alpha(p.color12), - bright_purple = palettes.strip_alpha(p.color13), - bright_cyan = palettes.strip_alpha(p.color14), + bg = bg, + fg = fg, + bg_alt = colors.surface(p), + surface_fg = colors.readable(fg, colors.surface(p), 4.5), + fg_alt = colors.readable(p.color15, colors.surface(p), 4.5), + comment = colors.comment(p), + red = readable(p.color1), + green = readable(p.color2), + yellow = readable(p.color3), + orange = readable(p.color4), + purple = readable(p.color5), + blue = readable(p.color6), + cyan = readable(p.color6), + bright_red = readable(p.color9), + bright_green = readable(p.color10), + bright_yellow = readable(p.color11), + bright_orange = readable(p.color12), + bright_purple = readable(p.color13), + bright_cyan = readable(p.color14), } set_hl("Normal", { fg = c.fg, bg = c.bg }) @@ -64,7 +61,7 @@ function M.apply(name) set_hl("VertSplit", { fg = c.bg_alt }) set_hl("WinSeparator", { fg = c.bg_alt }) - set_hl("Pmenu", { fg = c.fg, bg = c.bg_alt }) + set_hl("Pmenu", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("PmenuSel", { fg = c.bg, bg = c.bright_cyan, bold = true }) set_hl("PmenuSbar", { bg = c.bg_alt }) set_hl("PmenuThumb", { bg = c.comment }) @@ -76,7 +73,7 @@ function M.apply(name) set_hl("IncSearch", { fg = c.bg, bg = c.bright_orange }) set_hl("MatchParen", { fg = c.bright_cyan, bold = true }) - set_hl("StatusLine", { fg = c.fg, bg = c.bg_alt }) + set_hl("StatusLine", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("StatusLineNC", { fg = c.comment, bg = c.bg_alt }) set_hl("TabLine", { fg = c.comment, bg = c.bg_alt }) set_hl("TabLineSel", { fg = c.fg, bg = c.bg }) @@ -183,9 +180,40 @@ function M.apply(name) set_hl("@tag.attribute", { fg = c.yellow }) set_hl("@tag.delimiter", { fg = c.comment }) + -- Captures added/moved in recent Neovim releases. Unknown groups are + -- harmless on older versions, but having them keeps syntax highlighting + -- consistent across supported versions. + set_hl("@keyword.import", { link = "Keyword" }) + set_hl("@keyword.type", { link = "Type" }) + set_hl("@keyword.modifier", { link = "Keyword" }) + set_hl("@keyword.coroutine", { link = "Keyword" }) + set_hl("@function.method", { link = "Function" }) + set_hl("@function.method.call", { link = "Function" }) + set_hl("@type.qualifier", { link = "Type" }) + set_hl("@variable.member", { fg = c.cyan }) + set_hl("@variable.parameter", { fg = c.fg }) + set_hl("@string.documentation", { link = "String" }) + set_hl("@string.special.url", { fg = c.cyan, underline = true }) + set_hl("@comment.documentation", { link = "Comment" }) + set_hl("@markup.link.label", { fg = c.cyan }) + set_hl("@markup.link.url", { fg = c.cyan, underline = true }) + set_hl("@markup.raw.block", { fg = c.green }) + + set_hl("VisualNOS", { bg = c.bright_cyan, fg = c.bg }) + set_hl("Cursor", { fg = c.bg, bg = c.fg }) + set_hl("lCursor", { fg = c.bg, bg = c.fg }) + set_hl("TermCursor", { fg = c.bg, bg = c.fg }) + set_hl("TermCursorNC", { fg = c.bg, bg = c.comment }) + set_hl("WinBar", { fg = c.surface_fg, bg = c.bg_alt }) + set_hl("WinBarNC", { fg = c.comment, bg = c.bg_alt }) + set_hl("PmenuMatch", { fg = c.bright_cyan, bold = true }) + set_hl("PmenuMatchSel", { fg = c.bg, bg = c.bright_cyan, bold = true }) + set_hl("DiagnosticUnnecessary", { fg = c.comment }) + set_hl("DiagnosticDeprecated", { fg = c.red, strikethrough = true }) + set_hl("TelescopeBorder", { fg = c.comment, bg = c.bg }) set_hl("TelescopePromptBorder", { fg = c.comment, bg = c.bg_alt }) - set_hl("TelescopePromptNormal", { fg = c.fg, bg = c.bg_alt }) + set_hl("TelescopePromptNormal", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("TelescopePromptPrefix", { fg = c.cyan, bg = c.bg_alt }) set_hl("GitSignsAdd", { fg = c.green }) @@ -194,12 +222,12 @@ function M.apply(name) set_hl("BufferLineFill", { bg = c.bg_alt }) set_hl("BufferLineBackground", { fg = c.comment, bg = c.bg_alt }) - set_hl("BufferLineBufferVisible", { fg = c.fg, bg = c.bg_alt }) + set_hl("BufferLineBufferVisible", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("BufferLineBufferSelected", { fg = c.fg, bg = c.bg }) - set_hl("NoiceCmdlinePopup", { fg = c.fg, bg = c.bg_alt }) + set_hl("NoiceCmdlinePopup", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("NoiceCmdlinePopupBorder", { fg = c.cyan, bg = c.bg_alt }) - set_hl("NoicePopup", { fg = c.fg, bg = c.bg_alt }) + set_hl("NoicePopup", { fg = c.surface_fg, bg = c.bg_alt }) set_hl("NotifyERROR", { fg = c.red }) set_hl("NotifyWARN", { fg = c.yellow }) @@ -226,7 +254,10 @@ function M.apply(name) set_hl("CmpItemKind", { fg = c.purple }) set_hl("CmpItemMenu", { fg = c.comment }) - vim.g.terminal_color_0 = c.bg + -- ANSI slots 1-6 and 9-14 keep the canonical palette. Slots 7, 8 and 15 use + -- the readable foreground/comment colours because several palettes (notably + -- Paris) define color7 as the background itself. + vim.g.terminal_color_0 = colors.strip_alpha(p.color0) vim.g.terminal_color_1 = c.red vim.g.terminal_color_2 = c.green vim.g.terminal_color_3 = c.yellow @@ -242,12 +273,24 @@ function M.apply(name) vim.g.terminal_color_13 = c.bright_purple vim.g.terminal_color_14 = c.bright_cyan vim.g.terminal_color_15 = c.fg_alt + + return true end -M.names = vim.tbl_keys(palettes) +M.names = {} +for name, palette in pairs(palettes) do + if type(palette) == "table" then + table.insert(M.names, name) + end +end +table.sort(M.names) vim.api.nvim_create_user_command("Theme", function(opts) - M.apply(opts.args) + if not M.apply(opts.args) then + vim.notify(("Unknown theme %q. Available: %s"):format(opts.args, table.concat(M.names, ", ")), vim.log.levels.ERROR) + return + end + local lualine_ok, lualine = pcall(require, "lualine") if lualine_ok then lualine.setup({ options = { theme = require("themes.lualine").theme(opts.args) } }) diff --git a/lua/themes/lualine.lua b/lua/themes/lualine.lua index 753356e..bf45cba 100644 --- a/lua/themes/lualine.lua +++ b/lua/themes/lualine.lua @@ -1,3 +1,4 @@ +local colors = require("themes.colors") local palettes = require("themes.palettes") local M = {} @@ -5,50 +6,56 @@ local M = {} function M.theme(name) name = name or vim.g.colors_name or "x" local p = palettes[name] - if not p then + if type(p) ~= "table" then p = palettes.x end + local bg = colors.strip_alpha(p.background) + local function readable(colour) + return colors.readable(colour, bg, 4.5) + end + local c = { - bg = palettes.strip_alpha(p.background), - fg = palettes.strip_alpha(p.foreground), - bg_alt = palettes.strip_alpha(p.color8), - comment = palettes.strip_alpha(p.color8), - red = palettes.strip_alpha(p.color1), - green = palettes.strip_alpha(p.color2), - yellow = palettes.strip_alpha(p.color3), - cyan = palettes.strip_alpha(p.color6), + bg = bg, + fg = colors.strip_alpha(p.foreground), + bg_alt = colors.surface(p), + surface_fg = colors.readable(colors.strip_alpha(p.foreground), colors.surface(p), 4.5), + comment = colors.comment(p), + red = readable(p.color1), + green = readable(p.color2), + yellow = readable(p.color3), + cyan = readable(p.color6), } return { normal = { a = { bg = c.cyan, fg = c.bg, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, insert = { a = { bg = c.green, fg = c.bg, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, visual = { a = { bg = c.yellow, fg = c.bg, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, replace = { a = { bg = c.red, fg = c.bg, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, command = { a = { bg = c.red, fg = c.bg, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, terminal = { a = { bg = c.bg_alt, fg = c.green, gui = "bold" }, - b = { bg = c.bg_alt, fg = c.fg }, + b = { bg = c.bg_alt, fg = c.surface_fg }, c = { bg = c.bg, fg = c.fg }, }, inactive = { diff --git a/lua/themes/palettes.lua b/lua/themes/palettes.lua index 39e9671..36ee2b1 100644 --- a/lua/themes/palettes.lua +++ b/lua/themes/palettes.lua @@ -7,7 +7,7 @@ local M = { color4 = "#fd9353", color5 = "#948ae3", color6 = "#5ad4e6", - color7 = "#bbbbbb", -- grey, + color7 = "#f7f1ff", color8 = "#0f0f0f", color9 = "#fc618d", color10 = "#7bd88f", @@ -18,6 +18,7 @@ local M = { color15 = "#f7f1ff", background = "#050505", foreground = "#f7f1ff", + comment = "#bbbbbb", }, madrid = { color0 = "#fafafa", diff --git a/scripts/check_colors.lua b/scripts/check_colors.lua new file mode 100644 index 0000000..ddb4ae4 --- /dev/null +++ b/scripts/check_colors.lua @@ -0,0 +1,120 @@ +-- Headless checks for every Xscriptor theme. +-- +-- Run with: +-- nvim --headless -u NONE --cmd "set rtp+=." -l scripts/check_colors.lua +-- +-- The script applies each palette and verifies that the most important syntax +-- and UI highlight groups have enough contrast against their background. + +local colors = require("themes.colors") +local palettes = require("themes.palettes") +local themes = require("themes") + +local failures = {} + +local function fail(message) + table.insert(failures, message) +end + +local function to_hex(value) + if type(value) ~= "number" then + return nil + end + return string.format("#%06x", value) +end + +local function check_group(name, group, minimum, default_bg) + local hl = vim.api.nvim_get_hl(0, { name = group, link = false }) + local fg = to_hex(hl.fg) + local bg = to_hex(hl.bg) or default_bg + if not fg then + fail(("%s: %s has no foreground colour"):format(name, group)) + return + end + local ratio = colors.contrast(fg, bg) + if ratio < minimum then + fail(("%s: %s contrast %.2f < %.2f (%s on %s)"):format(name, group, ratio, minimum, fg, bg)) + end +end + +if type(palettes.strip_alpha) ~= "function" then + fail("palettes.strip_alpha is missing") +end + +if #themes.names ~= 12 then + fail(("expected 12 palettes, found %d"):format(#themes.names)) +end + +for _, name in ipairs(themes.names) do + if type(palettes[name]) ~= "table" then + fail(("%s: not a palette table"):format(name)) + else + local ok = themes.apply(name) + if not ok then + fail(("%s: themes.apply returned false"):format(name)) + else + local normal = vim.api.nvim_get_hl(0, { name = "Normal", link = false }) + local bg = to_hex(normal.bg) + if not bg then + fail(("%s: Normal has no background"):format(name)) + else + if vim.g.colors_name ~= name then + fail(("%s: colors_name is %s"):format(name, tostring(vim.g.colors_name))) + end + + local minimums = { + Comment = 3.0, + LineNr = 3.0, + String = 4.5, + Function = 4.5, + Keyword = 4.5, + Type = 4.5, + Constant = 4.5, + Number = 4.5, + Identifier = 4.5, + Operator = 4.5, + Statement = 4.5, + PreProc = 4.5, + DiagnosticError = 4.5, + DiagnosticWarn = 4.5, + DiagnosticInfo = 4.5, + DiagnosticHint = 4.5, + StatusLine = 4.5, + StatusLineNC = 3.0, + TabLine = 3.0, + CursorLineNr = 4.5, + WinBar = 4.5, + Pmenu = 4.5, + Visual = 4.5, + Search = 4.5, + PmenuSel = 4.5, + } + + for group, minimum in pairs(minimums) do + check_group(name, group, minimum, bg) + end + + for slot = 0, 15 do + local value = vim.g["terminal_color_" .. slot] + if type(value) ~= "string" or not value:match("^#%x%x%x%x%x%x$") then + fail(("%s: terminal_color_%d is %s"):format(name, slot, tostring(value))) + end + end + end + end + end +end + +if themes.apply("this-theme-does-not-exist") then + fail("unknown palette should return false") +end + +if #failures > 0 then + print(("Theme checks failed (%d):"):format(#failures)) + for _, message in ipairs(failures) do + print(" - " .. message) + end + vim.cmd("cquit 1") +else + print(("Theme checks passed for %d palettes."):format(#themes.names)) +end diff --git a/selene.toml b/selene.toml index faa6ec9..1904f03 100644 --- a/selene.toml +++ b/selene.toml @@ -1,9 +1,8 @@ -[base] -config = "selene" -std = "luajit" +std = "vim" -[style] -allow_unused_variable_prefixes = "_" +[lints] +mixed_table = "allow" +unused_arguments = "allow" -[lint] -unused_arguments = "warn" +[config] +unused_variable = { ignore_pattern = "^_" } diff --git a/vim.yml b/vim.yml new file mode 100644 index 0000000..608a4d3 --- /dev/null +++ b/vim.yml @@ -0,0 +1,5 @@ +base: lua51 + +globals: + vim: + any: true