Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local drawing = {}
- ------------------------------------------------
- local EU = require("ExtraUtilits")
- local io = require("io")
- local computer = require("computer")
- local serialization = require("serialization")
- local fs = require("filesystem")
- local gpu = require("component").gpu
- ------------------------------------------------
- function drawing.draw(x, y, image)
- local field, width, height = image.field, image.width, image.height
- for i = 1, width do
- for j = 1, height do
- if field[i][j].symbol then
- if field[i][j].background ~= gpu.getBackground() then gpu.setBackground(field[i][j].background) end
- if field[i][j].foreground ~= gpu.getForeground() then gpu.setForeground(field[i][j].foreground) end
- gpu.set(x + i - 1, y + j - 1, field[i][j].symbol)
- end
- end
- end
- end
- ------------------------------------------------
- function drawing.load(path)
- local file = io.open(path, "r")
- local data = serialization.unserialize(file:read())
- local w, h = data.w, data.h
- local field = {}
- for i = 1, w do
- field[i] = {}
- for j = 1, h do
- field[i][j] = {}
- local line = file:read()
- if line ~= "-" then
- local backstring = string.sub(line, 1, 3)
- local forestring = string.sub(line, 4, 6)
- local symbol = string.sub(line, 7, 7)
- local backgroundchars = {string.sub(backstring, 1, 1), string.sub(backstring, 2, 2), string.sub(backstring, 3, 3)}
- local foregroundchars = {string.sub(forestring, 1, 1), string.sub(forestring, 2, 2), string.sub(forestring, 3, 3)}
- local backRGB = {string.byte(backgroundchars[1]) or 0, string.byte(backgroundchars[2]) or 0, string.byte(backgroundchars[3]) or 0}
- local foreRGB = {string.byte(foregroundchars[1]) or 0, string.byte(foregroundchars[2]) or 0, string.byte(foregroundchars[3]) or 0}
- field[i][j].background = EU.Color.Pack(table.unpack(backRGB))
- field[i][j].foreground = EU.Color.Pack(table.unpack(foreRGB))
- field[i][j].symbol = symbol
- end
- end
- end
- file:close()
- return {field = field, width = w, height = h}
- end
- ------------------------------------------------
- function drawing.save(field, w, h, path)
- local file = io.open(path, "wb")
- file:write(serialization.serialize({w = w, h = h}) .. "\n")
- for i = 1, w do
- for j = 1, h do
- if field[i][j].symbol then
- local background = {EU.Color.Extract(field[i][j].background)}
- local foreground = {EU.Color.Extract(field[i][j].foreground)}
- local symbol = field[i][j].symbol
- local back = string.char(background[1]) .. string.char(background[2]) .. string.char(background[3])
- local fore = string.char(foreground[1]) .. string.char(foreground[2]) .. string.char(foreground[3])
- file:write(back .. fore .. symbol .. "\n")
- else
- file:write("-\n")
- end
- end
- end
- file:close()
- end
- ------------------------------------------------
- function drawing.getPixel(image, x, y)
- if not image.field[x][y].symbol then return end
- local pixel = {}
- pixel.color = {}
- pixel.color.background = {}
- pixel.color.foreground = {}
- pixel.color.background.color = image.field[x][y].background
- pixel.color.foreground.color = image.field[x][y].foreground
- pixel.color.background.r, pixel.color.background.g, pixel.color.background.b = EU.Color.Extract(pixel.color.background.color)
- pixel.color.foreground.r, pixel.color.foreground.g, pixel.color.foreground.b = EU.Color.Extract(pixel.color.foreground.color)
- pixel.symbol = image.field[x][y].symbol
- return pixel
- end
- ------------------------------------------------
- function drawing.setPixel(image, x, y, pixel)
- if not pixel then image.field[x][y].symbol = nil return image end
- image.field[x][y].background = pixel.color.background.color
- image.field[x][y].foreground = pixel.color.foreground.color
- image.field[x][y].symbol = pixel.symbol
- return image
- end
- ------------------------------------------------
- function drawing.drawPixel(x, y, pixel)
- if not pixel then return end
- if pixel.color.background.color ~= gpu.getBackground() then gpu.setBackground(pixel.color.background.color) end
- if pixel.color.foreground.color ~= gpu.getForeground() then gpu.setForeground(pixel.color.foreground.color) end
- gpu.set(x, y, pixel.symbol)
- end
- ------------------------------------------------
- function drawing.setScale(image, w, h)
- image.width, image.height = w, h
- return image
- end
- ------------------------------------------------
- function drawing.clear(image)
- local width, height = image.width, image.height
- for i = 1, width do
- image.field[i] = {}
- for j = 1, height do
- image.field[i][j] = {}
- end
- end
- return image
- end
- ------------------------------------------------
- function drawing.image(w, h)
- local image = {field = {}, width = w, height = h}
- image = drawing.clear(image)
- return image
- end
- function drawing.pixel(background, foreground, symbol)
- if not symbol then return nil end
- local pixel = {}
- pixel.color = {}
- pixel.color.background = {}
- pixel.color.foreground = {}
- pixel.color.background.color = background
- pixel.color.foreground.color = foreground
- pixel.color.background.r, pixel.color.background.g, pixel.color.background.b = EU.Color.Extract(pixel.color.background.color)
- pixel.color.foreground.r, pixel.color.foreground.g, pixel.color.foreground.b = EU.Color.Extract(pixel.color.foreground.color)
- pixel.symbol = symbol
- return pixel
- end
- ------------------------------------------------
- function drawing.filter(image, filter)
- for x = 1, image.w do
- for y = 1, image.h do
- image[x][y] = filter(image[x][y])
- end
- end
- return image
- end
- ------------------------------------------------
- return drawing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement