1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:40:03 +08:00

feat(cpicker): picker color from cursor

This commit is contained in:
Eric Wong 2024-07-14 22:32:55 +08:00
parent f35e0da911
commit dcde04c454
9 changed files with 472 additions and 410 deletions

View File

@ -48,7 +48,7 @@ let s:default_spaces = ['rgb', 'hsl']
function! SpaceVim#layers#tools#cpicker#plugins() abort
return [
\ [g:_spacevim_root_dir . 'bundle/cpicker.nvim', {'merged' : 0, 'loadconf' : 1, 'on_cmd' : 'Cpicker'}],
\ [g:_spacevim_root_dir . 'bundle/cpicker.nvim', {'merged' : 0, 'loadconf' : 1, 'on_cmd' : ['Cpicker', 'CpickerCursorForeground']}],
\ ]
endfunction

View File

@ -125,7 +125,11 @@ local function reduce()
end
M.picker = function(formats)
if #formats == 0 then
enabled_formats = {'rgb', 'hsl'}
else
enabled_formats = formats
end
log.info(vim.inspect(enabled_formats))
if not bufnr or not vim.api.nvim_win_is_valid(bufnr) then
bufnr = vim.api.nvim_create_buf(false, false)
@ -181,4 +185,8 @@ M.picker = function(formats)
update_buf_text()
end
M.set_default_color = function(hex)
color_hi = hex
end
return M

View File

@ -120,6 +120,7 @@ end
function M.on_change(f, code)
if f == 'cmyk' then
cyan, magenta, yellow, black = unpack(code)
return
end
cyan, magenta, yellow, black = color[f .. '2cmyk'](unpack(code))

View File

@ -105,6 +105,7 @@ end
function M.on_change(f, code)
if f == 'hsl' then
hue, saturation, lightness = unpack(code)
return
end
hue, saturation, lightness = color[f .. '2hsl'](unpack(code))

View File

@ -105,6 +105,7 @@ end
function M.on_change(f, code)
if f == 'hsv' then
hue, saturation, value = unpack(code)
return
end
hue, saturation, value = color[f .. '2hsv'](unpack(code))

View File

@ -105,6 +105,7 @@ end
function M.on_change(f, code)
if f == 'hwb' then
hue, whiteness, blackness = unpack(code)
return
end
hue, whiteness, blackness = color[f .. '2hwb'](unpack(code))

View File

@ -98,6 +98,7 @@ end
function M.on_change(f, code)
if f == 'rgb' then
red, green, blue = unpack(code)
return
end
red, green, blue = color[f .. '2rgb'](unpack(code))

View File

@ -34,4 +34,48 @@ function M.update_color_code_syntax(r)
vim.cmd('syn match SpaceVimPickerCode /' .. table.concat(regexes, '\\|') .. '/')
end
local function get_color(name)
local c = vim.api.nvim_get_hl(0, { name = name })
if c.link then
return get_color(c.link)
else
return c
end
end
function M.set_default_color(formats)
if #formats == 0 then
formats = { 'rgb', 'hsl' }
else
formats = formats
end
local inspect = vim.inspect_pos()
local hex
if #inspect.treesitter > 0 then
local fg =
vim.api.nvim_get_hl(0, { name = inspect.treesitter[#inspect.treesitter].hl_group_link }).fg
if fg then
hex = string.format('#%06X', fg)
end
else
local name = vim.fn.synIDattr(vim.fn.synID(vim.fn.line('.'), vim.fn.col('.'), 1), 'name', 'gui')
local fg = get_color(name).fg
if fg then
hex = string.format('#%06X', fg)
end
end
if hex then
require('cpicker').set_default_color(hex)
local r, g, b = color.hex2rgb(hex)
for _, format in ipairs(formats) do
local ok, f = pcall(require, 'cpicker.formats.' .. format)
if ok then
f.on_change('rgb', { r, g, b })
end
end
end
end
return M

View File

@ -14,4 +14,9 @@ if vim.api.nvim_create_user_command then
require('cpicker').picker(opt.fargs)
end, { nargs = '*', complete = complete })
vim.api.nvim_create_user_command('CpickerCursorForeground', function(opt)
require('cpicker.util').set_default_color(opt.fargs)
require('cpicker').picker(opt.fargs)
end, { nargs = '*', complete = complete })
end