LayZee

ComputerCraft Color Picker

Oct 7th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. local params = {...}
  2. local colorFile = params[1] .. ".col"
  3.  
  4. term.setBackgroundColor(colors.black)
  5. term.clear()
  6. term.setTextColor(colors.white)
  7.  
  8. local color = {}
  9. local palette = {
  10.   text = colors.black,
  11.   background = colors.black,
  12.   header = colors.black
  13. }
  14.  
  15. function fileExists(filename)
  16.   local file = fs.open(filename, "r")
  17.  
  18.   if file then
  19.     file.close()
  20.  
  21.     return true
  22.   end
  23.  
  24.   return false
  25. end
  26.  
  27. local function loadColors ()
  28.   local palette = {
  29.     text = colors.black,
  30.     background = colors.black,
  31.     header = colors.black
  32.   }
  33.  
  34.   local file = fs.open("/" .. colorFile, "r")
  35.  
  36.   if not file then
  37.     error("Could not open " .. colorFile)
  38.   end
  39.  
  40.   local index = 1
  41.  
  42.   repeat
  43.     line = file.readLine()
  44.    
  45.     if line then
  46.       if index == 1 then
  47.         palette.text = tonumber(line)
  48.       elseif index == 2 then
  49.         palette.background = tonumber(line)
  50.       elseif index == 3 then
  51.         palette.header = tonumber(line)
  52.       end
  53.  
  54.       index = index + 1
  55.     end
  56.   until line == nil
  57.  
  58.   file.close()
  59.  
  60.   return palette
  61. end
  62.  
  63. local function saveColors (palette)
  64.   local file = fs.open("/" .. colorFile, "w")
  65.  
  66.   file.writeLine(palette.text .. "")
  67.   file.writeLine(palette.background .. "")
  68.   file.writeLine(palette.header .. "")
  69.  
  70.   file.close()
  71. end
  72.  
  73. local function setColor (mouseButton, colorIndex)
  74.   if mouseButton == 1 then
  75.     palette.text = colorIndex
  76.   elseif mouseButton == 2 then
  77.     palette.background = colorIndex
  78.   elseif mouseButton == 3 then
  79.     palette.header = colorIndex
  80.   end
  81. end
  82.  
  83. local function colorsChosen ()
  84.   if not palette.header
  85.     or not palette.background        
  86.     or not palette.text
  87.   then
  88.     return false
  89.   end
  90.  
  91.   return true
  92. end
  93.  
  94. local function colorText (backgroundColor)
  95.   if backgroundColor == colors.white
  96.     or backgroundColor == colors.yellow
  97.   then
  98.     term.setTextColor(colors.black)
  99.   else
  100.     term.setTextColor(colors.white)
  101.   end
  102. end
  103.  
  104. local function displayColors (palette)
  105.   term.setCursorPos(10, #color + 1)
  106.  
  107.   term.setBackgroundColor(
  108.     palette.text
  109.   )
  110.  
  111.   colorText(palette.text)
  112.   term.write("Text")
  113.  
  114.   term.setCursorPos(10, #color + 2)
  115.  
  116.   term.setBackgroundColor(
  117.     palette.background
  118.   )
  119.  
  120.   colorText(palette.background)
  121.   term.write("Background")
  122.  
  123.   term.setCursorPos(10, #color + 3)
  124.  
  125.   term.setBackgroundColor(
  126.     palette.header
  127.   )
  128.  
  129.   colorText(palette.header)
  130.   term.write("Header")
  131. end
  132.  
  133. --Main program
  134. if not fileExists(colorFile) then
  135.   local file = fs.open(colorFile, "w")
  136.  
  137.   for i = 1, 3 do
  138.     file.writeLine(colors.black .. "")
  139.   end
  140.  
  141.   file.close()
  142. end
  143.  
  144. palette = loadColors()
  145.  
  146. local index = 1
  147.  
  148. for key, value in pairs(colors) do
  149.   term.setCursorPos(1, index)
  150.  
  151.   if type(value) ~= "function" then
  152.     color[index] = value
  153.    
  154.     if key == "white" or key == "yellow" then
  155.       term.setTextColor(colors.black)
  156.     else
  157.       term.setTextColor(colors.white)
  158.     end
  159.    
  160.     term.setBackgroundColor(value)
  161.    
  162.     term.write(key)
  163.    
  164.     index = index + 1
  165.   end
  166. end
  167.  
  168. repeat
  169.   displayColors(palette)
  170.  
  171.   local event, button, x, y
  172.     = os.pullEvent("mouse_click")
  173.  
  174.   if y <= #color then
  175.     setColor(button, color[y])
  176.   end
  177. until y > #color
  178.  
  179. saveColors(palette)
  180.  
  181. term.setTextColor(colors.white)
  182. term.setBackgroundColor(colors.black)
  183. term.clear()
  184. displayColors(palette)
  185. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment