dev (formatter): formatted with StyLua utils/

dev-doc
Pocco81 3 years ago
parent 685a90ad9a
commit 69ef80d89d

@ -1,7 +1,7 @@
local M = {} local M = {}
function M.available_commands() function M.available_commands()
return vim.tbl_keys(require("catppuccino.core.list_cs")) return vim.tbl_keys(require("catppuccino.core.list_cs"))
end end
return M return M

@ -1,28 +1,28 @@
local M = {} local M = {}
local function color_is_bright(r, g, b) local function color_is_bright(r, g, b)
-- Counting the perceptive luminance - human eye favors green color -- Counting the perceptive luminance - human eye favors green color
local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255 local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
if luminance > 0.5 then if luminance > 0.5 then
return true -- Bright colors, black font return true -- Bright colors, black font
else else
return false -- Dark colors, white font return false -- Dark colors, white font
end end
end end
function M.hex2rgb(hex) function M.hex2rgb(hex)
return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) return tonumber("0x" .. hex:sub(1, 2)), tonumber("0x" .. hex:sub(3, 4)), tonumber("0x" .. hex:sub(5, 6))
end end
function M.assert_brightness(color) function M.assert_brightness(color)
local hex = color:gsub("#", "") local hex = color:gsub("#", "")
local r = M.hex2rgb(string.sub(hex, 1, 2)) local r = M.hex2rgb(string.sub(hex, 1, 2))
local g = M.hex2rgb(string.sub(hex, 3, 4)) local g = M.hex2rgb(string.sub(hex, 3, 4))
local b = M.hex2rgb(string.sub(hex, 5, 6)) local b = M.hex2rgb(string.sub(hex, 5, 6))
if (color_is_bright(tonumber(r), tonumber(g), tonumber(b)) == true) then if color_is_bright(tonumber(r), tonumber(g), tonumber(b)) == true then
return true -- bright return true -- bright
end end
return false -- dull return false -- dull
end end

