From 21a63ccdef4ba2c754b738080cdf9b21d12da9cb Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 3 Jan 2024 20:30:16 +0300 Subject: [PATCH] bit32->bit and other stuff --- ccpi.lua | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/ccpi.lua b/ccpi.lua index aa480d3..af1c31c 100644 --- a/ccpi.lua +++ b/ccpi.lua @@ -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 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.scale = 0.5 + string.byte(fp:read(1)) * 5 / 255 for i = 1, 16 do - image.palette[i] = bit32.lshift(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] = bit32.bor(image.palette[i], string.byte(fp:read(1))) + image.palette[i] = bit.blshift(string.byte(fp:read(1)), 16) + image.palette[i] = bit.bor(image.palette[i], bit.blshift(string.byte(fp:read(1)), 8)) + image.palette[i] = bit.bor(image.palette[i], string.byte(fp:read(1))) end for y = 1, image.h do @@ -43,12 +24,11 @@ local function load(path) line.s = line.s .. fp:read(1) local char = fp:read(1) if char == nil then - printError("Fatal: failed to read color data for y=" .. y .. " x=" .. x) - break + return nil, string.format("Failed to read color data for x=%d y=%d", x, y) end local color = string.byte(char) - line.bg = line.bg .. string.format("%x", bit32.band(0xF, color)) - line.fg = line.fg .. string.format("%x", bit32.band(0xF, bit32.rshift(color, 4))) + line.bg = line.bg .. string.format("%x", bit.band(0xF, color)) + line.fg = line.fg .. string.format("%x", bit.band(0xF, bit.brshift(color, 4))) end table.insert(image.lines, line) end @@ -59,12 +39,13 @@ local function load(path) end local function draw(img, ox, oy, monitor) + -- todo: add expect() local t = monitor or term.current() ox = ox or 1 oy = oy or 1 for i = 1, 16 do - t.setPaletteColor(colors_list[i], img.palette[i]) + t.setPaletteColor(bit.blshift(1, i - 1), img.palette[i]) end t.setTextScale(img.scale)