Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Color): don't use shared global state (Color.BG etc.) #363

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
### Issues Fix

- Fixed JSX/TSX tags are missing highlights with nvim 0.10 (#360)
- Fixed loading palette or spec before colorscheme disrupts colors (#362 fixed-by #363)

## [v1.1.2] - 05 August 2024

Expand Down
2 changes: 1 addition & 1 deletion lua/github-theme/group/modules/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function M.get(spec, config, _opts)
-- being integrated.
local primitives = require(
'github-theme.palette.primitives.'
.. require('github-theme.config').theme
.. spec.palette.meta.name
:gsub('^github_(.-)_default$', '%1')
:gsub('^github_(.-)$', '%1')
)
Expand Down
28 changes: 22 additions & 6 deletions lua/github-theme/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function M.compile(force)

local hash = require('github-theme.lib.hash')(dummy) .. (git == -1 and git_path or git)

-- Compile
if force ~= false or cached ~= hash then
require('github-theme.lib.log').clear()
local compiler = require('github-theme.lib.compiler')
Expand Down Expand Up @@ -84,15 +85,15 @@ function M.load(opts)
end

local _, compiled_file = config.get_compiled_info(opts)
local f = loadfile(compiled_file)
local compiled_theme = loadfile(compiled_file)

if not did_setup or override.changed_since_last_compile or not f then
if not did_setup or override.changed_since_last_compile or not compiled_theme then
M.setup()
f = loadfile(compiled_file)
compiled_theme = loadfile(compiled_file)
end

---@diagnostic disable-next-line: need-check-nil
f()
compiled_theme()
require('github-theme.autocmds').set_autocmds()
end

Expand All @@ -115,8 +116,23 @@ function M.setup(opts)
end
end

M.compile(false)
require('github-theme.util.deprecation').check_deprecation(opts)
M.compile(not not vim.g.github_theme_force_compile)

-- Use our 1 time to check for deprecations the first time `setup()` is called with
-- opts, instead of the first time `setup()` is called at all.
if next(opts) ~= nil then
-- TODO: might be better to call this and emit notices whenever config changes and on
-- 1st load/setup(), while filtering deprecation messages at the msg level instead of
-- globally.
require('github-theme.util.deprecation').check_deprecation(opts)
end
end

-- Mainly for debugging, testing, development, etc.
for _, env in ipairs({ 'GITHUB_THEME_DEBUG', 'GITHUB_THEME_FORCE_COMPILE' }) do
if vim.env[env] then
vim.g[env:lower()] = true
end
end

return M
28 changes: 13 additions & 15 deletions lua/github-theme/lib/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ end

---Returns a new Color that is a linear blend between `self` and `other`.
---@param other GhTheme.Color
---@param f number Float [0,1] where 0 is `self` and 1 is `other`
---@param f number float `[0,1]` where `0` is `self` and `1` is `other`
---@return GhTheme.Color
---@nodiscard
function Color:blend(other, f)
Expand All @@ -349,20 +349,18 @@ function Color:blend(other, f)
end

---Returns a new Color that is a linear blend between `Color.BG` and `self`.
---@param f number Float [0,1] where 0 is `Color.BG` and 1 is `self`
---
---> WARNING: This method might not work correctly until ***after*** the colorscheme has
---> loaded, or `Color.BG` has been set! DO NOT USE INTERNALLY!
---@param alpha number float (`[0,1]`) where `0` is `Color.BG` and `1` is `self`
---@return GhTheme.Color
---@nodiscard
function Color:alpha_blend(f)
return M.init(
(self.red - self.BG.red) * f + self.BG.red,
(self.green - self.BG.green) * f + self.BG.green,
(self.blue - self.BG.blue) * f + self.BG.blue,
self.alpha
)
function Color:alpha_blend(alpha)
return self.BG:blend(self, alpha)
end

---Returns a new Color which is `self` shaded according to `f`.
---@param f number Amount. Float [-1,1]. -1 is black and 1 is white
---@param f number float (`[-1,1]`) where `-1` is black and `1` is white
---@return GhTheme.Color
---@nodiscard
function Color:shade(f)
Expand All @@ -377,9 +375,9 @@ function Color:shade(f)
)
end

---Adds value of `v` to the `value` of the current color. Returns a new Color
---that is either a brighter version (v >= 0), or darker (v < 0).
---@param v number Value. Float [-100,100].
---Adds value of `v` to the `value` of the current color. Returns a new Color that is
---either a brighter version (`v >= 0`), or darker (`v < 0`).
---@param v number Value. Float `[-100,100]`.
---@return GhTheme.Color
---@nodiscard
function Color:brighten(v)
Expand All @@ -390,7 +388,7 @@ end

---Adds value of `v` to the `lightness` of the current color. Returns a new Color
---that is either a lighter version if +v and darker if -v.
---@param v number Lightness. Float [-100,100].
---@param v number Lightness. Float `[-100,100]`.
---@return GhTheme.Color
---@nodiscard
function Color:lighten(v)
Expand All @@ -411,7 +409,7 @@ function Color:saturate(v)
end

---Adds value of `v` to the `hue` of the current color. Returns a new Color where
---the hue is rotated based on +/- of `v`. Resulting `hue` is wrapped [0,360].
---the hue is rotated based on +/- of `v`. Resulting `hue` is wrapped `[0,360]`.
---@param v number amount
---@return GhTheme.Color
---@nodiscard
Expand Down
6 changes: 3 additions & 3 deletions lua/github-theme/lib/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ vim.g.colors_name = "%s"

file = io.open(output_file, 'wb')

local f = loadstring(table.concat(lines, '\n'), '=')
if not f then
local dump_theme = loadstring(table.concat(lines, '\n'), 'dump_theme')
if not dump_theme then
local tmpfile = util.join_paths(util.get_tmp_dir(), 'github_theme_error.lua')
require('github-theme.lib.log').error(
fmt(
Expand All @@ -109,7 +109,7 @@ Bellow is the error message:
dofile(tmpfile)
end

file:write(f())
file:write(dump_theme())
file:close()
end

Expand Down
71 changes: 34 additions & 37 deletions lua/github-theme/palette/github_dark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ local scale = primitives.scale
C.WHITE = C(scale.white)
C.BLACK = C(scale.black)
C.BG = C(scale.gray[7])

local function alpha(color, a)
return color:alpha_blend(a):to_css()
end
local BG = C(scale.gray[7])

local palette = {
scale = scale,
Expand Down Expand Up @@ -52,86 +49,86 @@ local palette = {
border = {
default = scale.gray[9],
muted = scale.gray[8],
subtle = alpha(C.from_rgba(240, 246, 252, 1), 0.1),
subtle = BG:blend(C.from_rgba(240, 246, 252, 1), 0.1):to_css(),
},

neutral = {
emphasis_plus = scale.gray[5],
emphasis = scale.gray[5],
muted = alpha(C.from_rgba(110, 118, 129, 1), 0.4),
subtle = alpha(C.from_rgba(110, 118, 129, 1), 0.1),
muted = BG:blend(C.from_rgba(110, 118, 129, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(110, 118, 129, 1), 0.1):to_css(),
},

accent = {
fg = '#2f81f7',
emphasis = scale.blue[6],
muted = alpha(C.from_rgba(56, 139, 253, 1), 0.4),
subtle = alpha(C.from_rgba(56, 139, 253, 1), 0.15),
muted = BG:blend(C.from_rgba(56, 139, 253, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(56, 139, 253, 1), 0.15):to_css(),
},

success = {
fg = scale.green[4],
emphasis = scale.green[6],
muted = alpha(C.from_rgba(46, 160, 67, 1), 0.4),
subtle = alpha(C.from_rgba(46, 160, 67, 1), 0.15),
muted = BG:blend(C.from_rgba(46, 160, 67, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(46, 160, 67, 1), 0.15):to_css(),
},

attention = {
fg = scale.yellow[4],
emphasis = scale.yellow[6],
muted = alpha(C.from_rgba(187, 128, 9, 1), 0.4),
subtle = alpha(C.from_rgba(187, 128, 9, 1), 0.15),
muted = BG:blend(C.from_rgba(187, 128, 9, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(187, 128, 9, 1), 0.15):to_css(),
},

severe = {
fg = scale.orange[5],
emphasis = scale.orange[6],
muted = alpha(C.from_rgba(219, 109, 40, 1), 0.4),
subtle = alpha(C.from_rgba(219, 109, 40, 1), 0.1),
muted = BG:blend(C.from_rgba(219, 109, 40, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(219, 109, 40, 1), 0.1):to_css(),
},

danger = {
fg = scale.red[5],
emphasis = scale.red[6],
muted = alpha(C.from_rgba(248, 81, 73, 1), 0.4),
subtle = alpha(C.from_rgba(248, 81, 73, 1), 0.1),
muted = BG:blend(C.from_rgba(248, 81, 73, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(248, 81, 73, 1), 0.1):to_css(),
},

open = {
fg = scale.green[4],
emphasis = scale.green[6],
muted = alpha(C.from_rgba(46, 160, 67, 1), 0.4),
subtle = alpha(C.from_rgba(46, 160, 67, 1), 0.15),
muted = BG:blend(C.from_rgba(46, 160, 67, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(46, 160, 67, 1), 0.15):to_css(),
},

done = {
fg = scale.purple[5],
emphasis = scale.purple[6],
muted = alpha(C.from_rgba(163, 113, 247, 1), 0.4),
subtle = alpha(C.from_rgba(163, 113, 247, 1), 0.1),
muted = BG:blend(C.from_rgba(163, 113, 247, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(163, 113, 247, 1), 0.1):to_css(),
},

closed = {
fg = scale.red[5],
emphasis = scale.red[6],
muted = alpha(C.from_rgba(248, 81, 73, 1), 0.4),
subtle = alpha(C.from_rgba(248, 81, 73, 1), 0.15),
muted = BG:blend(C.from_rgba(248, 81, 73, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(248, 81, 73, 1), 0.15):to_css(),
},

sponsors = {
fg = scale.pink[5],
emphasis = scale.pink[6],
muted = alpha(C.from_rgba(219, 97, 162, 1), 0.4),
subtle = alpha(C.from_rgba(219, 97, 162, 1), 0.1),
muted = BG:blend(C.from_rgba(219, 97, 162, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(219, 97, 162, 1), 0.1):to_css(),
},
}

local function generate_spec(pal)
-- stylua: ignore start
local spec = {
bg0 = alpha(C(pal.canvas.inset), 0.75), -- Dark bg (popup and float)
bg0 = BG:blend(C(pal.canvas.inset), 0.75):to_css(), -- Dark bg (popup and float)
bg1 = pal.canvas.default, -- Default bg
bg2 = alpha(C(pal.neutral.emphasis), 0.1), -- Lighter bg (colorcolumn Folds)
bg2 = BG:blend(C(pal.neutral.emphasis), 0.1):to_css(), -- Lighter bg (colorcolumn Folds)
bg3 = pal.scale.gray[6], -- Lighter bg (cursor line)
bg4 = pal.scale.gray[4], -- Conceal

Expand All @@ -140,9 +137,9 @@ local function generate_spec(pal)
fg2 = pal.fg.muted, -- Darker fg (status line)
fg3 = pal.scale.gray[5], -- Darker fg (line numbers, fold columns)

sel0 = alpha(C(pal.accent.fg), 0.30), -- Visual selection bg
sel1 = alpha(C(pal.accent.muted), 0.90), -- Popup sel bg
sel2 = alpha(C(pal.scale.yellow[1]), 0.20), -- Search bg
sel0 = BG:blend(C(pal.accent.fg), 0.30):to_css(), -- Visual selection bg
sel1 = BG:blend(C(pal.accent.muted), 0.90):to_css(), -- Popup sel bg
sel2 = BG:blend(C(pal.scale.yellow[1]), 0.20):to_css(), -- Search bg
}

spec.syntax = {
Expand Down Expand Up @@ -178,16 +175,16 @@ local function generate_spec(pal)
}

spec.diag_bg = {
error = C(spec.bg1):blend(C(spec.diag.error), 0.15):to_css(),
warn = C(spec.bg1):blend(C(spec.diag.warn), 0.15):to_css(),
info = C(spec.bg1):blend(C(spec.diag.info), 0.15):to_css(),
hint = C(spec.bg1):blend(C(spec.diag.hint), 0.15):to_css(),
error = BG:blend(C(spec.diag.error), 0.15):to_css(),
warn = BG:blend(C(spec.diag.warn), 0.15):to_css(),
info = BG:blend(C(spec.diag.info), 0.15):to_css(),
hint = BG:blend(C(spec.diag.hint), 0.15):to_css(),
}

spec.diff = {
add = alpha(C(pal.scale.green[6]), 0.15),
delete = alpha(C(pal.scale.red[6]), 0.15),
change = alpha(C(pal.scale.yellow[6]), 0.15),
add = BG:blend(C(pal.scale.green[6]), 0.15):to_css(),
delete = BG:blend(C(pal.scale.red[6]), 0.15):to_css(),
change = BG:blend(C(pal.scale.yellow[6]), 0.15):to_css(),
text = spec.fg0
}

Expand Down
Loading
Loading