@ -24,310 +24,310 @@ local hsluv = {}
local hexChars = "0123456789abcdef" local hexChars = "0123456789abcdef"
local distance_line_from_origin = function(line) local distance_line_from_origin = function(line)
return math.abs(line.intercept) / math.sqrt((line.slope ^ 2) + 1) return math.abs(line.intercept) / math.sqrt((line.slope ^ 2) + 1)
end end
local length_of_ray_until_intersect = function(theta, line) local length_of_ray_until_intersect = function(theta, line)
return line.intercept / (math.sin(theta) - line.slope * math.cos(theta)) return line.intercept / (math.sin(theta) - line.slope * math.cos(theta))
end end
hsluv.get_bounds = function(l) hsluv.get_bounds = function(l)
local result = {} local result = {}
local sub2 local sub2
local sub1 = ((l + 16) ^ 3) / 1560896 local sub1 = ((l + 16) ^ 3) / 1560896
if sub1 > hsluv.epsilon then if sub1 > hsluv.epsilon then
sub2 = sub1 sub2 = sub1
else else
sub2 = l / hsluv.kappa sub2 = l / hsluv.kappa
end end
for i = 1, 3 do for i = 1, 3 do
local m1 = hsluv.m[i][1] local m1 = hsluv.m[i][1]
local m2 = hsluv.m[i][2] local m2 = hsluv.m[i][2]
local m3 = hsluv.m[i][3] local m3 = hsluv.m[i][3]
for t = 0, 1 do for t = 0, 1 do
local top1 = (284517 * m1 - 94839 * m3) * sub2 local top1 = (284517 * m1 - 94839 * m3) * sub2
local top2 = (838422 * m3 + 769860 * m2 + 731718 * m1) * l * sub2 - 769860 * t * l local top2 = (838422 * m3 + 769860 * m2 + 731718 * m1) * l * sub2 - 769860 * t * l
local bottom = (632260 * m3 - 126452 * m2) * sub2 + 126452 * t local bottom = (632260 * m3 - 126452 * m2) * sub2 + 126452 * t
table.insert(result, {slope = top1 / bottom, intercept = top2 / bottom}) table.insert(result, { slope = top1 / bottom, intercept = top2 / bottom })
end end
end end
return result return result
end end
hsluv.max_safe_chroma_for_l = function(l) hsluv.max_safe_chroma_for_l = function(l)
local bounds = hsluv.get_bounds(l) local bounds = hsluv.get_bounds(l)
local min = 1.7976931348623157e+308 local min = 1.7976931348623157e+308
for i = 1, 6 do for i = 1, 6 do
local length = distance_line_from_origin(bounds[i]) local length = distance_line_from_origin(bounds[i])
if length >= 0 then if length >= 0 then
min = math.min(min, length) min = math.min(min, length)
end end
end end
return min return min
end end
hsluv.max_safe_chroma_for_lh = function(l, h) hsluv.max_safe_chroma_for_lh = function(l, h)
local hrad = h / 360 * math.pi * 2 local hrad = h / 360 * math.pi * 2
local bounds = hsluv.get_bounds(l) local bounds = hsluv.get_bounds(l)
local min = 1.7976931348623157e+308 local min = 1.7976931348623157e+308
for i = 1, 6 do for i = 1, 6 do
local bound = bounds[i] local bound = bounds[i]
local length = length_of_ray_until_intersect(hrad, bound) local length = length_of_ray_until_intersect(hrad, bound)
if length >= 0 then if length >= 0 then
min = math.min(min, length) min = math.min(min, length)
end end
end end
return min return min
end end
hsluv.dot_product = function(a, b) hsluv.dot_product = function(a, b)
local sum = 0 local sum = 0
for i = 1, 3 do for i = 1, 3 do
sum = sum + a[i] * b[i] sum = sum + a[i] * b[i]
end end
return sum return sum
end end
hsluv.from_linear = function(c) hsluv.from_linear = function(c)
if c <= 0.0031308 then if c <= 0.0031308 then
return 12.92 * c return 12.92 * c
else else
return 1.055 * (c ^ 0.416666666666666685) - 0.055 return 1.055 * (c ^ 0.416666666666666685) - 0.055
end end
end end
hsluv.to_linear = function(c) hsluv.to_linear = function(c)
if c > 0.04045 then if c > 0.04045 then
return ((c + 0.055) / 1.055) ^ 2.4 return ((c + 0.055) / 1.055) ^ 2.4
else else
return c / 12.92 return c / 12.92
end end
end end
hsluv.xyz_to_rgb = function(tuple) hsluv.xyz_to_rgb = function(tuple)
return { return {
hsluv.from_linear(hsluv.dot_product(hsluv.m[1], tuple)), hsluv.from_linear(hsluv.dot_product(hsluv.m[1], tuple)),
hsluv.from_linear(hsluv.dot_product(hsluv.m[2], tuple)), hsluv.from_linear(hsluv.dot_product(hsluv.m[2], tuple)),
hsluv.from_linear(hsluv.dot_product(hsluv.m[3], tuple)) hsluv.from_linear(hsluv.dot_product(hsluv.m[3], tuple)),
} }
end end
hsluv.rgb_to_xyz = function(tuple) hsluv.rgb_to_xyz = function(tuple)
local rgbl = {hsluv.to_linear(tuple[1]), hsluv.to_linear(tuple[2]), hsluv.to_linear(tuple[3])} local rgbl = { hsluv.to_linear(tuple[1]), hsluv.to_linear(tuple[2]), hsluv.to_linear(tuple[3]) }
return { return {
hsluv.dot_product(hsluv.minv[1], rgbl), hsluv.dot_product(hsluv.minv[1], rgbl),
hsluv.dot_product(hsluv.minv[2], rgbl), hsluv.dot_product(hsluv.minv[2], rgbl),
hsluv.dot_product(hsluv.minv[3], rgbl) hsluv.dot_product(hsluv.minv[3], rgbl),
} }
end end
hsluv.y_to_l = function(Y) hsluv.y_to_l = function(Y)
if Y <= hsluv.epsilon then if Y <= hsluv.epsilon then
return Y / hsluv.refY * hsluv.kappa return Y / hsluv.refY * hsluv.kappa
else else
return 116 * ((Y / hsluv.refY) ^ 0.333333333333333315) - 16 return 116 * ((Y / hsluv.refY) ^ 0.333333333333333315) - 16
end end
end end
hsluv.l_to_y = function(L) hsluv.l_to_y = function(L)
if L <= 8 then if L <= 8 then
return hsluv.refY * L / hsluv.kappa return hsluv.refY * L / hsluv.kappa
else else
return hsluv.refY * (((L + 16) / 116) ^ 3) return hsluv.refY * (((L + 16) / 116) ^ 3)
end end
end end
hsluv.xyz_to_luv = function(tuple) hsluv.xyz_to_luv = function(tuple)
local X = tuple[1] local X = tuple[1]
local Y = tuple[2] local Y = tuple[2]
local divider = X + 15 * Y + 3 * tuple[3] local divider = X + 15 * Y + 3 * tuple[3]
local varU = 4 * X local varU = 4 * X
local varV = 9 * Y local varV = 9 * Y
if divider ~= 0 then if divider ~= 0 then
varU = varU / divider varU = varU / divider
varV = varV / divider varV = varV / divider
else else
varU = 0 varU = 0
varV = 0 varV = 0
end end
local L = hsluv.y_to_l(Y) local L = hsluv.y_to_l(Y)
if L == 0 then if L == 0 then
return {0, 0, 0} return { 0, 0, 0 }
end end
return {L, 13 * L * (varU - hsluv.refU), 13 * L * (varV - hsluv.refV)} return { L, 13 * L * (varU - hsluv.refU), 13 * L * (varV - hsluv.refV) }
end end
hsluv.luv_to_xyz = function(tuple) hsluv.luv_to_xyz = function(tuple)
local L = tuple[1] local L = tuple[1]
local U = tuple[2] local U = tuple[2]
local V = tuple[3] local V = tuple[3]
if L == 0 then if L == 0 then
return {0, 0, 0} return { 0, 0, 0 }
end end
local varU = U / (13 * L) + hsluv.refU local varU = U / (13 * L) + hsluv.refU
local varV = V / (13 * L) + hsluv.refV local varV = V / (13 * L) + hsluv.refV
local Y = hsluv.l_to_y(L) local Y = hsluv.l_to_y(L)
local X = 0 - (9 * Y * varU) / (((varU - 4) * varV) - varU * varV) local X = 0 - (9 * Y * varU) / (((varU - 4) * varV) - varU * varV)
return {X, Y, (9 * Y - 15 * varV * Y - varV * X) / (3 * varV)} return { X, Y, (9 * Y - 15 * varV * Y - varV * X) / (3 * varV) }
end end
hsluv.luv_to_lch = function(tuple) hsluv.luv_to_lch = function(tuple)
local L = tuple[1] local L = tuple[1]
local U = tuple[2] local U = tuple[2]
local V = tuple[3] local V = tuple[3]
local C = math.sqrt(U * U + V * V) local C = math.sqrt(U * U + V * V)
local H local H
if C < 0.00000001 then if C < 0.00000001 then
H = 0 H = 0
else else
H = math.atan2(V, U) * 180.0 / 3.1415926535897932 H = math.atan2(V, U) * 180.0 / 3.1415926535897932
if H < 0 then if H < 0 then
H = 360 + H H = 360 + H
end end
end end
return {L, C, H} return { L, C, H }
end end
hsluv.lch_to_luv = function(tuple) hsluv.lch_to_luv = function(tuple)
local L = tuple[1] local L = tuple[1]
local C = tuple[2] local C = tuple[2]
local Hrad = tuple[3] / 360.0 * 2 * math.pi local Hrad = tuple[3] / 360.0 * 2 * math.pi
return {L, math.cos(Hrad) * C, math.sin(Hrad) * C} return { L, math.cos(Hrad) * C, math.sin(Hrad) * C }
end end
hsluv.hsluv_to_lch = function(tuple) hsluv.hsluv_to_lch = function(tuple)
local H = tuple[1] local H = tuple[1]
local S = tuple[2] local S = tuple[2]
local L = tuple[3] local L = tuple[3]
if L > 99.9999999 then if L > 99.9999999 then
return {100, 0, H} return { 100, 0, H }
end end
if L < 0.00000001 then if L < 0.00000001 then
return {0, 0, H} return { 0, 0, H }
end end
return {L, hsluv.max_safe_chroma_for_lh(L, H) / 100 * S, H} return { L, hsluv.max_safe_chroma_for_lh(L, H) / 100 * S, H }
end end
hsluv.lch_to_hsluv = function(tuple) hsluv.lch_to_hsluv = function(tuple)
local L = tuple[1] local L = tuple[1]
local C = tuple[2] local C = tuple[2]
local H = tuple[3] local H = tuple[3]
local max_chroma = hsluv.max_safe_chroma_for_lh(L, H) local max_chroma = hsluv.max_safe_chroma_for_lh(L, H)
if L > 99.9999999 then if L > 99.9999999 then
return {H, 0, 100} return { H, 0, 100 }
end end
if L < 0.00000001 then if L < 0.00000001 then
return {H, 0, 0} return { H, 0, 0 }
end end
return {H, C / max_chroma * 100, L} return { H, C / max_chroma * 100, L }
end end
hsluv.hpluv_to_lch = function(tuple) hsluv.hpluv_to_lch = function(tuple)
local H = tuple[1] local H = tuple[1]
local S = tuple[2] local S = tuple[2]
local L = tuple[3] local L = tuple[3]
if L > 99.9999999 then if L > 99.9999999 then
return {100, 0, H} return { 100, 0, H }
end end
if L < 0.00000001 then if L < 0.00000001 then
return {0, 0, H} return { 0, 0, H }
end end
return {L, hsluv.max_safe_chroma_for_l(L) / 100 * S, H} return { L, hsluv.max_safe_chroma_for_l(L) / 100 * S, H }
end end
hsluv.lch_to_hpluv = function(tuple) hsluv.lch_to_hpluv = function(tuple)
local L = tuple[1] local L = tuple[1]
local C = tuple[2] local C = tuple[2]
local H = tuple[3] local H = tuple[3]
if L > 99.9999999 then if L > 99.9999999 then
return {H, 0, 100} return { H, 0, 100 }
end end
if L < 0.00000001 then if L < 0.00000001 then
return {H, 0, 0} return { H, 0, 0 }
end end
return {H, C / hsluv.max_safe_chroma_for_l(L) * 100, L} return { H, C / hsluv.max_safe_chroma_for_l(L) * 100, L }
end end
hsluv.rgb_to_hex = function(tuple) hsluv.rgb_to_hex = function(tuple)
local h = "#" local h = "#"
for i = 1, 3 do for i = 1, 3 do
local c = math.floor(tuple[i] * 255 + 0.5) local c = math.floor(tuple[i] * 255 + 0.5)
local digit2 = math.fmod(c, 16) local digit2 = math.fmod(c, 16)
local x = (c - digit2) / 16 local x = (c - digit2) / 16
local digit1 = math.floor(x) local digit1 = math.floor(x)
h = h .. string.sub(hexChars, digit1 + 1, digit1 + 1) h = h .. string.sub(hexChars, digit1 + 1, digit1 + 1)
h = h .. string.sub(hexChars, digit2 + 1, digit2 + 1) h = h .. string.sub(hexChars, digit2 + 1, digit2 + 1)
end end
return h return h
end end
hsluv.hex_to_rgb = function(hex) hsluv.hex_to_rgb = function(hex)
hex = string.lower(hex) hex = string.lower(hex)
local ret = {} local ret = {}
for i = 0, 2 do for i = 0, 2 do
local char1 = string.sub(hex, i * 2 + 2, i * 2 + 2) local char1 = string.sub(hex, i * 2 + 2, i * 2 + 2)
local char2 = string.sub(hex, i * 2 + 3, i * 2 + 3) local char2 = string.sub(hex, i * 2 + 3, i * 2 + 3)
local digit1 = string.find(hexChars, char1) - 1 local digit1 = string.find(hexChars, char1) - 1
local digit2 = string.find(hexChars, char2) - 1 local digit2 = string.find(hexChars, char2) - 1
ret[i + 1] = (digit1 * 16 + digit2) / 255.0 ret[i + 1] = (digit1 * 16 + digit2) / 255.0
end end
return ret return ret
end end
hsluv.lch_to_rgb = function(tuple) hsluv.lch_to_rgb = function(tuple)
return hsluv.xyz_to_rgb(hsluv.luv_to_xyz(hsluv.lch_to_luv(tuple))) return hsluv.xyz_to_rgb(hsluv.luv_to_xyz(hsluv.lch_to_luv(tuple)))
end end
hsluv.rgb_to_lch = function(tuple) hsluv.rgb_to_lch = function(tuple)
return hsluv.luv_to_lch(hsluv.xyz_to_luv(hsluv.rgb_to_xyz(tuple))) return hsluv.luv_to_lch(hsluv.xyz_to_luv(hsluv.rgb_to_xyz(tuple)))
end end
hsluv.hsluv_to_rgb = function(tuple) hsluv.hsluv_to_rgb = function(tuple)
return hsluv.lch_to_rgb(hsluv.hsluv_to_lch(tuple)) return hsluv.lch_to_rgb(hsluv.hsluv_to_lch(tuple))
end end
hsluv.rgb_to_hsluv = function(tuple) hsluv.rgb_to_hsluv = function(tuple)
return hsluv.lch_to_hsluv(hsluv.rgb_to_lch(tuple)) return hsluv.lch_to_hsluv(hsluv.rgb_to_lch(tuple))
end end
hsluv.hpluv_to_rgb = function(tuple) hsluv.hpluv_to_rgb = function(tuple)
return hsluv.lch_to_rgb(hsluv.hpluv_to_lch(tuple)) return hsluv.lch_to_rgb(hsluv.hpluv_to_lch(tuple))
end end
hsluv.rgb_to_hpluv = function(tuple) hsluv.rgb_to_hpluv = function(tuple)
return hsluv.lch_to_hpluv(hsluv.rgb_to_lch(tuple)) return hsluv.lch_to_hpluv(hsluv.rgb_to_lch(tuple))
end end
hsluv.hsluv_to_hex = function(tuple) hsluv.hsluv_to_hex = function(tuple)
return hsluv.rgb_to_hex(hsluv.hsluv_to_rgb(tuple)) return hsluv.rgb_to_hex(hsluv.hsluv_to_rgb(tuple))
end end
hsluv.hpluv_to_hex = function(tuple) hsluv.hpluv_to_hex = function(tuple)
return hsluv.rgb_to_hex(hsluv.hpluv_to_rgb(tuple)) return hsluv.rgb_to_hex(hsluv.hpluv_to_rgb(tuple))
end end
hsluv.hex_to_hsluv = function(s) hsluv.hex_to_hsluv = function(s)
return hsluv.rgb_to_hsluv(hsluv.hex_to_rgb(s)) return hsluv.rgb_to_hsluv(hsluv.hex_to_rgb(s))
end end
hsluv.hex_to_hpluv = function(s) hsluv.hex_to_hpluv = function(s)
return hsluv.rgb_to_hpluv(hsluv.hex_to_rgb(s)) return hsluv.rgb_to_hpluv(hsluv.hex_to_rgb(s))
end end
hsluv.m = { hsluv.m = {
{3.240969941904521, -1.537383177570093, -0.498610760293}, { 3.240969941904521, -1.537383177570093, -0.498610760293 },
{-0.96924363628087, 1.87596750150772, 0.041555057407175}, { -0.96924363628087, 1.87596750150772, 0.041555057407175 },
{0.055630079696993, -0.20397695888897, 1.056971514242878} { 0.055630079696993, -0.20397695888897, 1.056971514242878 },
} }
hsluv.minv = { hsluv.minv = {
{0.41239079926595, 0.35758433938387, 0.18048078840183}, { 0.41239079926595, 0.35758433938387, 0.18048078840183 },
{0.21263900587151, 0.71516867876775, 0.072192315360733}, { 0.21263900587151, 0.71516867876775, 0.072192315360733 },
{0.019330818715591, 0.11919477979462, 0.95053215224966} { 0.019330818715591, 0.11919477979462, 0.95053215224966 },
} }
hsluv.refY = 1.0 hsluv.refY = 1.0
hsluv.refU = 0.19783000664283 hsluv.refU = 0.19783000664283

