1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:20:05 +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 function! SpaceVim#layers#tools#cpicker#plugins() abort
return [ 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 endfunction

View File

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

View File

@ -1,128 +1,129 @@
--============================================================================= --=============================================================================
-- cmyk.lua --- -- cmyk.lua ---
-- Copyright (c) 2019-2024 Wang Shidong & Contributors -- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
local M = {} local M = {}
local color = require('spacevim.api.color') local color = require('spacevim.api.color')
local util = require('cpicker.util') local util = require('cpicker.util')
local cyan = 0 local cyan = 0
local magenta = 0 local magenta = 0
local yellow = 0 local yellow = 0
local black = 0 local black = 0
M.color_code_regex = [[\scmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)]] M.color_code_regex = [[\scmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)]]
local function on_change_argv() local function on_change_argv()
return 'cmyk', { cyan, magenta, yellow, black } return 'cmyk', { cyan, magenta, yellow, black }
end end
function M.buf_text() function M.buf_text()
local rst = {} local rst = {}
local c_bar = util.generate_bar(cyan, '+') local c_bar = util.generate_bar(cyan, '+')
local m_bar = util.generate_bar(magenta, '+') local m_bar = util.generate_bar(magenta, '+')
local y_bar = util.generate_bar(yellow, '+') local y_bar = util.generate_bar(yellow, '+')
local k_bar = util.generate_bar(black, '+') local k_bar = util.generate_bar(black, '+')
table.insert(rst, 'CMYK: C: ' .. string.format('%4s', math.floor(cyan * 100 + 0.5)) .. ' ' .. c_bar) table.insert(rst, 'CMYK: C: ' .. string.format('%4s', math.floor(cyan * 100 + 0.5)) .. ' ' .. c_bar)
table.insert(rst, ' M: ' .. string.format('%4s', math.floor(magenta * 100 + 0.5)) .. ' ' .. m_bar) table.insert(rst, ' M: ' .. string.format('%4s', math.floor(magenta * 100 + 0.5)) .. ' ' .. m_bar)
table.insert(rst, ' Y: ' .. string.format('%4s', math.floor(yellow * 100 + 0.5)) .. ' ' .. y_bar) table.insert(rst, ' Y: ' .. string.format('%4s', math.floor(yellow * 100 + 0.5)) .. ' ' .. y_bar)
table.insert(rst, ' K: ' .. string.format('%4s', math.floor(black * 100 + 0.5)) .. ' ' .. k_bar) table.insert(rst, ' K: ' .. string.format('%4s', math.floor(black * 100 + 0.5)) .. ' ' .. k_bar)
return rst return rst
end end
function M.color_code() function M.color_code()
return return
' =========' .. string.format( ' =========' .. string.format(
' cmyk(%s%%, %s%%, %s%%, %s%%)', ' cmyk(%s%%, %s%%, %s%%, %s%%)',
math.floor(cyan * 100 + 0.5), math.floor(cyan * 100 + 0.5),
math.floor(magenta * 100 + 0.5), math.floor(magenta * 100 + 0.5),
math.floor(yellow * 100 + 0.5), math.floor(yellow * 100 + 0.5),
math.floor(black * 100 + 0.5) math.floor(black * 100 + 0.5)
) )
end end
local function increase_cyan() local function increase_cyan()
if cyan <= 0.99 then if cyan <= 0.99 then
cyan = cyan + 0.01 cyan = cyan + 0.01
elseif cyan < 1 then elseif cyan < 1 then
cyan = 1 cyan = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_cyan() local function reduce_cyan()
if cyan > 0.01 then if cyan > 0.01 then
cyan = cyan - 0.01 cyan = cyan - 0.01
elseif cyan > 0 then elseif cyan > 0 then
cyan = 0 cyan = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_magenta() local function increase_magenta()
if magenta <= 0.99 then if magenta <= 0.99 then
magenta = magenta + 0.01 magenta = magenta + 0.01
elseif magenta < 1 then elseif magenta < 1 then
magenta = 1 magenta = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_magenta() local function reduce_magenta()
if magenta > 0.01 then if magenta > 0.01 then
magenta = magenta - 0.01 magenta = magenta - 0.01
elseif magenta > 0 then elseif magenta > 0 then
magenta = 0 magenta = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_yellow() local function increase_yellow()
if yellow <= 0.99 then if yellow <= 0.99 then
yellow = yellow + 0.01 yellow = yellow + 0.01
elseif yellow < 1 then elseif yellow < 1 then
yellow = 1 yellow = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_yellow() local function reduce_yellow()
if yellow > 0.01 then if yellow > 0.01 then
yellow = yellow - 0.01 yellow = yellow - 0.01
elseif yellow > 0 then elseif yellow > 0 then
yellow = 0 yellow = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_black() local function increase_black()
if black <= 0.99 then if black <= 0.99 then
black = black + 0.01 black = black + 0.01
elseif black < 1 then elseif black < 1 then
black = 1 black = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_black() local function reduce_black()
if black > 0.01 then if black > 0.01 then
black = black - 0.01 black = black - 0.01
elseif black > 0 then elseif black > 0 then
black = 0 black = 0
end end
return on_change_argv() return on_change_argv()
end end
function M.increase_reduce_functions() function M.increase_reduce_functions()
return { return {
{ increase_cyan, reduce_cyan }, { increase_cyan, reduce_cyan },
{ increase_magenta, reduce_magenta }, { increase_magenta, reduce_magenta },
{ increase_yellow, reduce_yellow }, { increase_yellow, reduce_yellow },
{ increase_black, reduce_black }, { increase_black, reduce_black },
} }
end end
function M.on_change(f, code) function M.on_change(f, code)
if f == 'cmyk' then if f == 'cmyk' then
return cyan, magenta, yellow, black = unpack(code)
end return
cyan, magenta, yellow, black = color[f .. '2cmyk'](unpack(code)) end
end cyan, magenta, yellow, black = color[f .. '2cmyk'](unpack(code))
end
return M
return M

