-
Notifications
You must be signed in to change notification settings - Fork 0
/
chpal.lua
74 lines (64 loc) · 2.49 KB
/
chpal.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
--[[
MIT License
Copyright (c) 2017 CrazedProgrammer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local args, pal = {...}, { }
local palmap = { 32768, 4096, 8192, 2, 2048, 1024, 512, 256,
128, 16384, 32, 16, 8, 4, 64, 1 }
if not args[1] then
print("chpal <path>\nchpal --export <path>\nchpal --reset")
return
elseif args[1] == "--export" then
local path = shell.resolve(args[2])
local handle = fs.open(path, "w")
if not handle then
printError("Failed to write to file \""..path.."\".")
return
end
for i = 1, 16 do
local palr, palg, palb = term.getPaletteColor(palmap[i])
handle.writeLine("*.color"..(i - 1)..": #"..
string.format("%02X%02X%02X", palr * 255, palg * 255, palb * 255))
end
handle.close()
return
elseif args[1] == "--reset" then
pal = { "#191919", "#7F664C", "#57A64E", "#F2B233", "#3366CC", "#B266E5", "#4C99B2", "#999999",
"#4C4C4C", "#CC4C4C", "#7FCC19", "#DEDE6C", "#99B2F2", "#E57FD8", "#F2B2CC", "#F0F0F0" }
else
local path = shell.resolve(args[1])
local handle = fs.open(path, "r")
if not handle then
printError("Palette file \""..path.."\" not found.")
return
end
local line = handle.readLine()
while line do
line = line:gsub("^%s*(.-)%s*$", "%1") -- trim
if line:find("%*%.color%d%d*: ") then
index = tonumber(line:sub(8, (line:sub(9, 9) == ":") and 8 or 9)) + 1
colour = line:sub(-7)
pal[index] = colour
end
line = handle.readLine()
end
handle.close()
end
for i = 1, 16 do
term.setPaletteColour(palmap[i], tonumber(pal[i]:sub(2), 16))
end