From f35e0da911fd5b1bfcde8b82d25893af41ea596e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 14 Jul 2024 19:01:57 +0800 Subject: [PATCH] feat(cpicker): change color code background --- bundle/cpicker.nvim/lua/cpicker.lua | 42 ++++++++++++------- .../cpicker.nvim/lua/cpicker/formats/cmyk.lua | 2 + .../cpicker.nvim/lua/cpicker/formats/hsl.lua | 2 + .../cpicker.nvim/lua/cpicker/formats/hsv.lua | 2 + .../cpicker.nvim/lua/cpicker/formats/hwb.lua | 2 + .../cpicker.nvim/lua/cpicker/formats/rgb.lua | 2 + bundle/cpicker.nvim/lua/cpicker/util.lua | 17 +++++++- .../cpicker.nvim/syntax/spacevim_cpicker.vim | 4 +- lua/spacevim/api/color.lua | 9 +++- lua/spacevim/api/data/string.lua | 9 ++-- 10 files changed, 67 insertions(+), 24 deletions(-) diff --git a/bundle/cpicker.nvim/lua/cpicker.lua b/bundle/cpicker.nvim/lua/cpicker.lua index c3a15d57b..4529852f2 100644 --- a/bundle/cpicker.nvim/lua/cpicker.lua +++ b/bundle/cpicker.nvim/lua/cpicker.lua @@ -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 - hi.hi({ - name = 'SpaceVimPickerCode', - guifg = color_hi, - guibg = normal_bg, - }) - hi.hi({ - name = 'SpaceVimPickerNoText', - guifg = normal_bg, - guibg = normal_bg, - }) + 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 = '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, diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua b/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua index bcb1d99bc..f7a339113 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua index d727b47a3..8c4aa8ec1 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua index 258d0ada5..0f1c49c38 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua index 44cf5ea5c..8987a3761 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua index d75403477..d07b09c89 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker/util.lua b/bundle/cpicker.nvim/lua/cpicker/util.lua index 661c58444..e5987ba6f 100644 --- a/bundle/cpicker.nvim/lua/cpicker/util.lua +++ b/bundle/cpicker.nvim/lua/cpicker/util.lua @@ -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 diff --git a/bundle/cpicker.nvim/syntax/spacevim_cpicker.vim b/bundle/cpicker.nvim/syntax/spacevim_cpicker.vim index 805983ed7..04d6b020c 100644 --- a/bundle/cpicker.nvim/syntax/spacevim_cpicker.vim +++ b/bundle/cpicker.nvim/syntax/spacevim_cpicker.vim @@ -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 diff --git a/lua/spacevim/api/color.lua b/lua/spacevim/api/color.lua index 2bbf4c4f4..95db00771 100644 --- a/lua/spacevim/api/color.lua +++ b/lua/spacevim/api/color.lua @@ -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 diff --git a/lua/spacevim/api/data/string.lua b/lua/spacevim/api/data/string.lua index c02479dd6..205e9419f 100644 --- a/lua/spacevim/api/data/string.lua +++ b/lua/spacevim/api/data/string.lua @@ -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