Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pixel API
- -- by SukaiPoppuGo
- --
- -- Draw graphics with square pixels, without color restriction
- --
- -- # Graphic resolution:
- -- Graphic size -> 9 pixels wide, 9 pixels high
- -- Result on screen -> 9 char wide, 6 lines high
- --
- -- # Graphic table format
- -- Set of lines.
- -- Each line is a string, whose chars correspond to a pixel.
- -- A pixel is a char set in [0-9a-f], as color reference (see term.blit and colors API)
- -- Transparent pixels could be written by space " ", and stand for terminal background color
- --
- --
- -- # Usage
- --
- -- ## Compute graphics from NFP file
- --
- -- local oCG = pixel.load("gfx/icon.nfp")
- --
- -- ## Compute graphics from table
- --
- local brand = {
- "0123 00000 0 0 ",
- "4567 0 0 0 ",
- "89ab 00000 0 0 0 000 0 ",
- "cdef 0 0 0 0 0 0 0 ",
- " 0 0 0 00000 0 ",
- " 0 0 0 0 0 0 ",
- " 0 0 0 0 0000 0"
- }
- -- local oCG = pixel.compute(brand)
- --
- -- ## Draw graphics
- --
- -- term.setCursorPos(2, 2)
- -- pixel.draw( oCG )
- --
- -- ## Draw graphics on a monitor
- --
- -- local monitor = peripheral.find("monitor")
- -- local oCG = pixel.load("gfx/icon.nfp", monitor)
- -- monitor.setCursorPos(2, 2)
- -- pixel.draw( oCG, monitor )
- --
- -- Colors references
- local _colors = {
- [ 1] = "0", -- white
- [ 2] = "1", -- orange
- [ 4] = "2", -- magenta
- [ 8] = "3", -- lightBlue
- [ 16] = "4", -- yellow
- [ 32] = "5", -- lime
- [ 64] = "6", -- pink
- [ 128] = "7", -- gray
- [ 256] = "8", -- lightGray
- [ 512] = "9", -- cyan
- [ 1024] = "a", -- purple
- [ 2048] = "b", -- blue
- [ 4096] = "c", -- brown
- [ 8192] = "d", -- green
- [16384] = "e", -- red
- [32768] = "f", -- black
- }
- local _grayScale = {
- ["0"] = "0", -- white
- ["1"] = "8", -- orange
- ["2"] = "8", -- magenta
- ["3"] = "8", -- lightBlue
- ["4"] = "8", -- yellow
- ["5"] = "8", -- lime
- ["6"] = "8", -- pink
- ["7"] = "7", -- gray
- ["8"] = "8", -- lightGray
- ["9"] = "7", -- cyan
- ["a"] = "7", -- purple
- ["b"] = "7", -- blue
- ["c"] = "7", -- brown
- ["d"] = "7", -- green
- ["e"] = "7", -- red
- ["f"] = "f", -- black
- }
- local function conv(ref, oTarget)
- local C = oTarget.isColor() and ref or _grayScale[ ref ]
- return C or " "
- end
- -- Combine odd and even chars
- local function getChar(C, line_a, line_b, x, bg, outputLine, oTarget)
- local pix_a = line_a and conv(line_a:sub(x,x), oTarget) or bg
- local pix_b = line_b and conv(line_b:sub(x,x), oTarget) or bg
- pix_a = (pix_a == " " or pix_a == "") and bg or pix_a
- pix_b = (pix_b == " " or pix_b == "") and bg or pix_b
- C = (pix_a == pix_b) and " " or C
- return {
- outputLine[1]..C,
- outputLine[2]..pix_a,
- outputLine[3]..pix_b
- }
- end
- -- pixel.compute( tGraphics [, oTarget ] )
- -- Compute graphic from a table
- -- param table tGraphics Table of pixel lines
- -- param object oTarget Terminal object
- -- retrun object oCG Computed graphics
- function compute(tGraphics, oTarget)
- local mon = oTarget or term
- local output = {}
- local bg = _colors[mon.getBackgroundColor()]
- for cy=1,#tGraphics,3 do
- local oddLine, evenLine = {"","",""}, {"","",""}
- local length = math.max(
- tGraphics[cy] and string.len(tGraphics[cy]) or 0,
- tGraphics[cy+1] and string.len(tGraphics[cy+1]) or 0,
- tGraphics[cy+3] and string.len(tGraphics[cy+2]) or 0
- )
- for cx=1,length do
- oddLine = getChar("\143", tGraphics[cy], tGraphics[cy+1], cx, bg, oddLine, mon)
- if tGraphics[cy+1] then
- evenLine = getChar("\131", tGraphics[cy+1], tGraphics[cy+2], cx, bg, evenLine, mon)
- else
- evenLine = false
- end
- end
- table.insert(output, {oddLine[1], oddLine[2], oddLine[3]})
- if evenLine then
- table.insert(output, {evenLine[1], evenLine[2], evenLine[3]})
- end
- end
- return output
- end
- -- pixel.loadFile( tGraphics [, oTarget ] )
- -- Compute NFP file (Graphics files from paint program)
- -- param string sPath Path to NFP file
- -- param object oTarget Terminal object
- -- retrun object oCG Computed graphics
- function loadFile(sPath, oTarget)
- local mon = oTarget or term
- local contents, line = {}
- local handler = fs.open(sPath, "r")
- while true do
- line = handler.readLine()
- if line then
- table.insert(contents, line)
- else
- handler.close()
- return compute(contents, mon)
- end
- end
- end
- -- pixel.draw( oCG [, oTarget ] )
- -- param table oCG Computed graphics object (get from pixel.compute() or pixel.loadFile())
- -- param object oTarget Terminal object
- function draw(oCG, oTarget)
- local mon = oTarget or term
- local x, y = mon.getCursorPos()
- for _, line in pairs(oCG) do
- mon.setCursorPos(x,y)
- mon.blit( unpack(line) )
- y = y+1
- end
- end
- local demo = ...
- if demo == "demo" then
- local oCG = compute(brand)
- term.setCursorPos(2,1)
- term.clear()
- print("Pixel API demo")
- term.setCursorPos(2,2)
- draw( oCG )
- print("")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement