1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 22:40:06 +08:00

feat(cpicker): change color code background

This commit is contained in:
Eric Wong 2024-07-14 19:01:57 +08:00
parent a9a14ff85b
commit f35e0da911
10 changed files with 67 additions and 24 deletions

View File

@ -35,23 +35,35 @@ local function update_buf_text()
end
end
table.insert(rst, '')
local color_code_regex = {}
for _, format in ipairs(enabled_formats) do
local ok, f = pcall(require, 'cpicker.formats.' .. format)
if ok then
table.insert(rst, f.color_code())
table.insert(rst, f.color_code() .. string.rep(' ', 20))
table.insert(color_code_regex, {#f.color_code(), f.color_code_regex})
end
end
util.update_color_code_syntax(color_code_regex)
local normal_bg = hi.group2dict('Normal').guibg
local normal_fg = hi.group2dict('Normal').guifg
if
math.abs(util.get_hsl_l(normal_bg) - util.get_hsl_l(color_hi))
> math.abs(util.get_hsl_l(color_hi) - util.get_hsl_l(normal_fg))
then
hi.hi({
name = 'SpaceVimPickerCode',
guifg = color_hi,
guibg = normal_bg,
bold = 1,
})
else
hi.hi({
name = 'SpaceVimPickerNoText',
guifg = normal_bg,
guibg = normal_bg,
name = 'SpaceVimPickerCode',
guifg = color_hi,
guibg = normal_fg,
bold = 1,
})
end
hi.hi({
name = 'SpaceVimPickerBackground',
guibg = color_hi,
@ -66,7 +78,7 @@ local function update_buf_text()
buf = bufnr,
})
vim.api.nvim_win_set_config(winid, {
height = #rst + 1
height = #rst + 1,
})
end
@ -74,7 +86,9 @@ end
local function copy_color()
local from, to = vim
.regex([[#[0123456789ABCDEF]\+\|rgb(\d\+,\s\d\+,\s\d\+)\|hsl(\d\+,\s\d\+%,\s\d\+%)\|hsv(\d\+,\s\d\+%,\s\d\+%)\|cmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)\|hwb(\d\+,\s\d\+%,\s\d\+%)]])
.regex(
[[#[0123456789ABCDEF]\+\|rgb(\d\+,\s\d\+,\s\d\+)\|hsl(\d\+,\s\d\+%,\s\d\+%)\|hsv(\d\+,\s\d\+%,\s\d\+%)\|cmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)\|hwb(\d\+,\s\d\+%,\s\d\+%)]]
)
:match_str(vim.fn.getline('.'))
if from then
vim.fn.setreg('+', string.sub(vim.fn.getline('.'), from, to + 1))
@ -149,7 +163,7 @@ M.picker = function(formats)
winid = vim.api.nvim_open_win(bufnr, true, {
relative = 'cursor',
border = 'single',
width = 40,
width = 44,
height = 10,
row = 1,
col = 1,

View File

@ -15,6 +15,8 @@ local magenta = 0
local yellow = 0
local black = 0
M.color_code_regex = [[\scmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)]]
local function on_change_argv()
return 'cmyk', { cyan, magenta, yellow, black }
end

View File

@ -14,6 +14,8 @@ local hue = 0 -- [0, 360]
local saturation = 0 -- [0, 100%]
local lightness = 0 -- [0, 100%]
M.color_code_regex = [[\shsl(\d\+,\s\d\+%,\s\d\+%)]]
local function on_change_argv()
return 'hsl', { hue, saturation, lightness }
end

View File

@ -14,6 +14,8 @@ local hue = 0
local saturation = 0
local value = 0
M.color_code_regex = [[\shsv(\d\+,\s\d\+%,\s\d\+%)]]
local function on_change_argv()
return 'hsv', {hue, saturation, value}
end

View File

@ -14,6 +14,8 @@ local hue = 0 -- [0, 360]
local whiteness = 0 -- [0, 100%]
local blackness = 0 -- [0, 100%]
M.color_code_regex = [[\shwb(\d\+,\s\d\+%,\s\d\+%)]]
local function on_change_argv()
return 'hwb', { hue, whiteness, blackness }
end

View File

@ -14,6 +14,8 @@ local red = 0 -- [0, 1]
local green = 0 -- [0, 1]
local blue = 0 -- [0, 1]
M.color_code_regex = [[\s#[0123456789ABCDEF]\+]]
local function on_change_argv()
return 'rgb', { red, green, blue }
end

View File

@ -6,7 +6,6 @@
-- License: GPLv3
--=============================================================================
local M = {}
local color = require('spacevim.api.color')
@ -19,4 +18,20 @@ function M.get_hex_code(t, code)
return color[t .. '2hex'](unpack(code))
end
function M.get_hsl_l(hex)
local _, _, l = color.rgb2hsl(color.hex2rgb(hex))
return l
end
function M.update_color_code_syntax(r)
local max = 0
local regexes = {}
for _, v in ipairs(r) do
max = math.max(max, v[1])
end
regexes = vim.tbl_map(function(val)
return val[2] .. string.rep('\\s', max - val[1])
end, r)
vim.cmd('syn match SpaceVimPickerCode /' .. table.concat(regexes, '\\|') .. '/')
end
return M

View File

@ -5,8 +5,8 @@ let b:current_syntax = 'spacevim_cpicker'
syntax case ignore
syn match ProcessBar /[?=+]\+/
syn match SpaceVimPickerCode /#[0123456789ABCDEF]\+\|rgb(\d\+,\s\d\+,\s\d\+)\|hsl(\d\+,\s\d\+%,\s\d\+%)\|hsv(\d\+,\s\d\+%,\s\d\+%)\|cmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)\|hwb(\d\+,\s\d\+%,\s\d\+%)/
syn match SpaceVimPickerBackground /=\+/
" syn match SpaceVimPickerCode /\s#[0123456789ABCDEF]\+\|\srgb(\d\+,\s\d\+,\s\d\+)\|\shsl(\d\+,\s\d\+%,\s\d\+%)\|\shsv(\d\+,\s\d\+%,\s\d\+%)\|\scmyk(\d\+%,\s\d\+%,\s\d\+%,\s\d\+%)\|\shwb(\d\+,\s\d\+%,\s\d\+%)/
syn match SpaceVimPickerBackground /=\+\s/
highlight ProcessBar ctermfg=Gray ctermbg=Gray guifg=Gray guibg=Gray

View File

@ -225,7 +225,6 @@ color.hwb2hsl = function(h, w, b)
end
color.hwb2hsv = function(h, w, b)
return color.rgb2hsv(color.hwb2rgb(h, w, b))
end
@ -255,6 +254,14 @@ color.rgb2hex = function(r, g, b)
b = decimalToHex(math.floor(b * 255 + 0.5))
return '#' .. r .. g .. b
end
color.hex2rgb = function(hex)
-- make sure hex is '#[0123456789ABCDEF]\+'
local r, g, b
r = tonumber(string.sub(hex, 2, 3), 16)
g = tonumber(string.sub(hex, 4, 5), 16)
b = tonumber(string.sub(hex, 6, 7), 16)
return r / 255, g / 255, b / 255
end
color.hsv2hex = function(h, s, v)
return color.rgb2hex(color.hsv2rgb(h, s, v))
end

View File

@ -17,21 +17,18 @@ end
function M.fill(str, length, ...)
local v = ''
local rightmost
if string.len(str) <= length then
v = str
else
local rightmost= 0
rightmost= 0
while string.len(string.sub(str, 0, rightmost)) < length do
rightmost = rightmost + 1
end
end
v = string.sub(str, 0, rightmost)
local argvs = ...
local char = ' '
if argvs ~= nil then
char = argvs[1] or char
end
local char = select(1, ...) or ' '
return v .. string.rep(char, length - string.len(v))
end