@ -11,150 +11,150 @@ util.day_brightness = 0.3
---@param hex_str string hexadecimal value of a color ---@param hex_str string hexadecimal value of a color
local hex_to_rgb = function(hex_str) local hex_to_rgb = function(hex_str)
local hex = "[abcdef0-9][abcdef0-9]" local hex = "[abcdef0-9][abcdef0-9]"
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$" local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
hex_str = string.lower(hex_str) hex_str = string.lower(hex_str)
assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str)) assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str))
local red, green, blue = string.match(hex_str, pat) local red, green, blue = string.match(hex_str, pat)
return {tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16)} return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) }
end end
---@param fg string foreground color ---@param fg string foreground color
---@param bg string background color ---@param bg string background color
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg ---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
function util.blend(fg, bg, alpha) function util.blend(fg, bg, alpha)
bg = hex_to_rgb(bg) bg = hex_to_rgb(bg)
fg = hex_to_rgb(fg) fg = hex_to_rgb(fg)
local blendChannel = function(i) local blendChannel = function(i)
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
return math.floor(math.min(math.max(0, ret), 255) + 0.5) return math.floor(math.min(math.max(0, ret), 255) + 0.5)
end end
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3)) return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
end end
function util.darken(hex, amount, bg) function util.darken(hex, amount, bg)
return util.blend(hex, bg or util.bg, math.abs(amount)) return util.blend(hex, bg or util.bg, math.abs(amount))
end end
function util.lighten(hex, amount, fg) function util.lighten(hex, amount, fg)
return util.blend(hex, fg or util.fg, math.abs(amount)) return util.blend(hex, fg or util.fg, math.abs(amount))
end end
function util.brighten(color, percentage) function util.brighten(color, percentage)
local hsl = hsluv.hex_to_hsluv(color) local hsl = hsluv.hex_to_hsluv(color)
local larpSpace = 100 - hsl[3] local larpSpace = 100 - hsl[3]
if percentage < 0 then if percentage < 0 then
larpSpace = hsl[3] larpSpace = hsl[3]
end end
hsl[3] = hsl[3] + larpSpace * percentage hsl[3] = hsl[3] + larpSpace * percentage
return hsluv.hsluv_to_hex(hsl) return hsluv.hsluv_to_hex(hsl)
end end
function util.invertColor(color) function util.invertColor(color)
if color ~= "NONE" then if color ~= "NONE" then
local hsl = hsluv.hex_to_hsluv(color) local hsl = hsluv.hex_to_hsluv(color)
hsl[3] = 100 - hsl[3] hsl[3] = 100 - hsl[3]
if hsl[3] < 40 then if hsl[3] < 40 then
hsl[3] = hsl[3] + (100 - hsl[3]) * util.day_brightness hsl[3] = hsl[3] + (100 - hsl[3]) * util.day_brightness
end end
return hsluv.hsluv_to_hex(hsl) return hsluv.hsluv_to_hex(hsl)
end end
return color return color
end end
function util.string_to_color(colors, value, default) function util.string_to_color(colors, value, default)
if not value or value == "" then if not value or value == "" then
return default return default
end end
-- If the value is a hex color code then return it -- If the value is a hex color code then return it
local hex = "[abcdef0-9][abcdef0-9]" local hex = "[abcdef0-9][abcdef0-9]"
local pat = "^#" .. hex .. hex .. hex .. "$" local pat = "^#" .. hex .. hex .. hex .. "$"
if string.match(value, pat) then if string.match(value, pat) then
return value return value
end end
local acceptable_colors = {"black", "red", "green", "blue", "magenta", "cyan", "white", "orange", "pink"} local acceptable_colors = { "black", "red", "green", "blue", "magenta", "cyan", "white", "orange", "pink" }
for _, ac in ipairs(acceptable_colors) do for _, ac in ipairs(acceptable_colors) do
if string.match(value, ac) then if string.match(value, ac) then
return colors[value] return colors[value]
end end
end end
-- Did not match anything to return default -- Did not match anything to return default
return default return default
end end
function util.highlight(group, color) function util.highlight(group, color)
local style = color.style and "gui=" .. color.style or "gui=NONE" local style = color.style and "gui=" .. color.style or "gui=NONE"
local fg = color.fg and "guifg=" .. color.fg or "guifg=NONE" local fg = color.fg and "guifg=" .. color.fg or "guifg=NONE"
local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE" local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE"
local sp = color.sp and "guisp=" .. color.sp or "" local sp = color.sp and "guisp=" .. color.sp or ""
local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp
vim.cmd(hl) vim.cmd(hl)
if color.link then if color.link then
vim.cmd("highlight! link " .. group .. " " .. color.link) vim.cmd("highlight! link " .. group .. " " .. color.link)
end end
end end
function util.syntax(tbl) function util.syntax(tbl)
for group, colors in pairs(tbl) do for group, colors in pairs(tbl) do
if (type(group) == "number") then if type(group) == "number" then
for inner_group, clrs in pairs(colors) do for inner_group, clrs in pairs(colors) do
util.highlight(inner_group, clrs) util.highlight(inner_group, clrs)
end end
else else
util.highlight(group, colors) util.highlight(group, colors)
end end
end end
end end
function util.properties(tbl) function util.properties(tbl)
for property, value in pairs(tbl) do for property, value in pairs(tbl) do
vim.o[property] = value vim.o[property] = value
end end
end end
function util.terminal(theme) function util.terminal(theme)
g.terminal_color_0 = theme.colors.black g.terminal_color_0 = theme.colors.black
g.terminal_color_1 = theme.colors.red g.terminal_color_1 = theme.colors.red
g.terminal_color_2 = theme.colors.green g.terminal_color_2 = theme.colors.green
g.terminal_color_3 = theme.colors.yellow g.terminal_color_3 = theme.colors.yellow
g.terminal_color_4 = theme.colors.blue g.terminal_color_4 = theme.colors.blue
g.terminal_color_5 = theme.colors.magenta g.terminal_color_5 = theme.colors.magenta
g.terminal_color_6 = theme.colors.cyan g.terminal_color_6 = theme.colors.cyan
g.terminal_color_7 = theme.colors.white g.terminal_color_7 = theme.colors.white
g.terminal_color_8 = theme.colors.black_br g.terminal_color_8 = theme.colors.black_br
g.terminal_color_9 = theme.colors.red_br g.terminal_color_9 = theme.colors.red_br
g.terminal_color_10 = theme.colors.green_br g.terminal_color_10 = theme.colors.green_br
g.terminal_color_11 = theme.colors.yellow_br g.terminal_color_11 = theme.colors.yellow_br
g.terminal_color_12 = theme.colors.blue_br g.terminal_color_12 = theme.colors.blue_br
g.terminal_color_13 = theme.colors.magenta_br g.terminal_color_13 = theme.colors.magenta_br
g.terminal_color_14 = theme.colors.cyan_br g.terminal_color_14 = theme.colors.cyan_br
g.terminal_color_15 = theme.colors.white_br g.terminal_color_15 = theme.colors.white_br
end end
function util.load(theme) function util.load(theme)
vim.cmd("hi clear") vim.cmd("hi clear")
if vim.fn.exists("syntax_on") then if vim.fn.exists("syntax_on") then
vim.cmd("syntax reset") vim.cmd("syntax reset")
end end
g.colors_name = "catppuccino" g.colors_name = "catppuccino"
util.properties(theme.properties) util.properties(theme.properties)
util.syntax(theme.base) util.syntax(theme.base)
util.syntax(theme.integrations) util.syntax(theme.integrations)
-- if opts.ui.terminal then -- if opts.ui.terminal then
-- util.terminal(theme) -- util.terminal(theme)
-- end -- end
end end
return util return util

Loading…
Cancel
Save