1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 20:10:05 +08:00

feat(cpicker): add xyz color space

This commit is contained in:
Eric Wong 2024-07-21 22:03:38 +08:00
parent 32f30ddb78
commit e291cd136e
5 changed files with 166 additions and 25 deletions

View File

@ -133,7 +133,9 @@ M.picker = function(formats)
vim.api.nvim_set_option_value('buftype', 'nofile', { vim.api.nvim_set_option_value('buftype', 'nofile', {
buf = bufnr, 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, buf = bufnr,
}) })
vim.api.nvim_set_option_value('bufhidden', 'wipe', { vim.api.nvim_set_option_value('bufhidden', 'wipe', {

View File

@ -44,48 +44,30 @@ function M.color_code()
return ' =========' .. ' ' .. color.rgb2hex(red, green, blue) return ' =========' .. ' ' .. color.rgb2hex(red, green, blue)
end 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() local function increase_rgb_red()
red = increase(red) red = util.increase(red, 255)
return on_change_argv() return on_change_argv()
end end
local function reduce_rgb_red() local function reduce_rgb_red()
red = reduce(red) red = util.reduce(red, 255)
return on_change_argv() return on_change_argv()
end end
local function increase_rgb_green() local function increase_rgb_green()
green = increase(green) green = util.increase(green, 255)
return on_change_argv() return on_change_argv()
end end
local function reduce_rgb_green() local function reduce_rgb_green()
green = reduce(green) green = util.reduce(green, 255)
return on_change_argv() return on_change_argv()
end end
local function increase_rgb_blue() local function increase_rgb_blue()
blue = increase(blue) blue = util.increase(blue, 255)
return on_change_argv() return on_change_argv()
end end
local function reduce_rgb_blue() local function reduce_rgb_blue()
blue = reduce(blue) blue = util.reduce(blue, 255)
return on_change_argv() return on_change_argv()
end end
function M.increase_reduce_functions() function M.increase_reduce_functions()

View File

@ -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

View File

@ -22,6 +22,37 @@ function M.get_hsl_l(hex)
local _, _, l = color.rgb2hsl(color.hex2rgb(hex)) local _, _, l = color.rgb2hsl(color.hex2rgb(hex))
return l return l
end 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) function M.update_color_code_syntax(r)
local max = 0 local max = 0
local regexes = {} local regexes = {}

View File

@ -384,6 +384,40 @@ function color.xyz2linear(x, y, z)
return product(xyz2linear, { x, y, z }) return product(xyz2linear, { x, y, z })
end 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 -- Lab color space
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------