diff --git a/bundle/cpicker.nvim/lua/cpicker.lua b/bundle/cpicker.nvim/lua/cpicker.lua index 0e114c406..0b6db0fd5 100644 --- a/bundle/cpicker.nvim/lua/cpicker.lua +++ b/bundle/cpicker.nvim/lua/cpicker.lua @@ -133,7 +133,9 @@ M.picker = function(formats) vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr, }) - vim.api.nvim_set_option_value('filetype', 'spacevim_cpicker', { + -- use set syntax instead of filetype + -- if using filetype, when open cpicker first time the SpaceVimPickerCode syntax is cleared + vim.api.nvim_set_option_value('syntax', 'spacevim_cpicker', { buf = bufnr, }) vim.api.nvim_set_option_value('bufhidden', 'wipe', { diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua index da2e05ecf..df7841fc3 100644 --- a/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua +++ b/bundle/cpicker.nvim/lua/cpicker/formats/rgb.lua @@ -44,48 +44,30 @@ function M.color_code() return ' =========' .. ' ' .. color.rgb2hex(red, green, blue) end -local function increase(c) - if c <= 1 - 1 / 255 then - c = c + 1 / 255 - elseif c < 1 then - c = 1 - end - return c -end - -local function reduce(c) - if c >= 1 / 255 then - c = c - 1 / 255 - elseif c > 0 then - c = 0 - end - return c -end - local function increase_rgb_red() - red = increase(red) + red = util.increase(red, 255) return on_change_argv() end local function reduce_rgb_red() - red = reduce(red) + red = util.reduce(red, 255) return on_change_argv() end local function increase_rgb_green() - green = increase(green) + green = util.increase(green, 255) return on_change_argv() end local function reduce_rgb_green() - green = reduce(green) + green = util.reduce(green, 255) return on_change_argv() end local function increase_rgb_blue() - blue = increase(blue) + blue = util.increase(blue, 255) return on_change_argv() end local function reduce_rgb_blue() - blue = reduce(blue) + blue = util.reduce(blue, 255) return on_change_argv() end function M.increase_reduce_functions() diff --git a/bundle/cpicker.nvim/lua/cpicker/formats/xyz.lua b/bundle/cpicker.nvim/lua/cpicker/formats/xyz.lua new file mode 100644 index 000000000..c3e2a1022 --- /dev/null +++ b/bundle/cpicker.nvim/lua/cpicker/formats/xyz.lua @@ -0,0 +1,92 @@ +--============================================================================= +-- xyz.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 x = 0 +local y = 0 +local z = 0 + +M.color_code_regex = [[\scolor(xyz,\s\d\+%,\s\d\+%,\s\d\+%)]] + +local function on_change_argv() + return 'xyz', { x, y, z } +end + +function M.buf_text() + local rst = {} + local h_bar = util.generate_bar(x, '+') + local s_bar = util.generate_bar(y, '+') + local l_bar = util.generate_bar(z, '+') + table.insert( + rst, + 'XYZ: X: ' .. string.format('%3s', math.floor(x * 100 + 0.5)) .. '% ' .. h_bar + ) + table.insert( + rst, + ' Y: ' .. string.format('%3s', math.floor(y * 100 + 0.5)) .. '% ' .. s_bar + ) + table.insert( + rst, + ' Z: ' .. string.format('%3s', math.floor(z * 100 + 0.5)) .. '% ' .. l_bar + ) + return rst +end + +function M.color_code() + return + ' =========' .. string.format( + ' color(xyz, %s%%, %s%%, %s%%)', + math.floor(x * 100 + 0.5), + math.floor(y * 100 + 0.5), + math.floor(z * 100 + 0.5) + ) +end +local function increase_x() + x = util.increase(x) + return on_change_argv() +end +local function reduce_x() + x = util.reduce(x) + return on_change_argv() +end +local function increase_y() + y = util.increase(y) + return on_change_argv() +end +local function reduce_y() + y = util.reduce(y) + return on_change_argv() +end +local function increase_z() + z = util.increase(z) + return on_change_argv() +end +local function reduce_z() + z = util.reduce(z) + return on_change_argv() +end +function M.increase_reduce_functions() + return { + { increase_x, reduce_x }, + { increase_y, reduce_y }, + { increase_z, reduce_z }, + } +end + +function M.on_change(f, code) + if f == 'xyz' then + x, y, z = unpack(code) + return + end + x, y, z = color[f .. '2xyz'](unpack(code)) +end + +return M diff --git a/bundle/cpicker.nvim/lua/cpicker/util.lua b/bundle/cpicker.nvim/lua/cpicker/util.lua index 6172944dc..2418fd3ee 100644 --- a/bundle/cpicker.nvim/lua/cpicker/util.lua +++ b/bundle/cpicker.nvim/lua/cpicker/util.lua @@ -22,6 +22,37 @@ function M.get_hsl_l(hex) local _, _, l = color.rgb2hsl(color.hex2rgb(hex)) return l end + +function M.increase(v, step, min, max) + step = step or 100 + min = min or 0 + max = max or 1 + local sep = (max - min) / step + v = (math.floor(v / sep + 0.5) + 1) * sep + if v >= max then + return max + elseif v <= min then + return min + else + return v + end +end + +function M.reduce(v, step, min, max) + step = step or 100 + min = min or 0 + max = max or 1 + local sep = (max - min) / step + v = (math.floor(v / sep + 0.5) - 1) * sep + if v >= max then + return max + elseif v <= min then + return min + else + return v + end +end + function M.update_color_code_syntax(r) local max = 0 local regexes = {} diff --git a/lua/spacevim/api/color.lua b/lua/spacevim/api/color.lua index 9d8aa0a89..cc34b2d71 100644 --- a/lua/spacevim/api/color.lua +++ b/lua/spacevim/api/color.lua @@ -384,6 +384,40 @@ function color.xyz2linear(x, y, z) return product(xyz2linear, { x, y, z }) end +function color.rgb2xyz(r, g, b) + return color.linear2xyz(color.rgb2linear(r, g, b)) +end +function color.xyz2rgb(x, y, z) + return color.linear2rgb(color.xyz2linear(x, y, z)) +end +function color.xyz2hex(x, y, z) + return color.rgb2hex(color.xyz2rgb(x, y, z)) +end +function color.xyz2hsv(x, y, z) + return color.rgb2hsv(color.xyz2rgb(x, y, z)) +end +function color.hsv2xyz(h, s, v) + return color.rgb2xyz(color.hsv2rgb(h, s, v)) +end +function color.xyz2hsl(x, y, z) + return color.rgb2hsl(color.xyz2rgb(x, y, z)) +end +function color.hsl2xyz(h, s, l) + return color.rgb2xyz(color.hsl2rgb(h, s, l)) +end +function color.xyz2cmyk(x, y, z) + return color.rgb2cmyk(color.xyz2rgb(x, y, z)) +end +function color.cmyk2xyz(c, m, y, k) + return color.rgb2xyz(color.cmyk2rgb(c, m, y, k)) +end +function color.xyz2hwb(x, y, z) + return color.rgb2hwb(color.xyz2rgb(x, y, z)) +end +function color.hwb2xyz(h, w, b) + return color.rgb2xyz(color.hwb2rgb(h, w, b)) +end + -------------------------------------------------------------------------------- -- Lab color space --------------------------------------------------------------------------------