View File

@ -1,113 +1,114 @@
--============================================================================= --=============================================================================
-- hsl.lua -- hsl.lua
-- Copyright (c) 2019-2024 Wang Shidong & Contributors -- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
local M = {} local M = {}
local color = require('spacevim.api.color') local color = require('spacevim.api.color')
local util = require('cpicker.util') local util = require('cpicker.util')
local hue = 0 -- [0, 360] local hue = 0 -- [0, 360]
local saturation = 0 -- [0, 100%] local saturation = 0 -- [0, 100%]
local lightness = 0 -- [0, 100%] local lightness = 0 -- [0, 100%]
M.color_code_regex = [[\shsl(\d\+,\s\d\+%,\s\d\+%)]] M.color_code_regex = [[\shsl(\d\+,\s\d\+%,\s\d\+%)]]
local function on_change_argv() local function on_change_argv()
return 'hsl', { hue, saturation, lightness } return 'hsl', { hue, saturation, lightness }
end end
function M.buf_text() function M.buf_text()
local rst = {} local rst = {}
local h_bar = util.generate_bar(hue, '+', 360) local h_bar = util.generate_bar(hue, '+', 360)
local s_bar = util.generate_bar(saturation, '+') local s_bar = util.generate_bar(saturation, '+')
local l_bar = util.generate_bar(lightness, '+') local l_bar = util.generate_bar(lightness, '+')
table.insert(rst, 'HSL: H: ' .. string.format('%4s', math.floor(hue + 0.5)) .. ' ' .. h_bar) table.insert(rst, 'HSL: H: ' .. string.format('%4s', math.floor(hue + 0.5)) .. ' ' .. h_bar)
table.insert( table.insert(
rst, rst,
' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar
) )
table.insert( table.insert(
rst, rst,
' L: ' .. string.format('%3s', math.floor(lightness * 100 + 0.5)) .. '% ' .. l_bar ' L: ' .. string.format('%3s', math.floor(lightness * 100 + 0.5)) .. '% ' .. l_bar
) )
return rst return rst
end end
function M.color_code() function M.color_code()
return return
' =========' .. string.format( ' =========' .. string.format(
' hsl(%s, %s%%, %s%%)', ' hsl(%s, %s%%, %s%%)',
math.floor(hue + 0.5), math.floor(hue + 0.5),
math.floor(saturation * 100 + 0.5), math.floor(saturation * 100 + 0.5),
math.floor(lightness * 100 + 0.5) math.floor(lightness * 100 + 0.5)
) )
end end
local function increase_hsl_h() local function increase_hsl_h()
if hue <= 359 then if hue <= 359 then
hue = hue + 1 hue = hue + 1
elseif hue < 360 then elseif hue < 360 then
hue = 360 hue = 360
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_h() local function reduce_hsl_h()
if hue >= 1 then if hue >= 1 then
hue = hue - 1 hue = hue - 1
elseif hue > 0 then elseif hue > 0 then
hue = 0 hue = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_hsl_s() local function increase_hsl_s()
if saturation <= 0.99 then if saturation <= 0.99 then
saturation = saturation + 0.01 saturation = saturation + 0.01
elseif saturation < 1 then elseif saturation < 1 then
saturation = 1 saturation = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_s() local function reduce_hsl_s()
if saturation >= 0.01 then if saturation >= 0.01 then
saturation = saturation - 0.01 saturation = saturation - 0.01
elseif saturation > 0 and saturation < 0.01 then elseif saturation > 0 and saturation < 0.01 then
saturation = 0 saturation = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_hsl_l() local function increase_hsl_l()
if lightness <= 0.99 then if lightness <= 0.99 then
lightness = lightness + 0.01 lightness = lightness + 0.01
elseif lightness < 1 then elseif lightness < 1 then
lightness = 1 lightness = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_l() local function reduce_hsl_l()
if lightness >= 0.01 then if lightness >= 0.01 then
lightness = lightness - 0.01 lightness = lightness - 0.01
elseif lightness > 0 and lightness < 0.01 then elseif lightness > 0 and lightness < 0.01 then
lightness = 0 lightness = 0
end end
return on_change_argv() return on_change_argv()
end end
function M.increase_reduce_functions() function M.increase_reduce_functions()
return { return {
{ increase_hsl_h, reduce_hsl_h }, { increase_hsl_h, reduce_hsl_h },
{ increase_hsl_s, reduce_hsl_s }, { increase_hsl_s, reduce_hsl_s },
{ increase_hsl_l, reduce_hsl_l }, { increase_hsl_l, reduce_hsl_l },
} }
end end
function M.on_change(f, code) function M.on_change(f, code)
if f == 'hsl' then if f == 'hsl' then
return hue, saturation, lightness = unpack(code)
end return
hue, saturation, lightness = color[f .. '2hsl'](unpack(code)) end
end hue, saturation, lightness = color[f .. '2hsl'](unpack(code))
end
return M
return M

