Advertisement
CrazedProgrammer

chpal

Jul 6th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. --[[
  2. MIT License
  3.  
  4. Copyright (c) 2017 CrazedProgrammer
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. ]]
  24.  
  25. local args, pal = {...}, { }
  26. local palmap = { 32768, 4096, 8192, 2, 2048, 1024, 512, 256,
  27.                  128, 16384, 32, 16, 8, 4, 64, 1 }
  28.  
  29. if not args[1] then
  30.     print("chpal <path>\nchpal --export <path>\nchpal --reset")
  31.     return
  32.  
  33. elseif args[1] == "--export" then
  34.     local path = shell.resolve(args[2])
  35.     local handle = fs.open(path, "w")
  36.     if not handle then
  37.         printError("Failed to write to file \""..path.."\".")
  38.         return
  39.     end
  40.     for i = 1, 16 do
  41.         local palr, palg, palb = term.getPaletteColor(palmap[i])
  42.         handle.writeLine("*.color"..(i - 1)..": #"..
  43.                          string.format("%02X%02X%02X", palr * 255, palg * 255, palb * 255))
  44.     end
  45.     handle.close()
  46.     return
  47.  
  48. elseif args[1] == "--reset" then
  49.     pal = { "#191919", "#7F664C", "#57A64E", "#F2B233", "#3366CC", "#B266E5", "#4C99B2", "#999999",
  50.             "#4C4C4C", "#CC4C4C", "#7FCC19", "#DEDE6C", "#99B2F2", "#E57FD8", "#F2B2CC", "#F0F0F0" }
  51.  
  52. else
  53.     local path = shell.resolve(args[1])
  54.     local handle = fs.open(path, "r")
  55.     if not handle then
  56.         printError("Palette file \""..path.."\" not found.")
  57.         return
  58.     end
  59.     local line = handle.readLine()
  60.     while line do
  61.         line = line:gsub("^%s*(.-)%s*$", "%1") -- trim
  62.         if line:find("%*%.color%d%d*: ") then
  63.             index = tonumber(line:sub(8, (line:sub(9, 9) == ":") and 8 or 9)) + 1
  64.             colour = line:sub(-7)
  65.             pal[index] = colour
  66.         end
  67.         line = handle.readLine()
  68.     end
  69.     handle.close()
  70. end
  71.  
  72. for i = 1, 16 do
  73.     term.setPaletteColour(palmap[i], tonumber(pal[i]:sub(2), 16))
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement