Advertisement
LDDestroier

Paint Utils Extra

Oct 25th, 2017
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.11 KB | None | 0 0
  1. --[[
  2.  paintutils extra functions
  3.  pastebin get 7XAmsAbX pue
  4.  
  5. list of extra functions:
  6.  paintutils.flipY(table image)
  7.     returns 'image' stretched vertically
  8.  paintutils.flipX(table image)
  9.     returns 'image' stretched horizontally
  10.  paintutils.grayOut(table image)
  11.     converts 'image' to grayscale
  12.  paintutils.lighten(table image)
  13.     returns 'image' but with brighter colors, suitable for transitions
  14.  paintutils.darken(table image)
  15.     returns 'image' but with darker colors, suitable for transitions
  16.  paintutils.getSize(table image)
  17.     returns the x and y sizes of 'image'
  18.  paintutils.stretchImage(table image, number stretchedX, number stretchedY)
  19.     returns 'image', but stretched to fit sizes 'stretchedX' and 'stretchedY'.
  20.  paintutils.merge(table {image1,x,y}, table {image2,x,y}, ...)
  21.     returns an image composed of image1 layered upon image2 layered upon image3 and so on.
  22.  paintutils.fullScreen(number color, terminal object)
  23.     returns a fullscreen picture made solidly of 'color'. sets size to current terminal or 'object' if defined.
  24.  paintutils.unloadImage(table image)
  25.     returns a string NFP picture made from 'image'.
  26.  paintutils.autocrop(table image)
  27.     returns 'image' but with the blank space cut off from the left and top.
  28.  paintutils.drawImageBlit(table image, number x, number y)
  29.     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.
  30.  paintutils.drawImageBlitCenter(table image, number x, number y, terminal object)
  31.     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.
  32.  paintutils.drawImageCenter(table image, number x, number y, terminal object)
  33.     same as drawImageBlitCenter, but with the normal paintutils.drawImage. 'object' can be defined to use its screen size instead.
  34.  paintutils.centerWithBlankSpace(table image, number x, number y, terminal object)
  35.     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.
  36. --]]
  37. local round = function(x) return x + 0.5 - (x + 0.5) % 1 end
  38. local deepCopy = function(tbl) return {table.unpack(tbl)} end
  39. local bcol, ccol = {
  40. [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"}, {}
  41. for k,v in pairs(bcol) do ccol[v] = k end
  42. paintutils.flipY = function(image)
  43.     local output = {}
  44.     for y = #image, 1, -1 do output[#output+1] = image[y] end
  45.     return output
  46. end
  47. paintutils.flipX = function(image)
  48.     local output,sizeX = {}, 0
  49.     for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end
  50.     for y = 1, #image do
  51.         output[y] = {}
  52.         for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end
  53.     end
  54.     return output
  55. end
  56. paintutils.grayOut = function(image)
  57.     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}
  58.     for y = 1, #image do
  59.         output[y] = {}
  60.         for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end
  61.     end
  62.     return output
  63. end
  64. paintutils.lighten = function(image)
  65.     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}
  66.     for y = 1, #image do
  67.         output[y] = {}
  68.         for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end
  69.     end
  70.     return output
  71. end
  72. paintutils.darken = function(image)
  73.     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}
  74.     for y = 1, #image do
  75.         output[y] = {}
  76.         for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end
  77.     end
  78.     return output
  79. end
  80. paintutils.getSize = function(image)
  81.     local xsize = 0
  82.     if type(image) ~= "table" then return 0,0 end
  83.     for y = 1, #image do xsize = math.max(xsize,#image[y]) end
  84.     return xsize, #image
  85. end
  86. paintutils.stretchImage = function(_image,sx,sy)
  87.     local image,output = deepCopy(_image), {}
  88.     if sx == 0 or sy == 0 then return {{}} end
  89.     if sx < 0 then image = paintutils.flipX(image) end
  90.     if sy < 0 then image = paintutils.flipY(image) end
  91.     sx,sy = round(math.abs(sx)), round(math.abs(sy))
  92.     if sx == 0 or sy == 0 then return {{}} end
  93.     local imX,imY = paintutils.getSize(image)
  94.     local xcur,ycur
  95.     for y = 1, sy do
  96.         output[y] = {}
  97.         for x = 1, sx do
  98.             output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil
  99.         end
  100.     end
  101.     return output
  102. end
  103. paintutils.drawImageBlit = function(image,ix,iy,object)
  104.     if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then
  105.         error("Expected image, x, y", 2)
  106.     else
  107.         object = object or term
  108.         ix, iy = round(ix), round(iy)
  109.         local buff
  110.         for y = 1, #image do
  111.             buff = ""
  112.             for x = 1, #image[y] do
  113.                 buff = buff..bcol[image[y][x]]
  114.             end
  115.             object.setCursorPos(ix,iy+(y-1))
  116.             object.blit(buff,buff,buff)
  117.         end
  118.     end
  119. end
  120. paintutils.drawImage = function(image,ix,iy,object)
  121.     if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then
  122.         error("Expected image, x, y", 2)
  123.     else
  124.         object = object or term
  125.         for y = 1, #image do
  126.             object.setCursorPos(ix,iy+(y-1))
  127.             for x = 1, #image[y] do
  128.                 if image[y][x] == 0 then
  129.                     object.setCursorPos(ix+x,iy+(y-1))
  130.                 else
  131.                     object.blit(" ","0",bcol[image[y][x]])
  132.                     if x == #image[y] and y == #image then --gotta have that backwards compatibility
  133.                         object.setBackgroundColor(image[y][x])
  134.                     end
  135.                 end
  136.             end
  137.         end
  138.     end
  139. end
  140. paintutils.centerWithBlankSpace = function(_image,ix,iy,object)
  141.     local image,scX,scY = deepCopy(_image)
  142.     if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
  143.     local imgXsize,imgYsize = paintutils.getSize(image)
  144.     if imgXsize == 0 or imgYsize == 0 then return {{}} end
  145.     local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2))
  146.     local output = {}
  147.     for y = 1, imgYsize + incY do
  148.         output[y] = {}
  149.         for x = 1, imgXsize + incX do
  150.             if (x > incX) and (y > incY) then
  151.                 output[y][x] = image[y-incY][x-incX] or 0
  152.             else
  153.                 output[y][x] = 0
  154.             end
  155.         end
  156.     end
  157.     return output
  158. end
  159. paintutils.drawImageBlitCenter = function(image,ix,iy,object)
  160.     local scX,scY
  161.     object = object or term
  162.     if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
  163.     local imgXsize,imgYsize = paintutils.getSize(image)
  164.     return paintutils.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2), object)
  165. end
  166.  
  167. paintutils.drawImageCenter = function(image,ix,iy,object)
  168.     local scX,scY
  169.     object = object or term
  170.     scX,scY = object.getSize()
  171.     local imgXsize,imgYsize = paintutils.getSize(image)
  172.     return paintutils.drawImage(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2), object)
  173. end
  174. paintutils.merge = function(...)
  175.     local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0
  176.     for a = 1, #arg do
  177.         local x,y = paintutils.getSize(arg[a][1])
  178.         if not (x == 0 or y == 0) then
  179.             x, y = x+arg[a][2], y+arg[a][3]
  180.             imgXsize = math.max(imgXsize,x)
  181.             imgYsize = math.max(imgYsize,y)
  182.         end
  183.     end
  184.     for a = #arg, 1, -1 do
  185.         image,xdiff,ydiff = table.unpack(arg[a])
  186.         xdiff, ydiff = xdiff - 1, ydiff - 1
  187.         for y = 1, imgYsize do
  188.             output[y] = output[y] or {}
  189.             for x = 1, imgXsize do
  190.                 output[y][x] = output[y][x] or 0
  191.                 if image[y-ydiff] then
  192.                     if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then
  193.                         output[y][x] = image[y-ydiff][x-xdiff]
  194.                     end
  195.                 end
  196.             end
  197.         end
  198.     end
  199.     return output
  200. end
  201. paintutils.fullScreen = function(bgcol,object)
  202.     local scr_x,scr_y = (object or term).getSize()
  203.     local output,l = {},{}
  204.     bgcol = bgcol or colors.black
  205.     for x = 1, scr_x do l[x] = bgcol end
  206.     for y = 1, scr_y do output[y] = l end
  207.     return output
  208. end
  209. paintutils.unloadImage = function(image)
  210.     local output = ""
  211.     for y = 1, #image do
  212.         for x = 1, #image[y] do output = output..bcol[image[y][x]] end
  213.         if y < #image then output = output.."\n" end
  214.     end
  215.     return output
  216. end
  217. paintutils.autocrop = function(image)
  218.     local output,top,leftings = {},1,{}
  219.     local isTopped = false
  220.     for a = 1, #image do
  221.         if (not isTopped) and (#image[a] > 0) then
  222.             top = a
  223.             isTopped = true
  224.         end
  225.         if isTopped then
  226.             output[#output+1] = deepCopy(image[a])
  227.         end
  228.     end
  229.     for y = 1, #output do
  230.         leftings[y] = 0
  231.         for x = 1, #output[y] do
  232.             if (output[y][x] == 0) or (output[y][x] == nil) then
  233.                 leftings[y] = leftings[y] + 1
  234.             else
  235.                 break
  236.             end
  237.         end
  238.     end
  239.     local left = math.min(table.unpack(leftings))
  240.     for y = 1, #output do
  241.         for l = 1, left do table.remove(output[y],1) end
  242.     end
  243.     return output
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement