1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-16 21:54:49 +00:00
opus/sys/apis/nft.lua

81 lines
1.8 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
local Util = require('util')
local NFT = { }
-- largely copied from http://www.computercraft.info/forums2/index.php?/topic/5029-145-npaintpro/
local tColourLookup = { }
for n = 1, 16 do
2018-01-24 22:39:38 +00:00
tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
2016-12-11 19:24:52 +00:00
end
local function getColourOf(hex)
2018-01-24 22:39:38 +00:00
return tColourLookup[hex:byte()]
2016-12-11 19:24:52 +00:00
end
function NFT.parse(imageText)
2018-01-24 22:39:38 +00:00
local image = {
fg = { },
bg = { },
text = { },
}
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local num = 1
local lines = Util.split(imageText)
while #lines[#lines] == 0 do
table.remove(lines, #lines)
end
2017-10-11 20:31:48 +00:00
2018-01-24 22:39:38 +00:00
for _,sLine in ipairs(lines) do
table.insert(image.fg, { })
table.insert(image.bg, { })
table.insert(image.text, { })
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
--As we're no longer 1-1, we keep track of what index to write to
local writeIndex = 1
--Tells us if we've hit a 30 or 31 (BG and FG respectively)- next char specifies the curr colour
local bgNext, fgNext = false, false
--The current background and foreground colours
local currBG, currFG = nil,nil
for i = 1, #sLine do
local nextChar = string.sub(sLine, i, i)
if nextChar:byte() == 30 then
bgNext = true
elseif nextChar:byte() == 31 then
fgNext = true
elseif bgNext then
currBG = getColourOf(nextChar)
bgNext = false
elseif fgNext then
currFG = getColourOf(nextChar)
fgNext = false
else
if nextChar ~= " " and currFG == nil then
currFG = _G.colors.white
end
image.bg[num][writeIndex] = currBG
image.fg[num][writeIndex] = currFG
image.text[num][writeIndex] = nextChar
writeIndex = writeIndex + 1
end
end
image.height = num
if not image.width or writeIndex - 1 > image.width then
image.width = writeIndex - 1
end
num = num+1
end
return image
2016-12-11 19:24:52 +00:00
end
function NFT.load(path)
2018-01-24 22:39:38 +00:00
local imageText = Util.readFile(path)
if not imageText then
error('Unable to read image file')
end
return NFT.parse(imageText)
2016-12-11 19:24:52 +00:00
end
return NFT