Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- nft extra functions
- pastebin get J11BaaDy nfte
- list of extra functions:
- nft.flipY(table image)
- returns 'image' stretched vertically
- nft.flipX(table image)
- returns 'image' stretched horizontally
- nft.grayOut(table image)
- converts 'image' to grayscale
- nft.lighten(table image)
- returns 'image' but with brighter colors, suitable for transitions
- nft.darken(table image)
- returns 'image' but with darker colors, suitable for transitions
- nft.getSize(table image)
- returns the x and y sizes of 'image'
- nft.stretchImage(table image, number stretchedX, number stretchedY)
- returns 'image', but stretched to fit sizes 'stretchedX' and 'stretchedY'.
- nft.merge(table {image1,x,y}, table {image2,x,y}, ...)
- returns an image composed of image1 layered upon image2 layered upon image3 and so on.
- nft.fullScreen(number color, terminal object)
- returns a fullscreen picture made solidly of 'color'. sets size to current terminal or 'object' if defined.
- nft.unloadImage(table image)
- returns a string NFP picture made from 'image'.
- nft.autocrop(table image)
- returns 'image' but with the blank space cut off from the left and top.
- nft.drawImageBlit(table image, number x, number y)
- draws 'image' at 'x' and 'y' using term.blit. As such, it does not support transparency. You will want to merge images together before rendering.
- nft.drawImageBlitCenter(table image, number x, number y, terminal object)
- draws 'image' centered around 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
- nft.centerWithBlankSpace(table image, number x, number y, terminal object)
- returns 'image', but with extra blank space to center it to 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
- --]]
- local explode = function(div,str)
- if (div=='') then return false end
- local pos,arr = 0,{}
- for st,sp in function() return string.find(str,div,pos,true) end do
- arr[#arr+1] = str:sub(pos,st-1)
- pos = sp + 1
- end
- arr[#arr+1] = str:sub(pos)
- return arr
- end
- local blitcolors = {
- [" "] = allowZero and 0 or nil,
- ["0"] = colors.white,
- ["1"] = colors.orange,
- ["2"] = colors.magenta,
- ["3"] = colors.lightBlue,
- ["4"] = colors.yellow,
- ["5"] = colors.lime,
- ["6"] = colors.pink,
- ["7"] = colors.gray,
- ["8"] = colors.lightGray,
- ["9"] = colors.cyan,
- ["a"] = colors.purple,
- ["b"] = colors.blue,
- ["c"] = colors.brown,
- ["d"] = colors.green,
- ["e"] = colors.red,
- ["f"] = colors.black,
- }
- local BTC = function(_color,allowZero) --Blit To Color
- if _color == nil then return nil end
- return blitcolors[_color]
- end
- local importFromNFT = function(input)
- local tinput = explode("\n",input)
- local tcol,bcol
- local cx --represents the x position in the picture
- local sx --represents the x position in the file
- local charoutput,txtoutput,bgoutput = {}
- for y = 1, #tinput do
- charoutput[y], txtoutput[y], bgoutput[y] = {}, {}, {}
- tcol,bcol = colors.white,colors.black
- cx, sx = 1, 0
- while sx < #tinput[y] do
- sx = sx + 1
- if tinput[y]:sub(sx,sx) == "\30" then
- bcol = BTC(tinput[y]:sub(sx+1,sx+1))
- sx = sx + 1
- elseif tinput[y]:sub(sx,sx) == "\31" then
- tcol = BTC(tinput[y]:sub(sx+1,sx+1))
- sx = sx + 1
- else
- if tcol and bcol then
- charoutput[y][cx] = tinput[y]:sub(sx,sx)
- txtoutput[y][cx] = tcol
- bgoutput[y][cx] = bcol
- end
- cx = cx + 1
- end
- end
- end
- return charoutput, txtoutput, bgoutput
- end
- local exportToNFT = function(charinput, txtinput, bginput)
- local input = {}
- for y = 1, #bginput do
- for x = 1, #bginput[y] do
- input[#input+1] = {
- x = x,
- y = y,
- b = bginput[y][x],
- t = txtinput[y][x],
- c = charinput[y][x],
- }
- end
- end
- local doop = {}
- local p = input
- local pheight = 0
- local pwidth = 0
- local nftoutput = "\30 \31f"
- for a = 1, #p do
- if p[a].y > pheight then
- pheight = p[a].y
- end
- if p[a].x > pwidth then
- pwidth = p[a].x
- end
- end
- for k,v in pairs(p) do
- if not doop[v.y] then doop[v.y] = {} end
- doop[v.y][v.x] = {}
- doop[v.y][v.x]["char"] = v.c
- doop[v.y][v.x]["text"] = CTB(v.t) or " "
- doop[v.y][v.x]["back"] = CTB(v.b) or " "
- end
- for y = 1, pheight do
- if doop[y] then
- for x = 1, pwidth do
- local nWriteBackground
- local nWriteColor
- if doop[y][x] then
- if doop[y][x]["back"] ~= nWriteBackground then
- nWriteBackground = b
- nftoutput = nftoutput.."\30"..doop[y][x]["back"]
- end
- if doop[y][x]["text"] ~= nWriteColor then
- nWriteColor = c
- nftoutput = nftoutput.."\31"..doop[y][x]["text"]
- end
- nftoutput = nftoutput..doop[y][x]["char"]
- if (not doop[y][x+1]) and x ~= pwidth then
- nftoutput = nftoutput.."\30".." "
- end
- else
- nftoutput = nftoutput.." "
- end
- end
- end
- if y ~= pheight then
- nftoutput = nftoutput.."\n\30 \31f"
- end
- end
- return nftoutput
- end
- local round = function(x) return x + 0.5 - (x + 0.5) % 1 end
- local deepCopy = function(tbl) return {table.unpack(tbl)} end
- local bcol, ccol = {
- [0]=" ",[colors.white]="0",[colors.orange]="1",[colors.magenta]="2",[colors.lightBlue]="3",[colors.yellow]="4",[colors.lime]="5",[colors.pink]="6",[colors.gray]="7",[colors.lightGray]="8",[colors.cyan]="9",[colors.purple]="a",[colors.blue]="b",[colors.brown]="c",[colors.green]="d",[colors.red]="e",[colors.black]="f"}, {}
- for k,v in pairs(bcol) do ccol[v] = k end
- local flipY = function(image)
- local output = {}
- for y = #image, 1, -1 do output[#output+1] = image[y] end
- return output
- end
- nft.flipY = funtion(image)
- local c,t,b = importFromNFT(image)
- return exportToNFT(flipY(c),flipY(t),flipY(b))
- end
- local flipX = function(image)
- local output,sizeX = {}, 0
- for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end
- for y = 1, #image do
- output[y] = {}
- for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end
- end
- return output
- end
- nft.flipX = function(image)
- local c,t,b = importFromNFT(image)
- return exportToNFT(flipX(c),flipX(t),flipX(b))
- end
- local grayOut = function(image)
- local output,grays = {},{[0] = 0,[colors.white]=colors.white,[colors.orange]=colors.lightGray,[colors.magenta]=colors.lightGray,[colors.lightBlue]=colors.lightGray,[colors.yellow]=colors.white,[colors.lime]=colors.lightGray,[colors.pink]=colors.lightGray,[colors.gray]=colors.gray,[colors.lightGray]=colors.lightGray,[colors.cyan]=colors.lightGray,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.gray,[colors.green]=colors.lightGray,[colors.red]=colors.gray,[colors.black]=colors.black}
- for y = 1, #image do
- output[y] = {}
- for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end
- end
- return output
- end
- nft.grayOut = function(image)
- local c,t,b = importFromNFT(image)
- return exportToNFT(grayOut(c),grayOut(t),grayOut(b))
- end
- local lighten = function(image)
- local output,lighters = {},{[0] = 0,[colors.white] = colors.white,[colors.orange] = colors.yellow,[colors.magenta] = colors.pink,[colors.lightBlue] = colors.white,[colors.yellow] = colors.white,[colors.lime] = colors.white,[colors.pink] = colors.white,[colors.gray] = colors.lightGray,[colors.lightGray] = colors.white,[colors.cyan] = colors.lightBlue,[colors.purple] = colors.magenta,[colors.blue] = colors.lightBlue,[colors.brown] = colors.orange,[colors.green] = colors.lime,[colors.red] = colors.magenta,[colors.black] = colors.gray}
- for y = 1, #image do
- output[y] = {}
- for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end
- end
- return output
- end
- nft.lighten = function(image)
- local c,t,b = importFromNFT(image)
- return exportToNFT(lighten(c),lighten(t),lighten(b))
- end
- local darken = function(image)
- local output, darkers = {},{[0]=0,[colors.white]=colors.lightGray,[colors.orange]=colors.brown,[colors.magenta]=colors.purple,[colors.lightBlue]=colors.cyan,[colors.yellow]=colors.orange,[colors.lime]=colors.green,[colors.pink]=colors.magenta,[colors.gray]=colors.black,[colors.lightGray]=colors.gray,[colors.cyan]=colors.blue,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.black,[colors.green]=colors.gray,[colors.red]=colors.gray,[colors.black]=colors.black}
- for y = 1, #image do
- output[y] = {}
- for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end
- end
- return output
- end
- nft.darken = function(image)
- local c,t,b = importFromNFT(image)
- return exportToNFT(darken(c),darken(t),darken(b))
- end
- nft.getSize = function(nimage)
- local image = importFromNFT(nimage)
- local xsize = 0
- if type(image) ~= "table" then return 0,0 end
- for y = 1, #image do xsize = math.max(xsize,#image[y]) end
- return xsize, #image
- end
- local stretchImage = function(_image,sx,sy)
- local image,output = deepCopy(_image), {}
- if sx == 0 or sy == 0 then return {{}} end
- if sx < 0 then image = nft.flipX(image) end
- if sy < 0 then image = nft.flipY(image) end
- sx,sy = round(math.abs(sx)), round(math.abs(sy))
- if sx == 0 or sy == 0 then return {{}} end
- local imX,imY = nft.getSize(image)
- local xcur,ycur
- for y = 1, sy do
- output[y] = {}
- for x = 1, sx do
- output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil
- end
- end
- return output
- end
- nft.stretchImage = function(image,sx,sy)
- local c,t,b = importFromNFT(image)
- return exportToNFT(stretchImage(c,sx,sy),stretchImage(t,sx,sy),stretchImage(b,sx,sy))
- end
- nft.drawImageBlit = function(image,ix,iy)
- if (type(ix) ~= "number") or (type(iy) ~= "number") then
- error("Expected image, x, y", 2)
- else
- local c,t,b = importFromNFT(image)
- ix, iy = round(ix), round(iy)
- local buffc,bufft,buffb
- for y = 1, #image do
- buffc,bufft,buffb = "","",""
- for x = 1, #image[y] do
- buffc = buffc..bcol[c[y][x]]
- bufft = bufft..bcol[t[y][x]]
- buffb = buffb..bcol[b[y][x]]
- end
- term.setCursorPos(ix,iy+(y-1))
- term.blit(buffc,bufft,buffb)
- end
- end
- end
- local centerWithBlankSpace = function(_image,ix,iy,object)
- local image,scX,scY = deepCopy(_image)
- if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
- local imgXsize,imgYsize = paintutils.getSize(image)
- if imgXsize == 0 or imgYsize == 0 then return {{}} end
- local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2))
- local output = {}
- for y = 1, imgYsize + incY do
- output[y] = {}
- for x = 1, imgXsize + incX do
- if (x > incX) and (y > incY) then
- output[y][x] = image[y-incY][x-incX] or 0
- else
- output[y][x] = 0
- end
- end
- end
- return output
- end
- nft.centerWithBlankSpace = function(image,ix,iy,object)
- local c,t,b = importFromNFT(image)
- return exportToNFT(centerWithBlankSpace(c,ix,iy,object),centerWithBlankSpace(t,ix,iy,object),centerWithBlankSpace(b,ix,iy,object))
- end
- nft.drawImageBlitCenter = function(image,ix,iy,object)
- local scX,scY
- if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
- local imgXsize,imgYsize = nft.getSize(image)
- return nft.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2))
- end
- local merge = function(...)
- local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0
- for a = 1, #arg do
- local x,y = nft.getSize(arg[a][1])
- if not (x == 0 or y == 0) then
- x, y = x+arg[a][2], y+arg[a][3]
- imgXsize = math.max(imgXsize,x)
- imgYsize = math.max(imgYsize,y)
- end
- end
- for a = #arg, 1, -1 do
- image,xdiff,ydiff = table.unpack(arg[a])
- xdiff, ydiff = xdiff - 1, ydiff - 1
- for y = 1, imgYsize do
- output[y] = output[y] or {}
- for x = 1, imgXsize do
- output[y][x] = output[y][x] or 0
- if image[y-ydiff] then
- if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then
- output[y][x] = image[y-ydiff][x-xdiff]
- end
- end
- end
- end
- end
- return output
- end
- nft.merge = function(...)
- --uuhhhh
- end
- nft.fullScreen = function(bgcol,object)
- local scr_x,scr_y = (object or term).getSize()
- local output,l = {},{}
- bgcol = bgcol or colors.black
- for x = 1, scr_x do l[x] = bgcol end
- for y = 1, scr_y do output[y] = l end
- return output
- end
- nft.unloadImage = function(image)
- local output = ""
- for y = 1, #image do
- for x = 1, #image[y] do output = output..bcol[image[y][x]] end
- if y < #image then output = output.."\n" end
- end
- return output
- end
- nft.autocrop = function(image)
- local output,top,leftings = {},1,{}
- local isTopped = false
- for a = 1, #image do
- if (not isTopped) and (#image[a] > 0) then
- top = a
- isTopped = true
- end
- if isTopped then
- output[#output+1] = deepCopy(image[a])
- end
- end
- for y = 1, #output do
- leftings[y] = 0
- for x = 1, #output[y] do
- if (output[y][x] == 0) or (output[y][x] == nil) then
- leftings[y] = leftings[y] + 1
- else
- break
- end
- end
- end
- local left = math.min(table.unpack(leftings))
- for y = 1, #output do
- for l = 1, left do table.remove(output[y],1) end
- end
- return output
- end
Add Comment
Please, Sign In to add comment