This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
vyse's profile picture

Choose background color

Started by vyse, 06 October 2014 - 04:40 PM
vyse #1
Posted 06 October 2014 - 06:40 PM
I am helping my friend with a program and we are wondering how you could make it were when you start the program you can select the background color.
KingofGamesYami #2
Posted 06 October 2014 - 07:38 PM
Well, you could do this

local color
repeat --#start a loop
  print( "choose a color" ) --#ask for a color
  color = read() --#get the color
until colors[ color ] --#if it is a valid color, exit the loop
term.setBackgroundColor( colors[ color ] ) --#set the background color
YoYoYonnY #3
Posted 06 October 2014 - 10:37 PM
Lets see how I'll do on my mobile phone
Original Version (Almost worked aswell :D/>)

local function pickColor()
  local w, h = term.getSize()
  local left = math.floor(w/2)-2
  local top = math.ceil(h/2)-2
  local color = colors.white
  local colorPalet = window.new(term.current(),left-5,top,4,4,true)
  local colorPreview = window.new(term.current(),left+1,top,4,4,true)
  while true do
	term.setBackgroundColor(colors.white)
	term.clear()
	term.setCursorPos(1,1)
	term.setBackgroundColor(colors.red)
	term.setTextColor(colors.white)
	term.clearLine()
	term.setCursorPos(math.floor((w-12)/2),11)
	term.write("Pick a color")
	for x=1,4 do
	  for y=1,4 do
		colorPalet.setCursorPos(x,y)
		colorPalet.setBackgroundColor(2^((x-1)+(y-1)*4))
		colorPalet.write(" ")
	  end
	end
	colorPreview.setBackgroundColor(color)
	colorPreview.clear()
	term.setBackgroundColor(colors.red)
	term.setTextColor(colors.white)
	term.setCursorPos(left,top+6)
	term.write(" ok ")
	sEvent, param1, param2, param3 = os.pullEvent()
	if sEvent == "mouse_click" and param1 == 1 then
	  if param2 >= left-5 and param2 < left-1 then
		if param3 >= top and param3 < top+4 then
		  color = 2^(param2-left)+(param3-top)*4
		end
	  end
	  if param2 >= left and param2 < left+4 and param3 == top+6 then
		return color
	  end
	end
  end
end
Working version:

local function pickColor()
  local w, h = term.getSize()
  local left = math.floor(w/2)-2
  local top = math.ceil(h/2)-2
  local color = colors.white
  local colorPalet = window.create(term.current(),left-5,top,4,4,true)
  local colorPreview = window.create(term.current(),left+1,top,4,4,true)
  while true do
	term.setBackgroundColor(colors.white)
	term.clear()
	term.setCursorPos(1,1)
	term.setBackgroundColor(colors.red)
	term.setTextColor(colors.white)
	term.clearLine()
	term.setCursorPos(math.floor((w-12)/2),1)
	term.write("Pick a color")
	for x=1,4 do
	  for y=1,4 do
		colorPalet.setCursorPos(x,y)
		colorPalet.setBackgroundColor(2^((x-1)+(y-1)*4))
		colorPalet.write(" ")
	  end
	end
	colorPreview.setBackgroundColor(color)
	colorPreview.clear()
	term.setBackgroundColor(colors.red)
	term.setTextColor(colors.white)
	term.setCursorPos(left,top+6)
	term.write(" ok ")
	sEvent, param1, param2, param3 = os.pullEvent()
	if sEvent == "mouse_click" and param1 == 1 then
	  if param2 >= left-5 and param2 < left-1 then
		if param3 >= top and param3 < top+4 then
		  color = 2^((param2-left-7)+(param3-top+3)*4)
		end
	  end
	  if param2 >= left and param2 < left+4 and param3 == top+6 then
		return color
	  end
	end
  end
end
Lets hope this works. I might check tomorrow.
Edited on 07 October 2014 - 01:51 PM