-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.lua
175 lines (122 loc) · 3.29 KB
/
analysis.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- (C) PureAsbestos, 2020
-- Distributed under the MIT License
-- Attribution welcome, but not required
-- this script requires lua-gd
gd = require("gd")
--[ HELPER FUNCTIONS ]--
-- get the directory of this file
function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*[/\\])") or "./"
end
BASE_DIR = script_path().."scripts/"
-- redefine dofile to work relative to the base directory
old_dofile = dofile
function dofile (filename)
return old_dofile(BASE_DIR..filename)
end
-- unpack for lua < 5.2
function unpack_ (t, i)
i = i or 1
if t[i] ~= nil then
return t[i], unpack(t, i + 1)
end
end
unpack = unpack or table.unpack or unpack_
-- convert 24 bit hexidecimal to RGB triple
function hex2rgb(hx)
hx = hx:gsub("#","")
return tonumber("0x"..hx:sub(1,2)), tonumber("0x"..hx:sub(3,4)), tonumber("0x"..hx:sub(5,6))
end
-- split a string with the given separator (whitespace by default)
function split(str, sep)
sep = sep or "%s"
local t={}
for s in string.gmatch(str, "([^"..sep.."]+)") do
table.insert(t, s)
end
return t
end
--[------------------]--
function main()
--[ CMD LINE ARGS ]--
local palette = split(arg[1], ',')
local output_loc = arg[2] or "out.png"
local options = split(arg[3] or "0,10,70,255,128,48,1,0,0", ',')
for i, num in ipairs(options) do
options[i] = tonumber(num)
end
--[---------------]--
--[ HACKY STUFF ]--
-- create image
local image = gd.create(640, 432)
-- give palette to image
for _, hx in ipairs(palette) do
local r, g, b = hex2rgb(hx)
image:colorAllocate(r, g, b)
end
num_colors = image:colorsTotal()
-- initialize foreground and background colors
fg = image:colorClosest(255, 255, 255)
bg = image:colorClosest(0, 0, 0)
-- fill in full size of palette
while image:colorsTotal() < 256 do
image:colorAllocate(image:red(bg), image:green(bg), image:blue(bg))
end
--[-------------]--
--[ API FUNCS ]--
function matchcolor(r, g, b)
return image:colorClosest(r, g, b)
end
matchcolor2 = matchcolor
function getforecolor()
return fg
end
function getbackcolor()
return bg
end
function setforecolor(c)
fg = c
end
function setbackcolor(c)
bg = c
end
function clearpicture(c)
image:filledRectangle(0, 0, image:sizeX() - 1, image:sizeY() - 1, c)
end
function drawfilledrect(x1, y1, x2, y2, c)
image:filledRectangle(x1, y1, x2, y2, c)
end
function putpicturepixel(x, y, c)
image:setPixel(x, y, c)
end
function getpicturepixel(x, y)
return image:getPixel(x, y)
end
function getcolor(c)
return image:red(c), image:green(c), image:blue(c)
end
function inputbox(...)
return true, unpack(options)
end
-- functions that should do nothing
function updatescreen() end
function waitbreak() end
function setpicturesize(x, y) end -- the image size is hardcoded
function messagebox() end
--[-----------]--
-- run the analysis
dofile("pic_db_AnalyzePalette18.lua")
-- deallocate unused colors
for i=num_colors,255 do
image:colorDeallocate(i)
end
-- write out the image file
image:pngEx(output_loc, 6)
end -- end of main
function handler(err)
print(err)
print("Usage: lua analysis.lua hex_color_1,hex_color_2,... [filename] [NOBG,CBW1,CBW2,dia_sat1,dia_sat2,dia_sat3,sat_mod1,sat_mod2,sat_mod3]")
os.exit()
end
xpcall(main, handler)