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:
parent
a9a14ff85b
commit
f35e0da911
@ -35,23 +35,35 @@ local function update_buf_text()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(rst, '')
|
table.insert(rst, '')
|
||||||
|
local color_code_regex = {}
|
||||||
for _, format in ipairs(enabled_formats) do
|
for _, format in ipairs(enabled_formats) do
|
||||||
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
||||||
if ok then
|
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
|
||||||
end
|
end
|
||||||
|
util.update_color_code_syntax(color_code_regex)
|
||||||
local normal_bg = hi.group2dict('Normal').guibg
|
local normal_bg = hi.group2dict('Normal').guibg
|
||||||
hi.hi({
|
local normal_fg = hi.group2dict('Normal').guifg
|
||||||
name = 'SpaceVimPickerCode',
|
if
|
||||||
guifg = color_hi,
|
math.abs(util.get_hsl_l(normal_bg) - util.get_hsl_l(color_hi))
|
||||||
guibg = normal_bg,
|
> math.abs(util.get_hsl_l(color_hi) - util.get_hsl_l(normal_fg))
|
||||||
})
|
then
|
||||||
hi.hi({
|
hi.hi({
|
||||||
name = 'SpaceVimPickerNoText',
|
name = 'SpaceVimPickerCode',
|
||||||
guifg = normal_bg,
|
guifg = color_hi,
|
||||||
guibg = normal_bg,
|
guibg = normal_bg,
|
||||||
})
|
bold = 1,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
hi.hi({
|
||||||
|
name = 'SpaceVimPickerCode',
|
||||||
|
guifg = color_hi,
|
||||||
|
guibg = normal_fg,
|
||||||
|
bold = 1,
|
||||||
|
})
|
||||||
|
end
|
||||||
hi.hi({
|
hi.hi({
|
||||||
name = 'SpaceVimPickerBackground',
|
name = 'SpaceVimPickerBackground',
|
||||||
guibg = color_hi,
|
guibg = color_hi,
|
||||||
@ -66,7 +78,7 @@ local function update_buf_text()
|
|||||||
buf = bufnr,
|
buf = bufnr,
|
||||||
})
|
})
|
||||||
vim.api.nvim_win_set_config(winid, {
|
vim.api.nvim_win_set_config(winid, {
|
||||||
height = #rst + 1
|
height = #rst + 1,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -74,7 +86,9 @@ end
|
|||||||
|
|
||||||
local function copy_color()
|
local function copy_color()
|
||||||
local from, to = vim
|
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('.'))
|
:match_str(vim.fn.getline('.'))
|
||||||
if from then
|
if from then
|
||||||
vim.fn.setreg('+', string.sub(vim.fn.getline('.'), from, to + 1))
|
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, {
|
winid = vim.api.nvim_open_win(bufnr, true, {
|
||||||
relative = 'cursor',
|
relative = 'cursor',
|
||||||
border = 'single',
|
border = 'single',
|
||||||
width = 40,
|
width = 44,
|
||||||
height = 10,
|
height = 10,
|
||||||
row = 1,
|
row = 1,
|
||||||
col = 1,
|
col = 1,
|
||||||
|
@ -15,6 +15,8 @@ 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\+%)]]
|
||||||
|
|
||||||
local function on_change_argv()
|
local function on_change_argv()
|
||||||
return 'cmyk', { cyan, magenta, yellow, black }
|
return 'cmyk', { cyan, magenta, yellow, black }
|
||||||
end
|
end
|
||||||
|
@ -14,6 +14,8 @@ 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\+%)]]
|
||||||
|
|
||||||
local function on_change_argv()
|
local function on_change_argv()
|
||||||
return 'hsl', { hue, saturation, lightness }
|
return 'hsl', { hue, saturation, lightness }
|
||||||
end
|
end
|
||||||
|
@ -14,6 +14,8 @@ 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\+%)]]
|
||||||
|
|
||||||
local function on_change_argv()
|
local function on_change_argv()
|
||||||
return 'hsv', {hue, saturation, value}
|
return 'hsv', {hue, saturation, value}
|
||||||
end
|
end
|
||||||
|
@ -14,6 +14,8 @@ local hue = 0 -- [0, 360]
|
|||||||
local whiteness = 0 -- [0, 100%]
|
local whiteness = 0 -- [0, 100%]
|
||||||
local blackness = 0 -- [0, 100%]
|
local blackness = 0 -- [0, 100%]
|
||||||
|
|
||||||
|
M.color_code_regex = [[\shwb(\d\+,\s\d\+%,\s\d\+%)]]
|
||||||
|
|
||||||
local function on_change_argv()
|
local function on_change_argv()
|
||||||
return 'hwb', { hue, whiteness, blackness }
|
return 'hwb', { hue, whiteness, blackness }
|
||||||
end
|
end
|
||||||
|
@ -14,6 +14,8 @@ local red = 0 -- [0, 1]
|
|||||||
local green = 0 -- [0, 1]
|
local green = 0 -- [0, 1]
|
||||||
local blue = 0 -- [0, 1]
|
local blue = 0 -- [0, 1]
|
||||||
|
|
||||||
|
M.color_code_regex = [[\s#[0123456789ABCDEF]\+]]
|
||||||
|
|
||||||
local function on_change_argv()
|
local function on_change_argv()
|
||||||
return 'rgb', { red, green, blue }
|
return 'rgb', { red, green, blue }
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
-- License: GPLv3
|
-- License: GPLv3
|
||||||
--=============================================================================
|
--=============================================================================
|
||||||
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local color = require('spacevim.api.color')
|
local color = require('spacevim.api.color')
|
||||||
@ -19,4 +18,20 @@ 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)
|
||||||
|
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
|
return M
|
||||||
|
@ -5,8 +5,8 @@ let b:current_syntax = 'spacevim_cpicker'
|
|||||||
syntax case ignore
|
syntax case ignore
|
||||||
|
|
||||||
syn match ProcessBar /[?=+]\+/
|
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 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 /=\+/
|
syn match SpaceVimPickerBackground /=\+\s/
|
||||||
|
|
||||||
highlight ProcessBar ctermfg=Gray ctermbg=Gray guifg=Gray guibg=Gray
|
highlight ProcessBar ctermfg=Gray ctermbg=Gray guifg=Gray guibg=Gray
|
||||||
|
|
||||||
|
@ -225,7 +225,6 @@ color.hwb2hsl = function(h, w, b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
color.hwb2hsv = function(h, w, b)
|
color.hwb2hsv = function(h, w, b)
|
||||||
|
|
||||||
return color.rgb2hsv(color.hwb2rgb(h, w, b))
|
return color.rgb2hsv(color.hwb2rgb(h, w, b))
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -255,6 +254,14 @@ color.rgb2hex = function(r, g, b)
|
|||||||
b = decimalToHex(math.floor(b * 255 + 0.5))
|
b = decimalToHex(math.floor(b * 255 + 0.5))
|
||||||
return '#' .. r .. g .. b
|
return '#' .. r .. g .. b
|
||||||
end
|
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)
|
color.hsv2hex = function(h, s, v)
|
||||||
return color.rgb2hex(color.hsv2rgb(h, s, v))
|
return color.rgb2hex(color.hsv2rgb(h, s, v))
|
||||||
end
|
end
|
||||||
|
@ -17,21 +17,18 @@ end
|
|||||||
|
|
||||||
function M.fill(str, length, ...)
|
function M.fill(str, length, ...)
|
||||||
local v = ''
|
local v = ''
|
||||||
|
local rightmost
|
||||||
if string.len(str) <= length then
|
if string.len(str) <= length then
|
||||||
v = str
|
v = str
|
||||||
else
|
else
|
||||||
local rightmost= 0
|
rightmost= 0
|
||||||
while string.len(string.sub(str, 0, rightmost)) < length do
|
while string.len(string.sub(str, 0, rightmost)) < length do
|
||||||
rightmost = rightmost + 1
|
rightmost = rightmost + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
v = string.sub(str, 0, rightmost)
|
v = string.sub(str, 0, rightmost)
|
||||||
local argvs = ...
|
local char = select(1, ...) or ' '
|
||||||
local char = ' '
|
|
||||||
if argvs ~= nil then
|
|
||||||
char = argvs[1] or char
|
|
||||||
end
|
|
||||||
return v .. string.rep(char, length - string.len(v))
|
return v .. string.rep(char, length - string.len(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user