Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- --* GCAPI *--
- --* https://pastebin.com/0uAaAcrW *--
- --* by: GravityCube *--
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- Changelog:
- 1.0.0 First release
- 1.1.0 Image displayer for monitors added and bugs in the economy fixed
- 1.2.0 Tracker of players and chatSpy added, a custom http.get was added and global peripheral.find.
- 1.3.0 New listener, chatEvents and local admins for selling programs!
- 1.4.0 Various bugs fixed (repited chatEvents, and rgb api changed). Added version changelog and getVersion()
- 1.5.0 table.contains added
- 1.5.1 ChatEvent Listener little fix
- 1.5.2 Split function modified (self) and getChar added
- 1.5.3 getCenter added and centered of monitors fixed
- 1.5.4 getCenter now supports number and string in the #2 argument
- 1.5.5 Added some default "URLs" to the images
- 1.5.6 URL Alias updated
- 1.5.7 Added password for unfiltered tracker (akaElite request)
- 1.6.0 New method for tracking and some minor modifications.
- 1.6.1 Some minor changes and bugs fixed
- 1.7.0 New method gcapi.customMonitorWrite()
- 1.7.1 Added table.isEmpty
- 1.7.2 New admin (Freecss).
- 1.8.0 New method printAvatar()
- 1.8.1 New function getTime added and seed for random numbers changed.
- 1.8.2 Added deepCopy function from http://lua-users.org/wiki/CopyTable
- 1.8.3 An issue with getCenter was solved.
- 1.8.4 The method getPlayersPos was deprecated but patched so the tracker could work
- 1.8.5 Added getStaffList and filter_list for getPosPlayers
- 1.8.6 Added toLower on filter_list
- 1.8.7 Migrate jake endpoint to cc.emx.cl
- --]]
- --------------------------------------------
- --> Version <--
- --------------------------------------------
- local debugAPI = false
- local version = "1.8.6"
- print("GCAPI Version: " .. version)
- function getVersion()
- return version
- end
- --------------------------------------------
- --> URL ALIAS <--
- --------------------------------------------
- local urlAlias = {
- ["emx"] = "http://jake.emx.cl/logo.jpg",
- ["cl"] = "https://i.imgur.com/hslZg4j.png",
- ["deadpool"] = "https://i.imgur.com/BXq9i6w.png"
- }
- --------------------------------------------
- --> Tools <--
- --------------------------------------------
- function getTime()
- --local response = http.get("http://jake.emx.cl/time.php")
- --if response then
- -- local millis = tonumber(response.readAll())
- -- if millis then
- -- return millis, true
- -- end
- --end
- return os.time(), false
- end
- local charToColor = {["0"]=0, ["1"]= 1,["2"]= 2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7, ["8"]=8, ["9"]=9, ["a"]=10 ,["b"]=11, ["c"]=12, ["d"]=13, ["e"]=14, ["f"]=15}
- function customMonitorWrite(mon, text)
- local xi, yi = mon.getCursorPos()
- local chars = {}
- for i = 1, string.len(text) do
- local c = text:sub(i,i)
- table.insert(chars, c)
- end
- --charToColor["g"] = mon.getBackgroundColor()
- local isNumber = false
- for i=1, #chars, 1 do
- local c = chars[i]
- if c == "&" and charToColor[chars[i+1]] then
- color = 2^chars[i+1]
- mon.setBackgroundColor(color)
- isNumber = true
- else
- if not isNumber then
- mon.write(c)
- end
- isNumber = false
- end
- end
- end
- function verifyVar(rawVar, varType)
- if type(rawVar) ~= varType then
- error( "bad argument (expected " .. varType .. ", got " .. type( rawVar ) .. ")", 2 )
- end
- end
- -- RANDOM STRING
- local charset = {}
- -- qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890
- for i = 48, 57 do table.insert(charset, string.char(i)) end
- for i = 65, 90 do table.insert(charset, string.char(i)) end
- for i = 97, 122 do table.insert(charset, string.char(i)) end
- --math.randomseed(getTime())
- function randomString(length)
- if length > 0 then
- return string.random(length - 1) .. charset[math.random(1, #charset)]
- else
- return ""
- end
- end
- string.random = randomString
- --END RANDOM STRING
- function getRandom(minValue, maxValue)
- return math.random(minValue, maxValue)
- end
- --peripheral.find() in Tekkit
- function findPeripheral(pType)
- local pList = {}
- for _,pName in pairs(peripheral.getNames()) do
- if peripheral.getType(pName) == pType then
- table.insert(pList, peripheral.wrap(pName))
- end
- end
- return unpack(pList)
- end
- peripheral.find = findPeripheral
- --Wipe table "t"
- function wipeTable(t)
- local listForDeletion = {}
- for k,v in pairs(t) do
- table.insert(listForDeletion, k)
- end
- for _,i in pairs(listForDeletion) do
- t[i] = nil
- end
- end
- table.wipe = wipeTable
- --Split string
- function split(self, delimiter)
- result = {};
- for match in (self..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- string.split = split
- string.getChar = function (self, nVar)
- if type(nVar) ~= "number" then
- error( "bad argument #2 (expected number, got " .. type( nVar ) .. ")")
- else
- return self:sub(nVar, nVar)
- end
- end
- table.isEmpty = function(self)
- for k,v in pairs(self) do
- return true
- end
- return false
- end
- table.contains = function (self, eVar)
- for k,v in pairs(self) do
- if v == eVar then
- return true
- end
- end
- return false
- end
- --Print to a file and term (message, dir(optional) )
- function printLog(line, dir)
- if line == nil then
- return nil
- end
- if dir == nil then
- dir = "/log"
- end
- line = line
- print(line)
- file = fs.open(dir,"a")
- file.writeLine(line)
- file.close()
- end
- --Are items equal? Ignoring quantity
- function equalItems(item1, item2)
- if item1 == nil or item2 == nil then
- return false
- end
- for k,v in pairs(item1) do
- if k ~= qty then
- if item2[k] ~= v then
- return false
- end
- end
- end
- return true
- end
- --Get number of lines file
- function getNumberOfLinesFile(filePath)
- if not fs.exists(filePath) then
- print("[GCAPI] File not found")
- return 0
- end
- file = fs.open(filePath, "r")
- if file then
- local i = 0
- while file.readLine() do
- i = i + 1
- end
- file.close()
- return i
- end
- return 0
- end
- function getListFromFile(filePath)
- local list = {}
- if not fs.exists(filePath) then
- saveListToFile(filePath, list)
- return list
- end
- local file = fs.open(filePath,"r")
- local data = file.readAll()
- file.close()
- list = textutils.unserialize(data)
- if textutils.unserialize(data) == nil then
- list = {}
- end
- return list
- end
- function saveListToFile(filePath, list)
- file = fs.open(filePath,"w")
- file.write(textutils.serialize(list))
- file.close()
- end
- string.starts = function (self,Start)
- return string.sub(self,1,string.len(Start))==Start
- end
- function getCenter(f, varP)
- if not varP then
- varP = 0
- end
- local lengthOfVar = 0
- if type(varP) == "number" then
- lengthOfVar = varP
- elseif type(varP) == "string" then
- lengthOfVar = string.len(varP)
- else
- error("For function gcapi.getCenter expected number or string in argument #2")
- end
- if ((f - string.len(varP)) % 2) == 0 then
- return math.floor((f - lengthOfVar)/2)
- end
- return math.floor((f - lengthOfVar)/2)+1
- end
- function deepCopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- copy[deepCopy(orig_key)] = deepCopy(orig_value)
- end
- setmetatable(copy, deepCopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- function numberBetween(v1, v2, v3)
- if v1 >= v2 and v1 <= v3 then
- return true
- end
- return false
- end
- --------------------------------------------
- --> Custom Http Request <--
- --> http.get with timeout variable <--
- --------------------------------------------
- function http.getStringWithTimeout(url, headers, timeout)
- if timeout == nil then
- local response = http.get(url, headers)
- local responseString = response.readAll()
- response.close()
- return responseString
- end
- --seconds to ticks
- timeout = timeout*20
- http.request(url, nil, headers)
- local requesting = true
- local localReloj = 0
- local nextTimeEventID = os.startTimer(0.1)
- while requesting do
- --Wait for event.
- tEvent = {os.pullEvent()}
- if "timer" == tEvent[1] then
- if tEvent[2] == nextTimeEventID then
- if timeout < localReloj then
- return nil
- end
- localReloj = localReloj + 2
- nextTimeEventID = os.startTimer(0.1)
- end
- else
- --Success.
- if tEvent[1] == "http_success" and url == tEvent[2] then
- local response = tEvent[3]
- local responseString = response.readAll()
- response.close()
- return responseString
- end
- --Failure
- if tEvent[1] == "http_failure" and url == tEvent[2] then
- return nil
- end
- nextTimeEventID = os.startTimer(0.1)
- end
- end
- end
- --------------------------------------------
- --> ImageDisplay <--
- --> <--
- --> IMPORTANT: <--
- --> The API server get bugged sometimes <--
- --> so I had to make a timeout http.get <--
- --------------------------------------------
- -- ** Display Image from URL **
- -- Example:
- -- url = http://www.image.com/image.jpg
- -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
- -- maxSize = 10 (10 max of height and weight) [Optional]
- -- NOTE: If you want the image to be send to only 1 player use
- -- peripheral.wrap(glassesName).getUserSurface(playerName)
- -- instead of peripheral.wrap(glassesName)
- function displayImageFromURL(url, glassesPeripheral, maxSize, xo, yo)
- local filePath, ok, err = getImagePathFromURL(url, maxSize)
- if not ok then
- return false, err
- end
- displayImageFromFile(filePath, glassesPeripheral, xo, yo)
- fs.delete(filePath)
- return true
- end
- -- ** Display Image from Rule34 **
- -- Example:
- -- tag = random (just 1)
- -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
- -- maxSize = 10 (10 max of height and weight) [Optional]
- -- NOTE: If you want the image to be send to only 1 player use
- -- peripheral.wrap(glassesName).getUserSurface(playerName)
- -- instead of peripheral.wrap(glassesName)
- function displayImageFromRule34(tag, glassesPeripheral, maxSize, xo, yo)
- url, err = getURLfromRule34(tag)
- if url == nil then
- return false, err
- end
- return displayImageFromURL(url, glassesPeripheral, maxSize, xo, yo)
- end
- function getGlasses(glassesPeripheral)
- if glassesPeripheral == nil then
- return peripheral.find("openperipheral_glassesbridge")
- end
- return glassesPeripheral
- end
- -- file = fs.open("imagen","r")
- -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
- function displayImageFromFile(filePath, glassesPeripheral, xo, yo)
- if xo == nil then
- xo = 1
- end
- if yo == nil then
- yo = 1
- end
- numberLines = getNumberOfLinesFile(filePath)
- file = fs.open(filePath, "r")
- gb = getGlasses(glassesPeripheral)
- if gb == nil then
- error("[GCAPI] Glasses peripheral not found")
- end
- for i=1,numberLines,1 do
- linea = file.readLine(i)
- lineaQ = {}
- lineaQ = split(linea,",")
- for x,color in pairs(lineaQ) do
- hcolor = assert(loadstring("return "..color))()
- gb.addBox(x+xo,i+yo,1,1,hcolor,1)
- end
- end
- file.close()
- end
- function getImagePathFromURL(url, maxSize, max_x, max_y, forMonitor, filePath)
- if filePath == nil then
- filePath = 'gcAPIImage'
- end
- if url == nil then
- err = '[GCAPI] No URL for image'
- print(err)
- return nil, false, err
- end
- --DELETE SAVED IMAGE
- if url == "none" then
- fs.delete(filePath)
- end
- --URL ALIAS
- if urlAlias[url] then
- url = urlAlias[url]
- end
- extraParameters = ""
- if maxSize ~= nil and tonumber(maxSize) then
- extraParameters = extraParameters .. '&max_size=' .. maxSize
- end
- if max_x ~= nil and tonumber(max_x) then
- extraParameters = extraParameters .. '&max_x=' .. max_x
- end
- if max_y ~= nil and tonumber(max_y) then
- extraParameters = extraParameters .. '&max_y=' .. max_y
- end
- script = "imageCC"
- if forMonitor then
- script = "monitorImageCC"
- end
- local dataWeb = http.getStringWithTimeout("http://cc.emx.cl/cc/" .. script .. ".php?url=" .. url .. extraParameters, nil, 3)
- if not dataWeb then
- err = "[GCAPI] Server error or timeout"
- print(err)
- return nil, false, err
- end
- if #dataWeb < 30 then
- err = "[GCAPI] " .. dataWeb
- print(err)
- return nil, false, err
- end
- fs.delete(filePath)
- f = fs.open(filePath, "w")
- f.write(dataWeb)
- f.close()
- return filePath, true, nil
- end
- --------------------------------------------
- --> ImageDisplay for <--
- --> monitors <--
- --------------------------------------------
- function getMonitor(monSelected)
- if monSelected == nil then
- return peripheral.find("monitor")
- end
- return monSelected
- end
- function printImageFromURL(url, monSelected, centered)
- mon = getMonitor(monSelected)
- if mon == nil then
- error("[GCAPI] Monitor not found")
- end
- mon.setTextScale(0.5)
- mon.clear()
- local max_x, max_y = mon.getSize()
- if url == nil then
- err = '[GCAPI] No URL for image'
- print(err)
- return nil, false, err
- end
- --URL ALIAS
- if urlAlias[url] then
- url = urlAlias[url]
- end
- extraParameters = ""
- if maxSize ~= nil and tonumber(maxSize) then
- extraParameters = extraParameters .. '&max_size=' .. maxSize
- end
- if max_x ~= nil and tonumber(max_x) then
- extraParameters = extraParameters .. '&max_x=' .. max_x
- end
- if max_y ~= nil and tonumber(max_y) then
- extraParameters = extraParameters .. '&max_y=' .. max_y
- end
- local dataWeb = http.getStringWithTimeout("https://cc.emx.cl/scripts/monitorImageCC.php?url=" .. url .. extraParameters, nil, 3)
- if not dataWeb then
- err = "[GCAPI] Server error or timeout"
- print(err)
- return nil, false, err
- end
- if #dataWeb < 30 then
- err = "[GCAPI] " .. dataWeb
- print(err)
- return nil, false, err
- end
- local image_lines = gcapi.split(dataWeb, '\n')
- mon.setTextScale(0.5)
- mon.clear()
- local max_x, max_y = mon.getSize()
- numberLines = #image_lines
- xo = 0
- yo = 0
- if centered and numberLines > 0 then
- local line = image_lines[1]
- local real_x = #(split(line, ","))
- xo = getCenter(max_x, real_x)
- yo = getCenter(max_y, numberLines)
- end
- for i=1,numberLines,1 do
- linea = image_lines[i]
- lineaQ = {}
- lineaQ = split(linea,",")
- for x,colorSet in pairs(lineaQ) do
- colors0 = split(colorSet,"-")
- r = tonumber(colors0[1])
- g = tonumber(colors0[2])
- b = tonumber(colors0[3])
- if r and g and b then
- hcolor = colors.fromRGB(r, g, b)
- mon.setBackgroundColour(hcolor)
- mon.setCursorPos(x+xo,i+yo)
- mon.write(" ")
- end
- end
- end
- return true
- end
- function printAvatar(mon, player_name, x, y, max_side)
- local xo = x
- local yo = y
- local filePath, ok, err = getImagePathFromURL("https://minotar.net/avatar/" .. player_name .. "/"..max_side, nil, max_side, max_side, true)
- if not ok then
- return false, err
- end
- numberLines = getNumberOfLinesFile(filePath)
- local file = fs.open(filePath, "r")
- for i=1,numberLines,1 do
- linea = file.readLine(i)
- lineaQ = {}
- lineaQ = split(linea,",")
- for x,colorSet in pairs(lineaQ) do
- colors0 = split(colorSet,"-")
- r = tonumber(colors0[1])
- g = tonumber(colors0[2])
- b = tonumber(colors0[3])
- if r and g and b then
- hcolor = colors.fromRGB(r, g, b)
- mon.setBackgroundColour(hcolor)
- mon.setCursorPos(x+xo,i+yo)
- mon.write(" ")
- end
- end
- end
- file.close()
- return true
- end
- function printImageFromFile(filePath, monSelected, centered, monSetup)
- mon = monSelected
- if monSetup == nil or monSetup == false then
- mon = getMonitor(monSelected)
- if mon == nil then
- error("[GCAPI] Monitor not found")
- end
- end
- mon.setTextScale(0.5)
- mon.clear()
- local max_x, max_y = mon.getSize()
- numberLines = getNumberOfLinesFile(filePath)
- xo = 0
- yo = 0
- if centered and numberLines > 0 then
- local file = fs.open(filePath, "r")
- local line = file.readLine()
- local real_x = #(split(line, ","))
- file.close()
- xo = getCenter(max_x, real_x)
- yo = getCenter(max_y, numberLines)
- end
- file = fs.open(filePath, "r")
- for i=1,numberLines,1 do
- linea = file.readLine()
- lineaQ = {}
- lineaQ = split(linea,",")
- for x,colorSet in pairs(lineaQ) do
- colors0 = split(colorSet,"-")
- r = tonumber(colors0[1])
- g = tonumber(colors0[2])
- b = tonumber(colors0[3])
- if r and g and b then
- hcolor = colors.fromRGB(r, g, b)
- mon.setBackgroundColour(hcolor)
- mon.setCursorPos(x+xo,i+yo)
- mon.write(" ")
- end
- end
- end
- file.close()
- return true
- end
- --------------------------------------------
- --> ImageDisplay for <--
- --> chunks of monitors <--
- --------------------------------------------
- function printPixel(x,y,color)
- end
- --------------------------------------------
- --> Tracker of players <--
- --> Just for craftersland <--
- --> DYNMAP <--
- --------------------------------------------
- function getPosPlayer(player, dataPlayers)
- if player == nil or player == "" then
- err = "User not found"
- print("[GCAPI] " .. err)
- return nil, false, err
- end
- player = string.lower(player)
- if not dataPlayers then
- dataPlayers, ok, err = getPosPlayers()
- end
- for i,dataPlayer in pairs(dataPlayers) do
- user = string.lower(dataPlayer["name"])
- if string.match(user, player) then
- return dataPlayer, true, nil
- end
- end
- err = "User not found"
- return nil, false, err
- end
- function getForumData()
- local dataWeb = http.get("https://forum.craftersland.net/staff/")
- if not dataWeb then return nil, false, "[GCAPI] Forum Error" end
- return dataWeb.readAll()
- end
- function getStaffList() -- lower case
- local result = {}
- staff = {"DragonSlayer","ZeeDerpMaster"}
- for i=1,#staff do
- if not table.contains(result, staff[i]) then
- table.insert(result, string.lower(staff[i]))
- end
- end
- return result, true, nil
- end
- function getDynmapData()
- local dataWeb = http.get("http://tekkit.craftersland.net:25800/up/world/world/")
- if not dataWeb then return nil, false, "[GCAPI] Dynmap Error" end
- return dataWeb.readAll(), true, nil
- end
- function getOnlinePlayers()
- local result = {}
- local dm_data, ok, err = getDynmapData()
- if not ok then
- return nil, false, err
- end
- local list = string.split(dm_data,"\"name\":\"")
- for i=2, #list do
- local player_name = string.split(list[i],"\"")[1]
- table.insert(result, player_name)
- end
- return result, true, nil
- end
- -- @Deprecated - It will return fake data - "name","x","y","z","world"
- function getPosPlayers(filter_list)
- local dataPlayers = {}
- local online_players, ok, err = getOnlinePlayers()
- if not ok then
- return nil, false, err
- end
- for _,player_name in pairs(online_players) do
- if not (filter_list and table.contains(filter_list, string.lower(player_name))) then
- table.insert(dataPlayers, {["name"] = player_name, ["x"] = 0, ["y"] = 0, ["z"] = 0, ["world"] = "Overworld"})
- end
- end
- return dataPlayers, true, nil
- end
- function getClosePlayers(user, range)
- local url = "http://cc.emx.cl/cc/closePlayers.php?user=" .. user .. "&range=" .. range
- local dataWeb = http.get(url)
- local closePlayers = textutils.unserialize(dataWeb.readAll())
- dataWeb.close()
- if not dataWeb then
- return nil, false, "[GCAPI] Server Error"
- end
- if closePlayers["error"] ~= nil then
- return nil, false, closePlayers["error"]
- end
- return closePlayers, true, nil
- end
- --------------------------------------------
- --> Chat tracker for <--
- --> Craftersland <--
- --------------------------------------------
- function getChat()
- dataWeb = http.get("http://cc.emx.cl/cc/chatSpy.php")
- dataChat = textutils.unserialize(dataWeb.readAll())
- dataWeb.close()
- if not dataWeb then
- return nil, false, "[GCAPI] Server Error"
- end
- return dataChat, true, nil
- end
- --------------------------------------------
- --> Economy <--
- --> <--
- --> This is saved every use <--
- --> for safety purposes. <--
- --------------------------------------------
- function getMoney(player)
- list = getListFromFile("/disk/economy")
- player = string.lower(player)
- for name,amount in pairs(list) do
- if name == player then
- return amount
- end
- end
- setMoney(player, 0)
- return 0
- end
- function setMoney(player, amount)
- player = string.lower(player)
- list = getListFromFile("/disk/economy")
- fs.delete("/disk/economy")
- list[player]=amount
- saveListToFile("/disk/economy", list)
- end
- function withdraw(player, amount)
- player = string.lower(player)
- if getMoney(player) < amount then
- return false
- end
- setMoney(player, (getMoney(player)-amount))
- return true
- end
- function addMoney(player, money)
- player = string.lower(player)
- setMoney(player,getMoney(player)+money)
- end
- --------------------------------------------
- --> Admins for selling <--
- --------------------------------------------
- admins = {"GravityCube", "Ubuntu_64bit", "Lancellot", "Freecss"}
- function hasPermissions(player, globalAdmins)
- if globalAdmins == nil then
- globalAdmins = true
- end
- for _,user in pairs(getAdmins(globalAdmins)) do
- if user == player then
- return true
- end
- end
- return false
- end
- function getAdmins(globalAdmins)
- localAdmins = {}
- if fs.exists("admins") then
- localAdmins = getListFromFile("admins")
- end
- if globalAdmins then
- for _,admin in pairs(admins) do
- table.insert(localAdmins, admin)
- end
- end
- return localAdmins
- end
- --------------------------------------------
- --> EventHanlder <--
- --------------------------------------------
- if not lastID then
- lastID = 0
- end
- function startChatEventQueue()
- while true do
- chatData, ok, err = getChat()
- if ok then
- lastIDTemp = lastID
- for i=1, 10, 1 do
- data = chatData[i]
- if data ~= nil then
- id = data["id"]
- if id > lastID then
- if id > lastIDTemp then
- lastIDTemp = id
- end
- player = data["name"]
- command = data["message"]
- if command ~= nil and string.starts(command, "!") then
- command = string.sub(command, 2)
- lastID = lastIDTemp
- os.queueEvent("chatEvent", player, command)
- end
- end
- end
- end
- lastID = lastIDTemp
- end
- sleep(1)
- end
- end
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- --* RBG TO COLOUR *--
- --* by CrazedProgrammer *--
- --* (Used in the printImageFromURL) *--
- --* https://pastebin.com/BCSWghjR/rgb *--
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- local hex = {"F0F0F0", "F2B233", "E57FD8", "99B2F2", "DEDE6C", "7FCC19", "F2B2CC", "4C4C4C", "999999", "4C99B2", "B266E5", "3366CC", "7F664C", "57A64E", "CC4C4C", "191919"}
- local rgb = {}
- for i=1,16,1 do
- rgb[i] = {tonumber(string.sub(hex[i],1, 2), 16), tonumber(string.sub(hex[i],3, 4), 16), tonumber(string.sub(hex[i], 5, 6), 16)}
- end
- colors.fromRGB = function (r, g, b)
- local dist = 1e100
- local d = 1e100
- local color = -1
- for i=1,16,1 do
- d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)
- if d < dist then
- dist = d
- color = i - 1
- end
- end
- return 2 ^ color
- end
- colours.fromRGB = colors.fromRGB
Add Comment
Please, Sign In to add comment