-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolored_text.lua
More file actions
150 lines (47 loc) · 1.91 KB
/
Copy pathcolored_text.lua
File metadata and controls
150 lines (47 loc) · 1.91 KB
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
148
149
150
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local table = _tl_compat and _tl_compat.table or table
require('utf8')
local u8 = require("utf8")
local inspect = require("inspect")
local function makeDescentColorText(
textobj,
textstr,
fromcolor,
tocolor,
...)
assert(textobj, "textobj should not be nil")
assert(type(textstr) == "string", "textstr should be a string, not " .. type(textstr))
assert(type(fromcolor) == "table", "fromcolor should be a table, not " .. type(fromcolor))
assert(type(tocolor) == "table", "tocolor should be a table, not " .. type(tocolor))
assert(#fromcolor == 4, "fromcolor should have 4 components")
assert(#tocolor == 4, "tocolor should have 4 components")
print("textobj", textobj)
print("textstr", textstr)
print("fromcolor", inspect(fromcolor))
print("tocolor", inspect(tocolor))
local slen = u8.len(textstr)
print("slen", slen)
local r, g, b, a = fromcolor[1], fromcolor[2], fromcolor[3], fromcolor[4]
local d_r = (tocolor[1] - fromcolor[1]) / slen
local d_g = (tocolor[2] - fromcolor[2]) / slen
local d_b = (tocolor[3] - fromcolor[3]) / slen
local d_a = (tocolor[4] - fromcolor[4]) / slen
print("d_r", d_r)
print("d_g", d_g)
print("d_b", d_b)
print("d_a", d_a)
local coloredtext = {}
for p, c in u8.codes(textstr) do
local char = u8.char(c)
print("p, c, char", p, c, u8.char(c))
table.insert(coloredtext, { r, g, b, a })
table.insert(coloredtext, char)
r = r + d_r
g = g + d_g
b = b + d_b
a = a + d_a
end
return textobj:add(coloredtext, ...)
end
return {
makeDescentColorText = makeDescentColorText,
}