Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This is the very basic libHLTI API. 2 functions, but they work!
- --Since HLTI is actually just many Lua tables, we can use it like this:
- local libHLTI = {}
- libHLTI.GetValues = function(file)
- if not fs.exists(file) then
- error("HLTI file doesn't exist.")
- end
- local file = fs.open(file,'r')
- local values = textutils.unserialize(file.readAll())
- if not values then
- error("HLTI image is misconfigured/corrupted.")
- end
- file.close()
- return values
- end
- libHLTI.SetValue = function(file, key, value)
- if not fs.exists(file) then
- error("HLTI file doesn't exist.")
- end
- local currentValues = libHLTI.GetValues(file)
- currentValues[key] = value
- local file = fs.open(file, 'w')
- file.write(textutils.serialize(currentValues))
- file.close()
- end
- -- Determines if the file exists, and can be edited on this computer (tooken from paint)
- local tArgs = {...}
- if #tArgs == 0 then
- print("Usage: hltipaint <path> <width> <height>")
- return
- end
- local sPath = shell.resolve(tArgs[1])
- local bReadOnly = fs.isReadOnly(sPath)
- if fs.exists(sPath) and fs.isDir(sPath) then
- print("Cannot edit a directory.")
- return
- end
- local canvas = {}
- ---------------
- -- Functions -- tooken from /rom/programs/paint
- ---------------
- local function getCanvasPixel( x, y )
- if canvas[y] then
- return canvas[y][x]
- end
- return nil
- end
- --[[
- Converts a colour value to a text character
- params: colour = the number to convert to a hex value
- returns: a string representing the chosen colour
- ]]
- local function getCharOf( colour )
- -- Incorrect values always convert to nil
- if type(colour) == "number" then
- local value = math.floor( math.log(colour) / math.log(2) ) + 1
- if value >= 1 and value <= 16 then
- return string.sub( "0123456789abcdef", value, value )
- end
- end
- return " "
- end
- --[[
- Converts a text character to colour value
- params: char = the char (from string.byte) to convert to number
- returns: the colour number of the hex value
- ]]
- local tColourLookup = {}
- for n=1,16 do
- tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
- end
- local function getColourOf( char )
- -- Values not in the hex table are transparent (canvas coloured)
- return tColourLookup[char]
- end
- print("Converting...")
- os.loadAPI("libHLTI")
- local h = fs.open("out.hlti", "w")
- h.write("{}")
- h.close()
- local w = tArgs[2]
- local h = tArgs[3]
- if w == nil then w,h = term.getSize() end
- libHLTI.SetValue("out.hlti", "HLTIVer", 1)
- libHLTI.SetValue("out.hlti", "Width", w)
- libHLTI.SetValue("out.hlti", "Height", h)
- local sectionCount = 0
- local file = fs.open(sPath, "r")
- local sLine = file.readLine()
- local y = 1
- while sLine do
- for x=1,w do
- term.setCursorPos(1,1)
- print("Section " .. sectionCount .. " | Pixel X" .. x .. " Y" .. y)
- color = getColourOf(string.byte(sLine,x,x))
- if color == colors.transparent or color == 0 then color = colors.black end
- libHLTI.SetValue("out.hlti", "Section" .. sectionCount, {Type="Color", X=x, Y=y, BackgroundColor=color})
- sectionCount = sectionCount + 1
- sleep(0)
- end
- y = y + 1
- sLine = file.readLine()
- end
- file.close()
- libHLTI.SetValue("out.hlti", "Sections", sectionCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement