bit32->bit and other stuff

This commit is contained in:
Casey 2024-01-03 20:30:16 +03:00
parent bd3b8b810c
commit 21a63ccdef
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 8 additions and 27 deletions

View File

@ -1,23 +1,4 @@
local colors_list = {
colors.white,
colors.orange,
colors.magenta,
colors.lightBlue,
colors.yellow,
colors.lime,
colors.pink,
colors.gray,
colors.lightGray,
colors.cyan,
colors.purple,
colors.blue,
colors.brown,
colors.green,
colors.red,
colors.black
}
local function load(path) local function load(path)
local image = { w = 0, h = 0, scale = 1.0, palette = {}, lines = {} } local image = { w = 0, h = 0, scale = 1.0, palette = {}, lines = {} }
@ -32,9 +13,9 @@ local function load(path)
image.w, image.h = string.byte(fp:read(1)), string.byte(fp:read(1)) image.w, image.h = string.byte(fp:read(1)), string.byte(fp:read(1))
image.scale = 0.5 + string.byte(fp:read(1)) * 5 / 255 image.scale = 0.5 + string.byte(fp:read(1)) * 5 / 255
for i = 1, 16 do for i = 1, 16 do
image.palette[i] = bit32.lshift(string.byte(fp:read(1)), 16) image.palette[i] = bit.blshift(string.byte(fp:read(1)), 16)
image.palette[i] = bit32.bor(image.palette[i], bit32.lshift(string.byte(fp:read(1)), 8)) image.palette[i] = bit.bor(image.palette[i], bit.blshift(string.byte(fp:read(1)), 8))
image.palette[i] = bit32.bor(image.palette[i], string.byte(fp:read(1))) image.palette[i] = bit.bor(image.palette[i], string.byte(fp:read(1)))
end end
for y = 1, image.h do for y = 1, image.h do
@ -43,12 +24,11 @@ local function load(path)
line.s = line.s .. fp:read(1) line.s = line.s .. fp:read(1)
local char = fp:read(1) local char = fp:read(1)
if char == nil then if char == nil then
printError("Fatal: failed to read color data for y=" .. y .. " x=" .. x) return nil, string.format("Failed to read color data for x=%d y=%d", x, y)
break
end end
local color = string.byte(char) local color = string.byte(char)
line.bg = line.bg .. string.format("%x", bit32.band(0xF, color)) line.bg = line.bg .. string.format("%x", bit.band(0xF, color))
line.fg = line.fg .. string.format("%x", bit32.band(0xF, bit32.rshift(color, 4))) line.fg = line.fg .. string.format("%x", bit.band(0xF, bit.brshift(color, 4)))
end end
table.insert(image.lines, line) table.insert(image.lines, line)
end end
@ -59,12 +39,13 @@ local function load(path)
end end
local function draw(img, ox, oy, monitor) local function draw(img, ox, oy, monitor)
-- todo: add expect()
local t = monitor or term.current() local t = monitor or term.current()
ox = ox or 1 ox = ox or 1
oy = oy or 1 oy = oy or 1
for i = 1, 16 do for i = 1, 16 do
t.setPaletteColor(colors_list[i], img.palette[i]) t.setPaletteColor(bit.blshift(1, i - 1), img.palette[i])
end end
t.setTextScale(img.scale) t.setTextScale(img.scale)