Skip to content

Commit

Permalink
feat(colors): allow color overrides to override shades with predefine…
Browse files Browse the repository at this point in the history
…d colors
  • Loading branch information
ramojus committed Jun 23, 2024
1 parent 4284fd3 commit 66da055
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
53 changes: 38 additions & 15 deletions lua/mellifluous/colors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,41 @@ local function tbl_extend_non_nil(base_table, overlay_table)
end
end

local function get_color_overrides(is_bg_dark, color_set_name)
local function get_color_overrides_bg()
local config = require('mellifluous.config').config
local color_overrides = vim.tbl_get(config, color_set_name,
'color_overrides', is_bg_dark and 'dark' or 'light') or {}

for key, color in pairs(color_overrides) do
if color.hex then -- overrides were already converted to mellifluous.color
break
end
color_overrides[key] = require('mellifluous.color').new(color)
local global_color_overrides_bg = vim.tbl_get(config,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'bg')
local color_overrides_bg = vim.tbl_get(config, config.color_set,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'bg')

if color_overrides_bg == nil then
color_overrides_bg = global_color_overrides_bg
end

return color_overrides
return color_overrides_bg
end

local function apply_color_overrides(colors)
local config = require('mellifluous.config').config
local global_color_overrides_fn = vim.tbl_get(config,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'colors')
local color_overrides_fn = vim.tbl_get(config, config.color_set,
'color_overrides', config.is_bg_dark and 'dark' or 'light', 'colors')

if global_color_overrides_fn ~= nil then
tbl_extend_non_nil(colors, global_color_overrides_fn(colors))
end
if color_overrides_fn ~= nil then
tbl_extend_non_nil(colors, color_overrides_fn(colors))
end
return colors
end

local function ensure_correct_color_types(colors)
local color_lib = require('mellifluous.color')
for key, color in pairs(colors) do
colors[key] = color_lib.new(color.hex or color)
end
end

function M.get_is_bg_dark(color_set_name)
Expand Down Expand Up @@ -59,19 +81,20 @@ function M.get_colors()
require('mellifluous').return_error("Color set '" .. config.color_set .. "' not found")
end

local color_overrides = get_color_overrides(config.is_bg_dark, config.color_set)
local color_overrides_bg = get_color_overrides_bg()

local color_set_functions = require('mellifluous.colors.sets.' .. config.color_set)
local colors
if config.is_bg_dark then
colors = color_set_functions.get_colors_dark(color_overrides.bg or color_set_functions.get_bg_dark())
colors = color_set_functions.get_colors_dark(color_overrides_bg or color_set_functions.get_bg_dark())
else
colors = color_set_functions.get_colors_light(color_overrides.bg or color_set_functions.get_bg_light())
colors = color_set_functions.get_colors_light(color_overrides_bg or color_set_functions.get_bg_light())
end

tbl_extend_non_nil(colors, color_overrides)
colors = apply_color_overrides(colors)
ensure_correct_color_types(colors)

colors = require'mellifluous.colors.shades'.extend_with_shades(colors)
colors = require('mellifluous.colors.shades').extend_with_shades(colors)

return colors
end
Expand Down
4 changes: 2 additions & 2 deletions lua/mellifluous/colors/shades.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function M.extend_with_shades(colors)
}
end

colors = vim.tbl_extend('force', colors, shared_shades)
return vim.tbl_extend('force', colors, shades)
colors = vim.tbl_extend('keep', colors, shared_shades)
return vim.tbl_extend('keep', colors, shades)
end

return M

0 comments on commit 66da055

Please sign in to comment.