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

Background Color Detector?

Started by wookiederk, 07 April 2013 - 11:28 AM
wookiederk #1
Posted 07 April 2013 - 01:28 PM
Hi I'm making a game where my character is loaded onto my image file. How can I detect the background color pixel of a spot? I need to know how to detect where the pixels of a spot are a specific color. Ex. if thisspot== backgroundcolorblue then etc. Been working on the code for a while and this is frustrating, any help ASAP appreciated.
Sammich Lord #2
Posted 07 April 2013 - 01:52 PM
Overwrite the term.setBackgroundColor and instead of automatically setting the background color save each point of the screen to a table with what color they were set to.
wookiederk #3
Posted 07 April 2013 - 02:03 PM
I don't know what you mean :/ example? Edit: I think I understand what your saying but I don't know how to make the image pixels into a table… or something along those lines.
SuicidalSTDz #4
Posted 07 April 2013 - 02:35 PM
You don't need to. Just re-write the term.setBackgroundColour function to save the args called with the function to a table:

The new function:
local colTable = {}
local function term.setBackgroundColour(colour)
 --some stuff
 colTable = {} --reset the table each time the function is called
 table.insert(colTable, {currentColour = colour})
end

Checking the current colour:
for _,v in pairs(colTable) do
 if v.currentColour ~= "colours.purple" then
  print("This is blasphemy!")
 else
  print("You prove yourself worthy to the Endermen")
 end
end

This code is untested and I am very tired from work. So, have fun!
Sammich Lord #5
Posted 07 April 2013 - 02:42 PM
You don't need to. Just re-write the term.setBackgroundColour function to save the args called with the function to a table:

The new function:
local colTable = {}
local function term.setBackgroundColour(colour)
--some stuff
colTable = {} --reset the table each time the function is called
table.insert(colTable, {currentColour = colour})
end

Checking the current colour:
for _,v in pairs(colTable) do
if v.currentColour ~= "colours.purple" then
  print("This is blasphemy!")
else
  print("You prove yourself worthy to the Endermen")
end
end

This code is untested and I am very tired from work. So, have fun!
That won't work and let me explain why. He is asking to be able to see the background color of a specific pixel, not the whole screen. I can change the color of the screen 50 times and have each pixel a specific background color. So your function will only give the current color that is set, not a table of every pixel on the whole screen.
SuicidalSTDz #6
Posted 07 April 2013 - 02:44 PM
Oh.. I never read carefully.. You would just need to insert the x and y pos of the pixel being coloured and the colour to a table. Then index the table when need be.
SadKingBilly #7
Posted 07 April 2013 - 03:28 PM
I'm not entirely sure that I even understand what you're asking, but can't you just check the clicked pixel against the image file? The image files are really simple, they just use hexadecimal to denote the sixteen (is it?) colors. So if the first character in an image file is 0, then the first pixel on the monitor is white. If the fourth character is f, then the fourth pixel is black (or whatever).
wookiederk #8
Posted 07 April 2013 - 04:43 PM
I'm not entirely sure that I even understand what you're asking, but can't you just check the clicked pixel against the image file? The image files are really simple, they just use hexadecimal to denote the sixteen (is it?) colors. So if the first character in an image file is 0, then the first pixel on the monitor is white. If the fourth character is f, then the fourth pixel is black (or whatever).
I need to know the background color of each pixel on the screen with a detection type of event. What I am going for basically say "P" was at coordinates (1,1). The X and Y being CurrentPosx, CurrentPosY. If the "P" goes to the coordinates whose pixel is blue, then print("On Blue") or when it reaches a red pixel print ("On Red"). Il try to work with the help you've given me. Thanks for the tips, if I still need more help after this Il let you know.
Kingdaro #9
Posted 07 April 2013 - 04:50 PM
I would go with just checking the pixel of the image too. You can access a pixel of an image using image[y][x].


if myImage[10][5] == colors.blue then
  print('Pixel at x5, y10 is blue')
end

Or something like that.
SuicidalSTDz #10
Posted 07 April 2013 - 05:19 PM
Yes, OP, listen to 'The Doctor'. He is all-knowing, omniscient, and well yeah.. No, but that is a great way of going by it. Just store the position of the pixels being coloured in a table, then you can compare it later using "The Doctor's" method :P/>