From dcde04c4540f8f5f55e039776dd6f7148f33f59b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 14 Jul 2024 22:32:55 +0800 Subject: [PATCH] feat(cpicker): picker color from cursor --- autoload/SpaceVim/layers/tools/cpicker.vim | 2 +- bundle/cpicker.nvim/lua/cpicker.lua | 10 +- .../cpicker.nvim/lua/cpicker/formats/cmyk.lua | 257 +++++++++--------- .../cpicker.nvim/lua/cpicker/formats/hsl.lua | 227 ++++++++-------- .../cpicker.nvim/lua/cpicker/formats/hsv.lua | 227 ++++++++-------- .../cpicker.nvim/lua/cpicker/formats/hwb.lua | 1 + .../cpicker.nvim/lua/cpicker/formats/rgb.lua | 1 + bundle/cpicker.nvim/lua/cpicker/util.lua | 118 +++++--- bundle/cpicker.nvim/plugin/cpicker.lua | 39 +-- 9 files changed, 472 insertions(+), 410 deletions(-) diff --git a/autoload/SpaceVim/layers/tools/cpicker.vim b/autoload/SpaceVim/layers/tools/cpicker.vim index 312ef8dc6..bd408a309 100644 --- a/autoload/SpaceVim/layers/tools/cpicker.vim +++ b/autoload/SpaceVim/layers/tools/cpicker.vim @@ -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 diff --git a/bundle/cpicker.nvim/lua/cpicker.lua b/bundle/cpicker.nvim/lua/cpicker.lua index 4529852f2..845e80646 100644 --- a/bundle/cpicker.nvim/lua/cpicker.lua +++ b/bundle/cpicker.nvim/lua/cpicker.lua @@ -125,7 +125,11 @@ local function reduce() end 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)) 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 diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua b/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua index f7a339113..23aa27ad5 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/cmyk.lua @@ -1,128 +1,129 @@ ---============================================================================= --- cmyk.lua --- --- Copyright (c) 2019-2024 Wang Shidong & Contributors --- Author: Wang Shidong < wsdjeg@outlook.com > --- URL: https://spacevim.org --- License: GPLv3 ---============================================================================= -local M = {} - -local color = require('spacevim.api.color') -local util = require('cpicker.util') - -local cyan = 0 -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 - -function M.buf_text() - local rst = {} - local c_bar = util.generate_bar(cyan, '+') - local m_bar = util.generate_bar(magenta, '+') - local y_bar = util.generate_bar(yellow, '+') - 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, ' 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, ' K: ' .. string.format('%4s', math.floor(black * 100 + 0.5)) .. ' ' .. k_bar) - return rst -end - -function M.color_code() - return - ' =========' .. string.format( - ' cmyk(%s%%, %s%%, %s%%, %s%%)', - math.floor(cyan * 100 + 0.5), - math.floor(magenta * 100 + 0.5), - math.floor(yellow * 100 + 0.5), - math.floor(black * 100 + 0.5) - ) -end - -local function increase_cyan() - if cyan <= 0.99 then - cyan = cyan + 0.01 - elseif cyan < 1 then - cyan = 1 - end - return on_change_argv() -end -local function reduce_cyan() - if cyan > 0.01 then - cyan = cyan - 0.01 - elseif cyan > 0 then - cyan = 0 - end - return on_change_argv() -end -local function increase_magenta() - if magenta <= 0.99 then - magenta = magenta + 0.01 - elseif magenta < 1 then - magenta = 1 - end - return on_change_argv() -end -local function reduce_magenta() - if magenta > 0.01 then - magenta = magenta - 0.01 - elseif magenta > 0 then - magenta = 0 - end - return on_change_argv() -end -local function increase_yellow() - if yellow <= 0.99 then - yellow = yellow + 0.01 - elseif yellow < 1 then - yellow = 1 - end - return on_change_argv() -end -local function reduce_yellow() - if yellow > 0.01 then - yellow = yellow - 0.01 - elseif yellow > 0 then - yellow = 0 - end - return on_change_argv() -end -local function increase_black() - if black <= 0.99 then - black = black + 0.01 - elseif black < 1 then - black = 1 - end - return on_change_argv() -end -local function reduce_black() - if black > 0.01 then - black = black - 0.01 - elseif black > 0 then - black = 0 - end - return on_change_argv() -end -function M.increase_reduce_functions() - return { - { increase_cyan, reduce_cyan }, - { increase_magenta, reduce_magenta }, - { increase_yellow, reduce_yellow }, - { increase_black, reduce_black }, - } -end - -function M.on_change(f, code) - if f == 'cmyk' then - return - end - cyan, magenta, yellow, black = color[f .. '2cmyk'](unpack(code)) -end - -return M +--============================================================================= +-- cmyk.lua --- +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= +local M = {} + +local color = require('spacevim.api.color') +local util = require('cpicker.util') + +local cyan = 0 +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 + +function M.buf_text() + local rst = {} + local c_bar = util.generate_bar(cyan, '+') + local m_bar = util.generate_bar(magenta, '+') + local y_bar = util.generate_bar(yellow, '+') + 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, ' 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, ' K: ' .. string.format('%4s', math.floor(black * 100 + 0.5)) .. ' ' .. k_bar) + return rst +end + +function M.color_code() + return + ' =========' .. string.format( + ' cmyk(%s%%, %s%%, %s%%, %s%%)', + math.floor(cyan * 100 + 0.5), + math.floor(magenta * 100 + 0.5), + math.floor(yellow * 100 + 0.5), + math.floor(black * 100 + 0.5) + ) +end + +local function increase_cyan() + if cyan <= 0.99 then + cyan = cyan + 0.01 + elseif cyan < 1 then + cyan = 1 + end + return on_change_argv() +end +local function reduce_cyan() + if cyan > 0.01 then + cyan = cyan - 0.01 + elseif cyan > 0 then + cyan = 0 + end + return on_change_argv() +end +local function increase_magenta() + if magenta <= 0.99 then + magenta = magenta + 0.01 + elseif magenta < 1 then + magenta = 1 + end + return on_change_argv() +end +local function reduce_magenta() + if magenta > 0.01 then + magenta = magenta - 0.01 + elseif magenta > 0 then + magenta = 0 + end + return on_change_argv() +end +local function increase_yellow() + if yellow <= 0.99 then + yellow = yellow + 0.01 + elseif yellow < 1 then + yellow = 1 + end + return on_change_argv() +end +local function reduce_yellow() + if yellow > 0.01 then + yellow = yellow - 0.01 + elseif yellow > 0 then + yellow = 0 + end + return on_change_argv() +end +local function increase_black() + if black <= 0.99 then + black = black + 0.01 + elseif black < 1 then + black = 1 + end + return on_change_argv() +end +local function reduce_black() + if black > 0.01 then + black = black - 0.01 + elseif black > 0 then + black = 0 + end + return on_change_argv() +end +function M.increase_reduce_functions() + return { + { increase_cyan, reduce_cyan }, + { increase_magenta, reduce_magenta }, + { increase_yellow, reduce_yellow }, + { increase_black, reduce_black }, + } +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)) +end + +return M diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua index 8c4aa8ec1..1b593962e 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hsl.lua @@ -1,113 +1,114 @@ ---============================================================================= --- hsl.lua --- Copyright (c) 2019-2024 Wang Shidong & Contributors --- Author: Wang Shidong < wsdjeg@outlook.com > --- URL: https://spacevim.org --- License: GPLv3 ---============================================================================= -local M = {} - -local color = require('spacevim.api.color') -local util = require('cpicker.util') - -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 - -function M.buf_text() - local rst = {} - local h_bar = util.generate_bar(hue, '+', 360) - local s_bar = util.generate_bar(saturation, '+') - 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, - ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar - ) - table.insert( - rst, - ' L: ' .. string.format('%3s', math.floor(lightness * 100 + 0.5)) .. '% ' .. l_bar - ) - return rst -end - -function M.color_code() - return - ' =========' .. string.format( - ' hsl(%s, %s%%, %s%%)', - math.floor(hue + 0.5), - math.floor(saturation * 100 + 0.5), - math.floor(lightness * 100 + 0.5) - ) -end - -local function increase_hsl_h() - if hue <= 359 then - hue = hue + 1 - elseif hue < 360 then - hue = 360 - end - return on_change_argv() -end -local function reduce_hsl_h() - if hue >= 1 then - hue = hue - 1 - elseif hue > 0 then - hue = 0 - end - return on_change_argv() -end -local function increase_hsl_s() - if saturation <= 0.99 then - saturation = saturation + 0.01 - elseif saturation < 1 then - saturation = 1 - end - return on_change_argv() -end -local function reduce_hsl_s() - if saturation >= 0.01 then - saturation = saturation - 0.01 - elseif saturation > 0 and saturation < 0.01 then - saturation = 0 - end - return on_change_argv() -end -local function increase_hsl_l() - if lightness <= 0.99 then - lightness = lightness + 0.01 - elseif lightness < 1 then - lightness = 1 - end - return on_change_argv() -end -local function reduce_hsl_l() - if lightness >= 0.01 then - lightness = lightness - 0.01 - elseif lightness > 0 and lightness < 0.01 then - lightness = 0 - end - return on_change_argv() -end -function M.increase_reduce_functions() - return { - { increase_hsl_h, reduce_hsl_h }, - { increase_hsl_s, reduce_hsl_s }, - { increase_hsl_l, reduce_hsl_l }, - } -end - -function M.on_change(f, code) - if f == 'hsl' then - return - end - hue, saturation, lightness = color[f .. '2hsl'](unpack(code)) -end - -return M +--============================================================================= +-- hsl.lua +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= +local M = {} + +local color = require('spacevim.api.color') +local util = require('cpicker.util') + +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 + +function M.buf_text() + local rst = {} + local h_bar = util.generate_bar(hue, '+', 360) + local s_bar = util.generate_bar(saturation, '+') + 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, + ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar + ) + table.insert( + rst, + ' L: ' .. string.format('%3s', math.floor(lightness * 100 + 0.5)) .. '% ' .. l_bar + ) + return rst +end + +function M.color_code() + return + ' =========' .. string.format( + ' hsl(%s, %s%%, %s%%)', + math.floor(hue + 0.5), + math.floor(saturation * 100 + 0.5), + math.floor(lightness * 100 + 0.5) + ) +end + +local function increase_hsl_h() + if hue <= 359 then + hue = hue + 1 + elseif hue < 360 then + hue = 360 + end + return on_change_argv() +end +local function reduce_hsl_h() + if hue >= 1 then + hue = hue - 1 + elseif hue > 0 then + hue = 0 + end + return on_change_argv() +end +local function increase_hsl_s() + if saturation <= 0.99 then + saturation = saturation + 0.01 + elseif saturation < 1 then + saturation = 1 + end + return on_change_argv() +end +local function reduce_hsl_s() + if saturation >= 0.01 then + saturation = saturation - 0.01 + elseif saturation > 0 and saturation < 0.01 then + saturation = 0 + end + return on_change_argv() +end +local function increase_hsl_l() + if lightness <= 0.99 then + lightness = lightness + 0.01 + elseif lightness < 1 then + lightness = 1 + end + return on_change_argv() +end +local function reduce_hsl_l() + if lightness >= 0.01 then + lightness = lightness - 0.01 + elseif lightness > 0 and lightness < 0.01 then + lightness = 0 + end + return on_change_argv() +end +function M.increase_reduce_functions() + return { + { increase_hsl_h, reduce_hsl_h }, + { increase_hsl_s, reduce_hsl_s }, + { increase_hsl_l, reduce_hsl_l }, + } +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)) +end + +return M diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua index 0f1c49c38..979085c29 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hsv.lua @@ -1,113 +1,114 @@ ---============================================================================= --- hsv.lua --- --- Copyright (c) 2019-2024 Wang Shidong & Contributors --- Author: Wang Shidong < wsdjeg@outlook.com > --- URL: https://spacevim.org --- License: GPLv3 ---============================================================================= -local M = {} - -local color = require('spacevim.api.color') -local util = require('cpicker.util') - -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 - -function M.buf_text() - - local rst = {} - local h_bar = util.generate_bar(hue, '+', 360) - local s_bar = util.generate_bar(saturation, '+') - 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, - ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar - ) - table.insert( - rst, - ' V: ' .. string.format('%3s', math.floor(value * 100 + 0.5)) .. '% ' .. l_bar - ) - return rst -end - -function M.color_code() - return - ' =========' .. string.format( - ' hsv(%s, %s%%, %s%%)', - math.floor(hue + 0.5), - math.floor(saturation * 100 + 0.5), - math.floor(value * 100 + 0.5) - ) - -end -local function increase_hsl_h() - if hue <= 359 then - hue = hue + 1 - elseif hue < 360 then hue = 360 - end - return on_change_argv() -end -local function reduce_hsl_h() - if hue >= 1 then - hue = hue - 1 - elseif hue > 0 then - hue = 0 - end - return on_change_argv() -end -local function increase_hsl_s() - if saturation <= 0.99 then - saturation = saturation + 0.01 - elseif saturation < 1 then - saturation = 1 - end - return on_change_argv() -end -local function reduce_hsl_s() - if saturation >= 0.01 then - saturation = saturation - 0.01 - elseif saturation > 0 and saturation < 0.01 then - saturation = 0 - end - return on_change_argv() -end -local function increase_hsl_v() - if value <= 0.99 then - value = value + 0.01 - elseif value < 1 then - value = 1 - end - return on_change_argv() -end -local function reduce_hsl_v() - if value >= 0.01 then - value = value - 0.01 - elseif value > 0 and value < 0.01 then - value = 0 - end - return on_change_argv() -end -function M.increase_reduce_functions() - return { - { increase_hsl_h, reduce_hsl_h }, - { increase_hsl_s, reduce_hsl_s }, - { increase_hsl_v, reduce_hsl_v }, - } -end - -function M.on_change(f, code) - if f == 'hsv' then - return - end - hue, saturation, value = color[f .. '2hsv'](unpack(code)) -end - -return M +--============================================================================= +-- hsv.lua --- +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= +local M = {} + +local color = require('spacevim.api.color') +local util = require('cpicker.util') + +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 + +function M.buf_text() + + local rst = {} + local h_bar = util.generate_bar(hue, '+', 360) + local s_bar = util.generate_bar(saturation, '+') + 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, + ' S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar + ) + table.insert( + rst, + ' V: ' .. string.format('%3s', math.floor(value * 100 + 0.5)) .. '% ' .. l_bar + ) + return rst +end + +function M.color_code() + return + ' =========' .. string.format( + ' hsv(%s, %s%%, %s%%)', + math.floor(hue + 0.5), + math.floor(saturation * 100 + 0.5), + math.floor(value * 100 + 0.5) + ) + +end +local function increase_hsl_h() + if hue <= 359 then + hue = hue + 1 + elseif hue < 360 then hue = 360 + end + return on_change_argv() +end +local function reduce_hsl_h() + if hue >= 1 then + hue = hue - 1 + elseif hue > 0 then + hue = 0 + end + return on_change_argv() +end +local function increase_hsl_s() + if saturation <= 0.99 then + saturation = saturation + 0.01 + elseif saturation < 1 then + saturation = 1 + end + return on_change_argv() +end +local function reduce_hsl_s() + if saturation >= 0.01 then + saturation = saturation - 0.01 + elseif saturation > 0 and saturation < 0.01 then + saturation = 0 + end + return on_change_argv() +end +local function increase_hsl_v() + if value <= 0.99 then + value = value + 0.01 + elseif value < 1 then + value = 1 + end + return on_change_argv() +end +local function reduce_hsl_v() + if value >= 0.01 then + value = value - 0.01 + elseif value > 0 and value < 0.01 then + value = 0 + end + return on_change_argv() +end +function M.increase_reduce_functions() + return { + { increase_hsl_h, reduce_hsl_h }, + { increase_hsl_s, reduce_hsl_s }, + { increase_hsl_v, reduce_hsl_v }, + } +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)) +end + +return M diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua b/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua index 8987a3761..f717f8047 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/hwb.lua @@ -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)) diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua index d07b09c89..c5c445677 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua @@ -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)) diff --git a/bundle/cpicker.nvim/lua/cpicker/util.lua b/bundle/cpicker.nvim/lua/cpicker/util.lua index e5987ba6f..6097cf46e 100644 --- a/bundle/cpicker.nvim/lua/cpicker/util.lua +++ b/bundle/cpicker.nvim/lua/cpicker/util.lua @@ -1,37 +1,81 @@ ---============================================================================= --- util.lua --- Copyright (c) 2019-2024 Wang Shidong & Contributors --- Author: Wang Shidong < wsdjeg@outlook.com > --- URL: https://spacevim.org --- License: GPLv3 ---============================================================================= - -local M = {} - -local color = require('spacevim.api.color') - -function M.generate_bar(n, char, m) - return string.rep(char, math.floor(24 * n / (m or 1))) -end - -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 +--============================================================================= +-- util.lua +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +local M = {} + +local color = require('spacevim.api.color') + +function M.generate_bar(n, char, m) + return string.rep(char, math.floor(24 * n / (m or 1))) +end + +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 + +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 diff --git a/bundle/cpicker.nvim/plugin/cpicker.lua b/bundle/cpicker.nvim/plugin/cpicker.lua index 216b09730..29da4d690 100644 --- a/bundle/cpicker.nvim/plugin/cpicker.lua +++ b/bundle/cpicker.nvim/plugin/cpicker.lua @@ -1,17 +1,22 @@ ---============================================================================= --- cpicker.lua --- Copyright (c) 2019-2024 Wang Shidong & Contributors --- Author: Wang Shidong < wsdjeg@outlook.com > --- URL: https://spacevim.org --- License: GPLv3 ---============================================================================= - -if vim.api.nvim_create_user_command then - local function complete(...) - return {'all', 'rgb', 'hsl'} - end - vim.api.nvim_create_user_command('Cpicker', function(opt) - require('cpicker').picker(opt.fargs) - - end, { nargs = '*', complete = complete }) -end +--============================================================================= +-- cpicker.lua +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +if vim.api.nvim_create_user_command then + local function complete(...) + return {'all', 'rgb', 'hsl'} + end + vim.api.nvim_create_user_command('Cpicker', function(opt) + 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