Advertisement
fatboychummy

canvasWriter

Apr 11th, 2020
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. --[[
  2.   Made by fatboychummy
  3.   Feel free to edit and do stuff with it.  Open source is open source.
  4.  
  5.   Using this
  6.     (or the minified version, found at <Insert link here once uploaded>):
  7.  
  8.   local writer = require("whateveryousavedthisfileas")
  9.   writer(
  10.     text:string,
  11.     xpos:number,
  12.     ypos:number,
  13.     textColor:table[number,number,number],
  14.     backgroundColor:table[number,number,number]
  15.   )
  16.   Where:
  17.     text: the text you want to write
  18.     xpos: the x position of the text (Warning: Not like the terminal,
  19.           an x value of 2 only moves forward by one pixel rather than full
  20.           character from x = 1)
  21.     ypos: the y position of the text (Warning: Not like the terminal,
  22.           a y value of 2 only moves down by one pixel rather than full character
  23.           from y = 1)
  24.     textColor: a table of 3 rgb values [0-255] for the text color
  25.                {255, 0, 0} is red
  26.                {0, 255, 0} is blue
  27.                {0, 0, 255} is green
  28.       OR: a colo[u]rs api color
  29.     textColor: a table of 3 rgb values [0-255] for the background color
  30.                {255, 0, 0} is red
  31.                {0, 255, 0} is blue
  32.                {0, 0, 255} is green
  33.       OR: a colo[u]rs api color
  34.  
  35.   example:
  36.   writer("Hello world!", 0, 0, colors.white, colors.black)
  37.   example 2:
  38.   writer("Hello world!", 0, 10, {255, 255, 255}, {0, 0, 0})
  39.  
  40.   Note: this version requires the font to be saved in the same directory as this
  41.   program, and named "font[.lua]"
  42.  
  43.   The minified version does not have this requirement.
  44. ]]
  45.  
  46. local expect = dofile("rom/modules/main/cc/expect.lua").expect
  47. local a = require("font")
  48. local canvas = peripheral.call("back", "canvas")
  49. local colorConvert = {
  50.   [colors.white]     = {240, 240, 240},
  51.   [colors.orange]    = {242, 178, 51 },
  52.   [colors.magenta]   = {229, 127, 216},
  53.   [colors.lightBlue] = {153, 178, 242},
  54.   [colors.yellow]    = {222, 222, 108},
  55.   [colors.lime]      = {127, 204, 25 },
  56.   [colors.pink]      = {242, 178, 204},
  57.   [colors.gray]      = {76 , 76 , 76 },
  58.   [colors.lightGray] = {153, 153, 153},
  59.   [colors.cyan]      = {76 , 153, 178},
  60.   [colors.purple]    = {178, 102, 229},
  61.   [colors.blue]      = {51 , 102, 204},
  62.   [colors.brown]     = {127, 102, 76 },
  63.   [colors.green]     = {87 , 166, 78 },
  64.   [colors.red]       = {204, 76 , 76 },
  65.   [colors.black]     = {17 , 17 , 17 }
  66. }
  67.  
  68. local function write(arg, x, y, fg, bg)
  69.   arg = tostring(arg)
  70.   expect(2, x, "number")
  71.   expect(3, y, "number")
  72.   if type(fg) == "number" then
  73.     fg = colorConvert[fg]
  74.     if not fg then
  75.       error("Bad argument #4: Not a color.", 2)
  76.     end
  77.   end
  78.   if type(fg) ~= "table" then
  79.     error(string.format("Bad argument #4: Expected table or number, got %s.", fg))
  80.   end
  81.   if type(bg) == "number" then
  82.     bg = colorConvert[bg]
  83.     if not bg then
  84.       error("Bad argument #5: Not a color.", 2)
  85.     end
  86.   end
  87.   if type(bg) ~= "table" then
  88.     error(string.format("Bad argument #5: Expected table or number, got %s.", bg))
  89.   end
  90.  
  91.   local fr, fg_, fb = table.unpack(fg, 1, 3)
  92.   local br, bg_, bb = table.unpack(bg, 1, 3)
  93.  
  94.   local char = 0
  95.   for letter in arg:gmatch(".") do
  96.     local num = string.byte(letter)
  97.     local sxPos = 2 + 6 * (num % 16) + 2 * (num % 16)
  98.     local syPos = 2 + 9 * math.floor(num / 16) + 2 * math.floor(num / 16)
  99.     local ypos = y
  100.     local xpos = x + (6 * char)
  101.     local tmp = canvas.addRectangle(xpos, ypos, 6, 9)
  102.     tmp.setColor(br, bg_, bb)
  103.     for y = syPos, syPos + 8 do
  104.       local xtbl = a[y]
  105.       xpos = x + 6*char
  106.       for x = sxPos, sxPos + 5 do
  107.         local val = xtbl[x]
  108.         if val == 1 then
  109.           local tmp = canvas.addRectangle(xpos, ypos, 1, 1)
  110.           tmp.setColor(fr, fg_, fb)
  111.         end
  112.         xpos = xpos + 1
  113.       end
  114.       ypos = ypos + 1
  115.     end
  116.     char = char + 1
  117.   end
  118. end
  119.  
  120. return write
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement