tommyroyall

Graphics

Oct 29th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. function getColor ( mctag )
  2.     if mctag == "&0" then
  3.         return 32768
  4.     elseif mctag == "&f" then
  5.         return 1
  6.     elseif mctag == "&e" then
  7.         return 16
  8.     elseif mctag == "&a" then
  9.         return colors.lime
  10.     elseif mctag == "&b" then
  11.         return colors.lightBlue
  12.     elseif mctag == "&c" then
  13.         return colors.red
  14.     elseif mctag == "&d" then
  15.         return colors.pink
  16.     elseif mctag == "&1" then
  17.         return colors.blue
  18.     elseif mctag == "&2" then
  19.         return colors.green
  20.     elseif mctag == "&3" then
  21.         return colors.cyan
  22.     elseif mctag == "&4" then
  23.         return colors.red
  24.     elseif mctag == "&5" then
  25.         return colors.purple
  26.     elseif mctag == "&6" then
  27.         return colors.orange
  28.     elseif mctag == "&7" then
  29.         return colors.lightGray
  30.     elseif mctag == "&8" then
  31.         return colors.gray
  32.     elseif mctag == "&9" then
  33.         return colors.cyan
  34.     else
  35.         return 1
  36.     end
  37. end
  38. function setColor ( mctag )
  39.     term.setTextColor(getColor(mctag))
  40. end
  41. function pColor ( text, color, bcolor )
  42.     if bcolor == nil then
  43.     term.setTextColor(getColor(color))
  44.     print(text)
  45.     term.setTextColor(getColor("&f"))
  46.     else
  47.     term.setBackgroundColor(getColor(color))
  48.     term.setTextColor(getColor(color))
  49.     print(text)
  50.     term.setBackgroundColor(getColor("&f"))
  51.     term.setTextColor(getColor("&0"))
  52.     end
  53. end
  54. function setBColor ( mctag )
  55.     term.setBackgroundColor(getColor(mctag))
  56. end
  57.  
  58. nitro_api = {}
  59. function nitro_api.split(str, pat)
  60.   local t = { }
  61.   local fpat = "(.-)"..pat
  62.   local last_end = 1
  63.   local s, e, cap = str:find(fpat, 1)
  64.   while s do
  65.     if s ~= 1 or cap ~= "" then
  66.       table.insert(t,cap)
  67.     end
  68.     last_end = e+1
  69.     s, e, cap = str:find(fpat, last_end)
  70.   end
  71.   if last_end <= #str then
  72.     cap = str:sub(last_end)
  73.     table.insert(t, cap)
  74.   end
  75.   return t
  76. end
  77.  
  78. function pBack ( text, color )
  79.     term.setBackgroundColor(getColor(color))
  80.     if color == "&f" then
  81.         term.setTextColor(getColor("&0"))
  82.         print(text)
  83.         term.setTextColor(getColor("&f"))
  84.         term.setBackgroundColor(getColor("&0"))
  85.     else
  86.         print(text)
  87.         term.setBackgroundColor(getColor("&0"))
  88.     end
  89. end
  90.  
  91. function resetScr ()
  92.     term.setBackgroundColor(getColor("&0"))
  93.     term.setTextColor(getColor("&f"))
  94. end
  95.  
  96. function setPixel ( pix, piy, mcTag )
  97.     oldx, oldy = term.getCursorPos()
  98.     term.setCursorPos(pix,piy)
  99.     term.setBackgroundColor(getColor(mcTag))
  100.     write(" ")
  101.     term.setBackgroundColor(getColor("&0"))
  102.     term.setCursorPos(oldx,oldy)
  103. end
  104.  
  105. function get_png_raw ( sfile )
  106.     if not fs.exists(sfile) then
  107.         return nil
  108.     elseif fs.isDir(sfile) then
  109.         return nil
  110.     else
  111.     local kb_0001 = io.open(sfile, "r")
  112.     local slines = {}
  113.     for line in kb_0001:lines() do
  114.         slines[#slines+1] = line
  115.     end
  116.     kb_0001:close()
  117.     local img_data = {}
  118.     for n,m in ipairs(slines) do
  119.         img_data[n] = nitro_api.split(m, " ")
  120.     end
  121.     return img_data
  122.     end
  123. end
  124.  
  125. function draw_png ( sfile )
  126.     if not fs.exists(sfile) then
  127.         return nil
  128.     elseif fs.isDir(sfile) then
  129.         return nil
  130.     else
  131.         oldX, oldY = term.getCursorPos()
  132.         image_data = get_png_raw(sfile)
  133.         for n, m in ipairs(image_data) do
  134.             for zz, zy in ipairs(m) do
  135.                 setPixel(oldX+zz-1, oldY+n, zy)
  136.             end
  137.         end
  138.         return true
  139.     end
  140. end
  141.  
  142. function export_png ( sfile, image_data )
  143.     finalString = ""
  144.     for n,m in ipairs(image_data) do
  145.         for x,y in ipairs(m) do
  146.             finalString = finalString..y.." "
  147.         end
  148.         finalString = string.sub(finalString,1,string.len(finalString)-1)
  149.         finalString = finalString.."\n"
  150.     end
  151.     local ame = io.open(sfile, "w")
  152.     ame:write(finalString)
  153.     ame:close()
  154. end
  155.  
  156. function drawLine(x1, y1, x2, y2, tag)
  157.     local xdif = x1-x2
  158.     local ydif = y1-y2
  159.     local grad = ydif/xdif
  160.     local curpos = 0
  161.       for i=1,ydif,1 do
  162.         if grad <= 1 then
  163.           setPixel(x1+curpos, i)
  164.         else
  165.           for j=1,tonumber(grad),1 do
  166.             setPixel(x1+curpos+j, i)
  167.           end
  168.         end
  169.           curpos = curpos + grad
  170.       end
  171. end
  172.  
  173. function drawLine(pos1, pos2, tag)
  174.     drawLine(pos1[0], pos1[1], pos2[0], pos2[1], tag)
  175. end
  176.  
  177. function drawSquare(corner1, corner2, corner3, corner4, tag)
  178.     drawLine(corner1, corner2, tag)
  179.     drawLine(corner2, corner3, tag)
  180.     drawLine(corner3, corner4, tag)
  181.     drawline(corner4, corner1, tag)
  182. end
  183.  
  184. function drawCircle(center, radius, tag, fill)        -- (x-a)^2 + (y-B)^2 = r^2 (formula, where r is radius, x and y are coordinates and a and b is center)        
  185.     local rads = radius^2        
  186.     local topright = {center[0]-radius, center[1]+radius}        
  187.     local bottomleft = {center[0]+radius, center[1]-radius}        
  188.         for cx=topright[0],bottomleft[0],1 do
  189.             for cy=topright[1],bottomleft[1],1 do
  190.                 local xb = (cx-center[0])^2
  191.                 local yb = (cy-center[1])^2
  192.                 if (xb+yb) == rads then
  193.                     setPixel(cx, cy, tag)
  194.                 elseif fill and (xb+yb) <= rads then
  195.                     setPixel(cx,  cy, tag)
  196.                 end
  197.             end        
  198.         end
  199. end
  200.  
  201. function drawPolygon(points, tag)
  202.     for i,v in ipairs(points) do
  203.         local precorner = nil
  204.             if i == 1 then                        
  205.                 precorner = points[#points]
  206.             else
  207.                 precorner = points[i-1]
  208.             end
  209.                 drawLine(v, precorner, tag)
  210.     end
  211. end
Advertisement
Add Comment
Please, Sign In to add comment