View File

@ -1,113 +1,114 @@
--============================================================================= --=============================================================================
-- hsv.lua --- -- hsv.lua ---
-- Copyright (c) 2019-2024 Wang Shidong & Contributors -- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
local M = {} local M = {}
local color = require('spacevim.api.color') local color = require('spacevim.api.color')
local util = require('cpicker.util') local util = require('cpicker.util')
local hue = 0 local hue = 0
local saturation = 0 local saturation = 0
local value = 0 local value = 0
M.color_code_regex = [[\shsv(\d\+,\s\d\+%,\s\d\+%)]] M.color_code_regex = [[\shsv(\d\+,\s\d\+%,\s\d\+%)]]
local function on_change_argv() local function on_change_argv()
return 'hsv', {hue, saturation, value} return 'hsv', {hue, saturation, value}
end end
function M.buf_text() function M.buf_text()
local rst = {} local rst = {}
local h_bar = util.generate_bar(hue, '+', 360) local h_bar = util.generate_bar(hue, '+', 360)
local s_bar = util.generate_bar(saturation, '+') local s_bar = util.generate_bar(saturation, '+')
local l_bar = util.generate_bar(value, '+') local l_bar = util.generate_bar(value, '+')
table.insert(rst, 'HSV: H: ' .. string.format('%4s', math.floor(hue + 0.5)) .. ' ' .. h_bar) table.insert(rst, 'HSV: H: ' .. string.format('%4s', math.floor(hue + 0.5)) .. ' ' .. h_bar)
table.insert( table.insert(
rst, rst,
' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar
) )
table.insert( table.insert(
rst, rst,
' V: ' .. string.format('%3s', math.floor(value * 100 + 0.5)) .. '% ' .. l_bar ' V: ' .. string.format('%3s', math.floor(value * 100 + 0.5)) .. '% ' .. l_bar
) )
return rst return rst
end end
function M.color_code() function M.color_code()
return return
' =========' .. string.format( ' =========' .. string.format(
' hsv(%s, %s%%, %s%%)', ' hsv(%s, %s%%, %s%%)',
math.floor(hue + 0.5), math.floor(hue + 0.5),
math.floor(saturation * 100 + 0.5), math.floor(saturation * 100 + 0.5),
math.floor(value * 100 + 0.5) math.floor(value * 100 + 0.5)
) )
end end
local function increase_hsl_h() local function increase_hsl_h()
if hue <= 359 then if hue <= 359 then
hue = hue + 1 hue = hue + 1
elseif hue < 360 then hue = 360 elseif hue < 360 then hue = 360
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_h() local function reduce_hsl_h()
if hue >= 1 then if hue >= 1 then
hue = hue - 1 hue = hue - 1
elseif hue > 0 then elseif hue > 0 then
hue = 0 hue = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_hsl_s() local function increase_hsl_s()
if saturation <= 0.99 then if saturation <= 0.99 then
saturation = saturation + 0.01 saturation = saturation + 0.01
elseif saturation < 1 then elseif saturation < 1 then
saturation = 1 saturation = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_s() local function reduce_hsl_s()
if saturation >= 0.01 then if saturation >= 0.01 then
saturation = saturation - 0.01 saturation = saturation - 0.01
elseif saturation > 0 and saturation < 0.01 then elseif saturation > 0 and saturation < 0.01 then
saturation = 0 saturation = 0
end end
return on_change_argv() return on_change_argv()
end end
local function increase_hsl_v() local function increase_hsl_v()
if value <= 0.99 then if value <= 0.99 then
value = value + 0.01 value = value + 0.01
elseif value < 1 then elseif value < 1 then
value = 1 value = 1
end end
return on_change_argv() return on_change_argv()
end end
local function reduce_hsl_v() local function reduce_hsl_v()
if value >= 0.01 then if value >= 0.01 then
value = value - 0.01 value = value - 0.01
elseif value > 0 and value < 0.01 then elseif value > 0 and value < 0.01 then
value = 0 value = 0
end end
return on_change_argv() return on_change_argv()
end end
function M.increase_reduce_functions() function M.increase_reduce_functions()
return { return {
{ increase_hsl_h, reduce_hsl_h }, { increase_hsl_h, reduce_hsl_h },
{ increase_hsl_s, reduce_hsl_s }, { increase_hsl_s, reduce_hsl_s },
{ increase_hsl_v, reduce_hsl_v }, { increase_hsl_v, reduce_hsl_v },
} }
end end
function M.on_change(f, code) function M.on_change(f, code)
if f == 'hsv' then if f == 'hsv' then
return hue, saturation, value = unpack(code)
end return
hue, saturation, value = color[f .. '2hsv'](unpack(code)) end
end hue, saturation, value = color[f .. '2hsv'](unpack(code))
end
return M
return M

View File

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

View File

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

View File

@ -1,37 +1,81 @@
--============================================================================= --=============================================================================
-- util.lua -- util.lua
-- Copyright (c) 2019-2024 Wang Shidong & Contributors -- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
local M = {} local M = {}
local color = require('spacevim.api.color') local color = require('spacevim.api.color')
function M.generate_bar(n, char, m) function M.generate_bar(n, char, m)
return string.rep(char, math.floor(24 * n / (m or 1))) return string.rep(char, math.floor(24 * n / (m or 1)))
end end
function M.get_hex_code(t, code) function M.get_hex_code(t, code)
return color[t .. '2hex'](unpack(code)) return color[t .. '2hex'](unpack(code))
end end
function M.get_hsl_l(hex) function M.get_hsl_l(hex)
local _, _, l = color.rgb2hsl(color.hex2rgb(hex)) local _, _, l = color.rgb2hsl(color.hex2rgb(hex))
return l return l
end end
function M.update_color_code_syntax(r) function M.update_color_code_syntax(r)
local max = 0 local max = 0
local regexes = {} local regexes = {}
for _, v in ipairs(r) do for _, v in ipairs(r) do
max = math.max(max, v[1]) max = math.max(max, v[1])
end end
regexes = vim.tbl_map(function(val) regexes = vim.tbl_map(function(val)
return val[2] .. string.rep('\\s', max - val[1]) return val[2] .. string.rep('\\s', max - val[1])
end, r) end, r)
vim.cmd('syn match SpaceVimPickerCode /' .. table.concat(regexes, '\\|') .. '/') vim.cmd('syn match SpaceVimPickerCode /' .. table.concat(regexes, '\\|') .. '/')
end end
return M 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

@ -1,17 +1,22 @@
--============================================================================= --=============================================================================
-- cpicker.lua -- cpicker.lua
-- Copyright (c) 2019-2024 Wang Shidong & Contributors -- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
if vim.api.nvim_create_user_command then if vim.api.nvim_create_user_command then
local function complete(...) local function complete(...)
return {'all', 'rgb', 'hsl'} return {'all', 'rgb', 'hsl'}
end end
vim.api.nvim_create_user_command('Cpicker', function(opt) vim.api.nvim_create_user_command('Cpicker', function(opt)
require('cpicker').picker(opt.fargs) require('cpicker').picker(opt.fargs)
end, { nargs = '*', complete = complete }) end, { nargs = '*', complete = complete })
end 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