-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolsw.lua
220 lines (203 loc) · 6.94 KB
/
colsw.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
local filepath = ""
---@param fileHandler ReadHandle
local function applyPaletteFromFile(fileHandler)
local cols = {}
for i = 1, 16 do
local line = fileHandler.readLine()
if not line then
error("File could not be read completely!")
end
local occurrences = {}
for str in line:gmatch("%d+") do
if not tonumber(str) then
error(("Can't put %s as a number"):format(str))
end
occurrences[#occurrences+1] = tonumber(str)
end
if #occurrences > 3 then
error("More colors than should be possible!")
end
cols[#cols+1] = {unpack(occurrences)}
end
for i = 1, 16 do
term.setPaletteColor(2^(i-1), cols[i][1]/255, cols[i][2]/255, cols[i][3]/255)
end
end
do
local applyOnly = false
if #arg > 2 then
error("Too many arguments passed!")
end
for i, argument in ipairs(arg) do
if argument == "--help" then
print("Color Switch by Simadude")
print("Version 1.0.0")
print("A simple program that allows you to edit and save your palettes!\n")
print("colsw apply [PATH]; applies the palette from the file and quits.\n")
print("colsw [PATH]; opens existing palette file (or creates a new one)\n")
print("colsw --help; prints out this helpful message\n")
print("Repo - https://github.com/simadude/colorswitch")
return
elseif argument == "apply" and #arg == 2 and i == 1 then
applyOnly = true
elseif #arg == 1 and i == 1 then
filepath = fs.combine(shell.dir(), argument)
if fs.isDir(filepath) then
error(("Path %s is not a file"):format(filepath))
elseif fs.isReadOnly(filepath) then
error(("Path %s is ReadOnly"):format(filepath))
end
elseif #arg == 2 and i == 2 and applyOnly then
filepath = fs.combine(shell.dir(), argument)
if fs.isDir(filepath) then
error(("Path %s is not a file"):format(filepath))
elseif fs.isReadOnly(filepath) then
error(("Path %s is ReadOnly"):format(filepath))
end
if not fs.exists(filepath) then
error(("Path %s does not exist"):format(filepath))
end
else
error(("Encountered unforseen argument: %s"):format(argument))
end
end
if filepath ~= "" and fs.exists(filepath) then
local fh, e = fs.open(filepath, "r")
if not fh then error(e) end
---@cast fh ReadHandle
applyPaletteFromFile(fh);
end
if applyOnly then
return
end
end
-- index of a chosen color (1-16)
local chosenColor = 1
-- index of a chosen RGB value to change ( 1 - red, 2 - green, 3 - blue)
local chosenValue = 1
---@type string[]
local rgbstr = {};
local function saveFile()
if filepath == "" then return end
local fh, e = fs.open(filepath, "w")
if not fh then error(e) end
for i = 1, 16 do
local r, g, b = term.getPaletteColor(2^(i-1))
r, g, b = r*255, g*255, b*255
fh.writeLine(("%s, %s, %s"):format(r, g, b))
end
fh.flush()
fh.close()
end
local function changeRGB()
---@type number[]
local rgb = {term.getPaletteColor(2^(chosenColor-1))}
for i, value in ipairs(rgb) do
rgbstr[i] = tostring(value*255)
end
end
local function changePalette()
local r, g, b = tonumber(rgbstr[1]) --[[@as number]], tonumber(rgbstr[2])--[[@as number]], tonumber(rgbstr[3])--[[@as number]]
term.setPaletteColour(2^(chosenColor-1), r/255, g/255, b/255)
end
changeRGB()
local function colorToHex(col)
return ("%x"):format(col)
end
local function redraw()
term.clear()
term.setCursorBlink(false)
local width, height = term.getSize()
local colorNames = {
"white",
"orange",
"magenta",
"lightBlue",
"yellow",
"lime",
"pink",
"gray",
"lightGray",
"cyan",
"purple",
"blue",
"brown",
"green",
"red",
"black"
}
local longestNameLength = 1;
for _, colorName in ipairs(colorNames) do
longestNameLength = math.max(longestNameLength, #colorName)
end
for i = 1, 16 do
local colorName = colorNames[i]
term.setCursorPos(width - 8 - #colorName, i+1)
term.blit(colorName..": ", ("0"):rep(#colorName+2), ("f"):rep(#colorName+1)..colorToHex(i-1))
if chosenColor == i then
term.setCursorPos(width - 8 - longestNameLength - 1, i+1)
term.write((">"):rep(longestNameLength - #colorName + 1))
end
term.setCursorPos(width - 4 + ((i-1) % 4), height/2 + math.floor((i-1) / 4))
term.blit(" ", "0", colorToHex(i-1))
end
term.setCursorPos(3, 5)
term.write("red: "..rgbstr[1])
term.setCursorPos(3, 6)
term.write("green: "..rgbstr[2])
term.setCursorPos(3, 7)
term.write("blue: "..rgbstr[3])
term.setCursorBlink(true)
term.setCursorPos(10+#rgbstr[chosenValue], 4+chosenValue)
end
local holdingShift = false
local holdingCtrl = false
local function eventLoop()
while true do
local event = {os.pullEvent()}
if event[1] == "char" then
if tonumber(event[2]) then
local digit = event[2]
if tonumber(rgbstr[chosenValue]..digit) < 255 then
rgbstr[chosenValue] = tostring(tonumber(rgbstr[chosenValue]..digit))
else
rgbstr[chosenValue] = "255"
end
elseif event[2] == "s" then
changePalette()
saveFile()
elseif event[2] == "q" then
return
end
elseif event[1] == "key" then
if event[2] == keys.backspace then
rgbstr[chosenValue] = tostring(tonumber(rgbstr[chosenValue]:sub(1, #rgbstr[chosenValue]-1)))
if rgbstr[chosenValue] == "nil" then
rgbstr[chosenValue] = "0"
end
elseif event[2] == keys.down and not holdingShift then
chosenValue = math.min(chosenValue + 1, 3)
elseif event[2] == keys.up and not holdingShift then
chosenValue = math.max(chosenValue - 1, 1)
elseif event[2] == keys.down and holdingShift then
chosenColor = math.min(chosenColor + 1, 16)
changeRGB()
elseif event[2] == keys.up and holdingShift then
chosenColor = math.max(chosenColor - 1, 1)
changeRGB()
elseif event[2] == keys.leftShift then
holdingShift = true
end
elseif event[1] == "key_up" then
if event[2] == keys.leftShift then
holdingShift = false
end
end
redraw()
end
end
term.clear()
redraw()
eventLoop()
term.clear()
term.setCursorPos(1, 1)