1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-13 15:37:58 +08:00

feat(cpicker): add hwb color-mix

This commit is contained in:
Eric Wong 2024-07-18 18:31:54 +08:00
parent 85f97eedb2
commit b0f1e65b35
2 changed files with 393 additions and 387 deletions

View File

@ -6,7 +6,8 @@ local color_mix_color2
local color_mix_color3 local color_mix_color3
local color_mix_p1 = 0.5 local color_mix_p1 = 0.5
local color_mix_p2 = 0.5 local color_mix_p2 = 0.5
local available_methods = { 'srgb', 'hsl' } -- https://developer.mozilla.org/en-US/docs/Web/CSS/color-interpolation-method
local available_methods = { 'srgb', 'hsl', 'hwb' }
local available_hue_methods = { 'shorter', 'longer', 'increasing', 'decreasing' } local available_hue_methods = { 'shorter', 'longer', 'increasing', 'decreasing' }
local method = 'srgb' local method = 'srgb'
local hue_interpolation_method = 'shorter' local hue_interpolation_method = 'shorter'
@ -61,9 +62,16 @@ local function update_color_mix_buftext()
p2 = color_mix_p2 / (color_mix_p1 + color_mix_p2) p2 = color_mix_p2 / (color_mix_p1 + color_mix_p2)
end end
r3, g3, b3 = r1 * p1 + r2 * p2, g1 * p1 + g2 * p2, b1 * p1 + b2 * p2 r3, g3, b3 = r1 * p1 + r2 * p2, g1 * p1 + g2 * p2, b1 * p1 + b2 * p2
elseif method == 'hsl' then elseif method == 'hsl' or method == 'hwb' then
local h1, s1, l1 = color.rgb2hsl(color.hex2rgb(color_mix_color1)) local h1, s1, l1, w1, b1
local h2, s2, l2 = color.rgb2hsl(color.hex2rgb(color_mix_color2)) local h2, s2, l2, w2, b2
if method == 'hsl' then
h1, s1, l1 = color.rgb2hsl(color.hex2rgb(color_mix_color1))
h2, s2, l2 = color.rgb2hsl(color.hex2rgb(color_mix_color2))
elseif method == 'hwb' then
h1, w1, b1 = color.rgb2hwb(color.hex2rgb(color_mix_color1))
h2, w2, b2 = color.rgb2hwb(color.hex2rgb(color_mix_color2))
end
local h3 local h3
local p1, p2 local p1, p2
if color_mix_p1 == 0 and color_mix_p2 == 0 then if color_mix_p1 == 0 and color_mix_p2 == 0 then
@ -117,8 +125,13 @@ local function update_color_mix_buftext()
if h3 >= 360 then if h3 >= 360 then
h3 = h3 - 360 h3 = h3 - 360
end end
if method == 'hsl' then
r3, g3, b3 = color.hsl2rgb(h3, s1 * p1 + s2 * p2, l1 * p1 + l2 * p2) r3, g3, b3 = color.hsl2rgb(h3, s1 * p1 + s2 * p2, l1 * p1 + l2 * p2)
elseif method == 'hwb' then
r3, g3, b3 = color.hwb2rgb(h3, w1 * p1 + w2 * p2, b1 * p1 + b2 * p2)
end end
end
-- 验证结果 https://products.aspose.app/svg/zh/color-mixer
color_mix_color3 = color.rgb2hex(r3, g3, b3) color_mix_color3 = color.rgb2hex(r3, g3, b3)
local normal_bg = hi.group2dict('Normal').guibg local normal_bg = hi.group2dict('Normal').guibg
local normal_fg = hi.group2dict('Normal').guifg local normal_fg = hi.group2dict('Normal').guifg
@ -171,7 +184,9 @@ local function update_color_mix_buftext()
table.insert(rst, ' ') table.insert(rst, ' ')
table.insert( table.insert(
rst, rst,
' ======= ' .. color_mix_color3 .. ' ' ' ======= '
.. color_mix_color3
.. ' '
) )
table.insert( table.insert(
rst, rst,
@ -287,9 +302,8 @@ local function reduce_p()
end end
local function copy_color_mix() local function copy_color_mix()
local from, to = vim local from, to =
.regex([[#[0123456789ABCDEF]\+\|color-mix([^)]*)]]) vim.regex([[#[0123456789ABCDEF]\+\|color-mix([^)]*)]]):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))
notify.notify('copied:' .. string.sub(vim.fn.getline('.'), from, to + 1)) notify.notify('copied:' .. string.sub(vim.fn.getline('.'), from, to + 1))

View File

@ -176,29 +176,21 @@ end
color.hwb2rgb = function(h, w, b) color.hwb2rgb = function(h, w, b)
if w + b >= 1 then if w + b >= 1 then
local gray = w / (w + b) local grey = w / (w + b)
return gray, gray, gray return grey, grey, grey
end end
local r, g, b = color.hsl2rgb(h, 1, 0.5) local R, G, B = color.hsl2rgb(h, 1, 0.5)
r = r * (1 - w - b) + w local function f(c)
if r > 1 then c = c * (1 - w - b) + w
r = 1 if c > 1 then
elseif r < 0 then return 1
r = 0 elseif c <= 0 then
return 0
else
return c
end end
g = g * (1 - w - b) + w
if g > 1 then
g = 1
elseif g < 0 then
g = 0
end end
b = b * (1 - w - b) + w return f(R), f(G), f(B)
if b > 1 then
b = 1
elseif b < 0 then
b = 0
end
return r, g, b
end end
color.rgb2hwb = function(r, g, b) color.rgb2hwb = function(r, g, b)
@ -221,7 +213,7 @@ color.cmyk2hwb = function(c, m, y, k)
end end
color.hwb2hsl = function(h, w, b) color.hwb2hsl = function(h, w, b)
return color.rgb2hwb(color.hwb2rgb(h, w, b)) return color.rgb2hsl(color.hwb2rgb(h, w, b))
end end
color.hwb2hsv = function(h, w, b) color.hwb2hsv = function(h, w, b)