Advertisement
skypop

CC Pixel API

Jul 11th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.33 KB | None | 0 0
  1. -- Pixel API
  2. -- by SukaiPoppuGo
  3. --
  4. -- Draw graphics with square pixels, without color restriction
  5. --
  6. -- # Graphic resolution:
  7. -- Graphic size -> 9 pixels wide, 9 pixels high
  8. -- Result on screen -> 9 char wide, 6 lines high
  9. --
  10. -- # Graphic table format
  11. -- Set of lines.
  12. -- Each line is a string, whose chars correspond to a pixel.
  13. -- A pixel is a char set in [0-9a-f], as color reference (see term.blit and colors API)
  14. -- Transparent pixels could be written by space " ", and stand for terminal background color
  15. --
  16. --
  17. -- # Usage
  18. --
  19. -- ## Compute graphics from NFP file
  20. --
  21. -- local oCG = pixel.load("gfx/icon.nfp")
  22. --
  23. -- ## Compute graphics from table
  24. --
  25. local brand = {
  26.     "0123  00000   0               0 ",
  27.     "4567  0    0                  0 ",
  28.     "89ab  00000   0  0   0  000   0 ",
  29.     "cdef  0       0   0 0  0   0  0 ",
  30.     "      0       0    0   00000  0 ",
  31.     "      0       0   0 0  0      0 ",
  32.     "      0       0  0   0  0000   0"
  33. }
  34. -- local oCG = pixel.compute(brand)
  35. --
  36. -- ## Draw graphics
  37. --
  38. -- term.setCursorPos(2, 2)
  39. -- pixel.draw( oCG )
  40. --
  41. -- ## Draw graphics on a monitor
  42. --
  43. -- local monitor = peripheral.find("monitor")
  44. -- local oCG = pixel.load("gfx/icon.nfp", monitor)
  45. -- monitor.setCursorPos(2, 2)
  46. -- pixel.draw( oCG, monitor )
  47. --
  48.  
  49. -- Colors references
  50. local _colors = {
  51.     [    1] = "0", -- white
  52.     [    2] = "1", -- orange
  53.     [    4] = "2", -- magenta
  54.     [    8] = "3", -- lightBlue
  55.     [   16] = "4", -- yellow
  56.     [   32] = "5", -- lime
  57.     [   64] = "6", -- pink
  58.     [  128] = "7", -- gray
  59.     [  256] = "8", -- lightGray
  60.     [  512] = "9", -- cyan
  61.     [ 1024] = "a", -- purple
  62.     [ 2048] = "b", -- blue
  63.     [ 4096] = "c", -- brown
  64.     [ 8192] = "d", -- green
  65.     [16384] = "e", -- red
  66.     [32768] = "f", -- black
  67. }
  68.  
  69. local _grayScale = {
  70.     ["0"] = "0", -- white
  71.     ["1"] = "8", -- orange
  72.     ["2"] = "8", -- magenta
  73.     ["3"] = "8", -- lightBlue
  74.     ["4"] = "8", -- yellow
  75.     ["5"] = "8", -- lime
  76.     ["6"] = "8", -- pink
  77.     ["7"] = "7", -- gray
  78.     ["8"] = "8", -- lightGray
  79.     ["9"] = "7", -- cyan
  80.     ["a"] = "7", -- purple
  81.     ["b"] = "7", -- blue
  82.     ["c"] = "7", -- brown
  83.     ["d"] = "7", -- green
  84.     ["e"] = "7", -- red
  85.     ["f"] = "f", -- black
  86. }
  87.  
  88. local function conv(ref, oTarget)
  89.     local C = oTarget.isColor() and ref or _grayScale[ ref ]
  90.     return C or " "
  91. end
  92.  
  93. -- Combine odd and even chars
  94. local function getChar(C, line_a, line_b, x, bg, outputLine, oTarget)
  95.     local pix_a = line_a and conv(line_a:sub(x,x), oTarget) or bg
  96.     local pix_b = line_b and conv(line_b:sub(x,x), oTarget) or bg
  97.     pix_a = (pix_a == " " or pix_a == "") and bg or pix_a
  98.     pix_b = (pix_b == " " or pix_b == "") and bg or pix_b
  99.     C = (pix_a == pix_b) and " " or C
  100.     return {
  101.         outputLine[1]..C,
  102.         outputLine[2]..pix_a,
  103.         outputLine[3]..pix_b
  104.     }
  105. end
  106.  
  107. -- pixel.compute( tGraphics [, oTarget ] )
  108. -- Compute graphic from a table
  109. -- param    table   tGraphics   Table of pixel lines
  110. -- param    object  oTarget Terminal object
  111. -- retrun   object  oCG     Computed graphics
  112. function compute(tGraphics, oTarget)
  113.     local mon = oTarget or term
  114.    
  115.     local output = {}
  116.     local bg = _colors[mon.getBackgroundColor()]
  117.    
  118.     for cy=1,#tGraphics,3 do
  119.         local oddLine, evenLine = {"","",""}, {"","",""}
  120.         local length = math.max(
  121.             tGraphics[cy]   and string.len(tGraphics[cy]) or 0,
  122.             tGraphics[cy+1] and string.len(tGraphics[cy+1]) or 0,
  123.             tGraphics[cy+3] and string.len(tGraphics[cy+2]) or 0
  124.         )
  125.         for cx=1,length do
  126.             oddLine = getChar("\143", tGraphics[cy], tGraphics[cy+1], cx, bg, oddLine, mon)
  127.             if tGraphics[cy+1] then
  128.                 evenLine = getChar("\131", tGraphics[cy+1], tGraphics[cy+2], cx, bg, evenLine, mon)
  129.             else
  130.                 evenLine = false
  131.             end
  132.         end
  133.         table.insert(output, {oddLine[1], oddLine[2], oddLine[3]})
  134.         if evenLine then
  135.             table.insert(output, {evenLine[1], evenLine[2], evenLine[3]})
  136.         end
  137.     end
  138.    
  139.     return output
  140. end
  141.  
  142. -- pixel.loadFile( tGraphics [, oTarget ] )
  143. -- Compute NFP file (Graphics files from paint program)
  144. -- param    string  sPath   Path to NFP file
  145. -- param    object  oTarget Terminal object
  146. -- retrun   object  oCG     Computed graphics
  147. function loadFile(sPath, oTarget)
  148.     local mon = oTarget or term
  149.    
  150.     local contents, line = {}
  151.     local handler = fs.open(sPath, "r")
  152.     while true do
  153.         line = handler.readLine()
  154.         if line then
  155.             table.insert(contents, line)
  156.         else
  157.             handler.close()
  158.             return compute(contents, mon)
  159.         end
  160.     end
  161. end
  162.  
  163. -- pixel.draw( oCG [, oTarget ] )
  164. -- param    table   oCG     Computed graphics object (get from pixel.compute() or pixel.loadFile())
  165. -- param    object  oTarget Terminal object
  166. function draw(oCG, oTarget)
  167.     local mon = oTarget or term
  168.    
  169.     local x, y = mon.getCursorPos()
  170.    
  171.     for _, line in pairs(oCG) do
  172.         mon.setCursorPos(x,y)
  173.         mon.blit( unpack(line) )
  174.         y = y+1
  175.     end
  176. end
  177.  
  178. local demo = ...
  179. if demo == "demo" then
  180.     local oCG = compute(brand)
  181.     term.setCursorPos(2,1)
  182.     term.clear()
  183.     print("Pixel API demo")
  184.     term.setCursorPos(2,2)
  185.     draw( oCG )
  186.     print("")
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement