sainnhe / gruvbox-material

Gruvbox with Material Palette
MIT License
1.9k stars 164 forks source link

Different contrasts for dark and light modes #208

Closed subnut closed 3 months ago

subnut commented 3 months ago

Thank you for making such a beautiful and featureful colorscheme.

I have set my terminal to use GruvboxLight and GruvboxDarkHard on light and dark mode respectively. But I can't seem to find any way to do the same for this colorscheme on neovim.

To clarify, what I wanted is some way to have normal contrast for bg=light and hard contrast for bg=dark

Is that achievable?

antoineco commented 3 months ago

Since the contrast is set via a global option, it is required to reload the colorscheme for changes to take effect at runtime.

(Neo)vim does it for you upon changing between dark and light backgrounds, so all you need to do is ensure that the option is set before the colorscheme is being reloaded.

Here is an autocmd that should achieve this:

vim.api.nvim_create_autocmd('ColorSchemePre', {
  group = vim.api.nvim_create_augroup('gruvbox_material_background', {}),
  pattern = 'gruvbox-material',
  callback = function()
    vim.g.gruvbox_material_background = vim.o.bg == 'dark' and 'hard' or 'medium'
  end
})

(It should be declared in your init.lua before calling vim.cmd.colorscheme 'gruvbox-material'.)

subnut commented 3 months ago

I see.

I was just wondering if there was any other way instead of resorting to autocommands.

Thank you for the snippet. I can confirm it works :+1:

antoineco commented 3 months ago

autocmds are a core functionality of Vim and Neovim. It's a power to use them, not a shame (and it's a pity that the Lua API turns them into something so verbose in Neovim). A lot of plugins are built on the shoulders of autocmds. For instance, they are well accepted as the most idiomatic and robust way to define highlight overrides per colorscheme.

The alternative here would be to define a separate set of options for dark and light modes in Gruvbox-Material, but this would be tedious to maintain, fairly niche in terms of usage, and still nowhere as powerful as autocmds.