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

Get background colour

Started by darkhenmore, 17 June 2013 - 04:10 PM
darkhenmore #1
Posted 17 June 2013 - 06:10 PM
Hi,
Im writing a program in which i need to get the current background colour. I know its probably simple but i cannot find out how to do it.
Thanks
MysticT #2
Posted 17 June 2013 - 06:52 PM
You can't. There's no function to get the colors in the screen. However, you could keep track of the colors before writing them to the screen, so you can get it back later. The implementation would depend on your needs, so if you want some help with that, tell us what you're trying to do and we'll do our best to help.
Bomb Bloke #3
Posted 17 June 2013 - 06:57 PM
I don't see a command that offers this function. Best I can suggest is to track it yourself (eg, create a background-setting function that records whatever it's being changed to into a variable you can check up on later), but that won't allow you to see what colour the background was before your program started.

Edit: Whoops, ninja'd. That's what I get for opening too many tabs.
darkhenmore #4
Posted 17 June 2013 - 06:57 PM
ok, thanks! ill just keep track of it. not hard just slightly inconvenient
apemanzilla #5
Posted 19 June 2013 - 04:52 PM
I would reccommend a custom function that switches the color and saves it:

function changeBackgroundColor( color )
  currentbgcolor = color
  term.setBackgroundColor(color)
end
And to get the color (as a number at least) just use currentbgcolor.
H4X0RZ #6
Posted 19 June 2013 - 05:05 PM
Use something like this:

local oldBgSet = term.setBackgroundColor
local colorStack = {}
term.setBackgroundColor = function(col, tStack)
if tStack then
table.insert(tStack, col)
end
oldBgSet(col)
end

--Use it so
term.setBackgroundColor(colors.white, colorStack)
print(colorStack[#colorStack]) --to get the last colorcode

--At the end of your program
term.setBackgroundColor = oldBgSet
Lyqyd #7
Posted 19 June 2013 - 05:58 PM
Please ignore the above two posters. Keeping track of the order you used colors in wont help you determine what the color is at a given point on the screen.

You would instead want to hook into term.setBackgroundColor (so you know which color you are currently using), term.setCursorPos (so you know where the color is going) and term.write (so you know how many characters to make that color). You'll want to use these three methods to change values in a table so that you can look up the color that's on the screen at any given set of coordinates.