-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.lua
147 lines (133 loc) · 3.83 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
module = {}
---@param tbl table
---@param func function
---@return table
function module.enumerate(tbl, func) -- almost same as python's enumerate(map with index)
local t = {}
for idx, v in ipairs(tbl) do
t[idx] = func(v, idx)
end
return t
end
-- https://gist.github.com/w13b3/5d8a80fae57ab9d51e285f909e2862e0
---@diagnostic disable-next-line: unused-function, unused-local
function module.zip(...) -- almost same as python's zip
local idx, ret, args = 1, {}, { ... }
while true do -- loop smallest table-times
local sub_table = {}
local value
for _, table_ in ipairs(args) do
value = table_[idx] -- becomes nil if index is out of range
if value == nil then
break
end -- break for-loop
table.insert(sub_table, value)
end
if value == nil then
break
end -- break while-loop
table.insert(ret, sub_table) -- insert the sub result
idx = idx + 1
end
return ret
end
---@param tbl any
function module.dump(tbl)
module.dump_helper(tbl, 0, true)
end
module.dump_space = ' '
---@param tbl any
---@param depth number
---@param root boolean
function module.dump_helper(tbl, depth, root)
if root then
print('{')
end
for k, v in pairs(tbl) do
if type(k) == 'number' then
k = '[' .. k .. ']'
elseif type(k) == 'string' then
k = '\'' .. k .. '\''
end
if type(v) == 'table' then
print(module.dump_space:rep(depth + 1) .. k .. ': {')
module.dump_helper(v, depth + 1, false)
else
print(module.dump_space:rep(depth + 1) .. k .. ': ' .. v .. ',')
end
end
if root then
print('}')
else
print(module.dump_space:rep(depth) .. '},')
end
end
---@param tbls table
function module.flatten1(tbls) -- flatten 1 level
local result = {}
---@diagnostic disable-next-line: unused-local
for index, tbl in next, tbls do
for _, value in next, tbl do
table.insert(result, value)
end
end
return result
end
--- C:/Users/26523/Notes => c/u/2/Notes
--- /home/timxing/.config/wezterm => /h/t/./wezterm
---@param path string|nil
---@return string
function module.short_path(path)
if path == nil then
return ''
end
if path:sub(-1) == '/' then
path = path:sub(1, -2)
end
path = string.gsub(path, '([^/])[^/]-/', function(s)
-- return s:lower() .. '/'
return s .. '/'
end)
print(path)
return path
end
-- 用于计算颜色亮度的函数
---@param color table color@{u8,u8,u8}
---@return number
function module.calculate_brightness(color)
return 0.299 * color[1] + 0.587 * color[2] + 0.114 * color[3]
end
-- 通过字符串哈希生成颜色
---@param title string
---@return string
function module.generate_window_color(title)
local target_brightness = 127 -- 你可以调整目标亮度的阈值
print('--- utils.generate_window_color ---[[')
-- 计算字符串哈希值
local hash = 0
for i = 1, #title do
hash = (hash << 5) - hash + title:byte(i)
end
-- 将哈希值映射到RGB颜色空间
local r = (hash & 0xFF0000) >> 16
local g = (hash & 0x00FF00) >> 8
local b = hash & 0x0000FF
-- 计算亮度
local brightness = module.calculate_brightness({r, g, b})
print('hash: ' .. string.format("%x", hash))
print('rgb: ' .. string.format("%02x,%02x,%02x", r,g,b))
print('brightness: ' .. brightness)
-- 调整亮度,确保颜色相对亮
if brightness < target_brightness then
local brightness_factor = target_brightness / brightness
r = math.min(255, math.floor(r * brightness_factor))
g = math.min(255, math.floor(g * brightness_factor))
b = math.min(255, math.floor(b * brightness_factor))
print('target rgb: ' .. string.format("%02x,%02x,%02x", r,g,b))
end
local color = (r << 16) | (g << 8) | b
print('color: ' .. string.format("%06x", color))
print('--- utils.generate_window_color ---]] ')
return string.format('#%06x',color)
end
return module