-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_color.lua
More file actions
162 lines (132 loc) · 3.43 KB
/
Copy pathgradient_color.lua
File metadata and controls
162 lines (132 loc) · 3.43 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
151
152
153
154
155
156
157
158
159
160
161
162
function GetFile()
print("Enter path to *.txt: (or drop text file in terminal)")
local filepath = io.read()
local file = io.open(filepath, 'r')
if file == nil then
while file == nil do
filepath = io.read()
if filepath == "stop" then
return nil
end
file = io.open(filepath, 'r')
end
end
return file
end
function ReadFile(file)
local content = "No file"
if file then
content = file:read("*all")
return content
end
return content
end
-- Lerp
local function Lerp(a, b, c)
return a + c * (b - a)
end
-- Color
function Color(r, g, b)
local color = {}
color.r = math.floor(r)
color.g = math.floor(g)
color.b = math.floor(b)
function color.lerp(o, a)
return Color(Lerp(color.r, o.r, a), Lerp(color.g, o.g, a), Lerp(color.b, o.b, a))
end
function color.hex()
local hex = (color.r * 0x10000) + (color.g * 0x100) + (color.b * 0x1)
return "#" .. string.format("%02X", hex)
end
function color.tostring()
return ("rgb(%d, %d, %d)"):format(color.r, color.g, color.b)
end
return color
end
function HexToColor(hex)
hex = hex:gsub("#", "")
local r, g, b = hex:match("(%w%w)(%w%w)(%w%w)")
local function convert(h)
return tonumber("0x"..h)
end
return Color(convert(r), convert(g), convert(b))
end
-- Regex
function DeleteTags(text)
text = text:gsub("%[center%]%s?", "")
text = text:gsub("%[/center%]%s?", "")
return text
end
function ColorMarks(text)
text = text:gsub("\n", "(COLOR_END)\n(COLOR_BEGIN)")
text = text:gsub("^", "(COLOR_BEGIN)")
text = text:gsub("%(COLOR_BEGIN%)%(COLOR_END%)", "")
return text
end
function GradientText(text, _begin, _finish)
text = ColorMarks(text)
text = text:gsub("%(COLOR_END%)", "[/color]")
local len = 0
text:gsub("%(COLOR_BEGIN%)", function()
len = len + 1
end)
local current_color = _begin
local alpha = 1 / len
local position = 0
text = text:gsub("%(COLOR_BEGIN%)", function()
local color = "[color="..current_color.hex().."]"
current_color = current_color.lerp(_finish, alpha)
position = position + 1
if position < len then
return color
else
return ""
end
end)
return text
end
local function ReadColor()
print("Red")
local r = io.read()
print("Green")
local g = io.read()
print("Blue")
local b = io.read()
return Color(r, g, b)
end
print("Gradient Color")
print("Made by TehnoDragon (7/12/2021)")
print()
-- Getting and Reading file
local text = ReadFile(GetFile())
-- Getting First and Second Colors
local FIRST_COLOR = Color(255, 255, 255)
local SECOND_COLOR = Color(0, 0, 0)
print("Select Input Color Type: (0 - RGB), (1 - HEX)")
local INPUT_COLOR_TYPE = tonumber(io.read())
if INPUT_COLOR_TYPE == 0 then
print("(RGB) Enter First Color:")
FIRST_COLOR = ReadColor()
print("(RGB) Enter Second Color:")
SECOND_COLOR = ReadColor()
elseif INPUT_COLOR_TYPE == 1 then
print("(HEX) Enter First Color:")
FIRST_COLOR = HexToColor(io.read())
print("(HEX) Enter Second Color:")
SECOND_COLOR = HexToColor(io.read())
else
print(INPUT_COLOR_TYPE .. ": Input Color Type unsupported")
os.execute("pause")
os.exit()
end
-- Removing Tags
print("Remove Tags? 0 - No")
if tonumber(io.read()) ~= 0 then
text = DeleteTags(text)
end
-- Generating Gradient
text = GradientText(text, FIRST_COLOR, SECOND_COLOR)
-- Creating new file
local file = io.open("gradient_text.txt", "w")
file:write(text)
file:close()