How to determine the color of a pixel?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to determine the color of a pixel?
Hello, all!
I have taken up "book folding" which is the folding, measuring and cutting of the pages of a book to create a "relief" image in the pages when the book is set on it's end and opened to 90 degrees (if that made no sense whatsoever, search for "book folding" on Etsy for lots of examples).
Since I haven't found any good book-fold creation software on the Mac, I decided to write my own.
I wrote a stack that "reads" vertical slices of a black & white 1-bit graphic and lets me know the screen coordinates when the color changes (from white to black or from black to white).
Currently, I'm doing it by automating the mouse pointer to move down a single column of the graphic in teensy, weensy steps and logs the change into a field when the color change occurs. When it gets to the bottom of that column, it starts over again at the top in the next column and continues until it has covered the entire graphic (depending on the size of the graphic, this can take several hours to finish this process). It works fine but it's tediously slow.
What I would LOVE to do, to speed this up, is to have the "scanning" process happen WITHOUT having to move the mouse pointer. In other words, I'd like to quickly and easily know if the pixel at location x,y is "0,0,0" (black) or "255,255,255" (white).
Is there any way to do this without moving the mouse pointer to that location? I've scoured the LC user dictionary but this basic (to me) function seems to be missing.
Thanks for taking the time to read through this. Much appreciated.
Geoff
			
			
									
									
						I have taken up "book folding" which is the folding, measuring and cutting of the pages of a book to create a "relief" image in the pages when the book is set on it's end and opened to 90 degrees (if that made no sense whatsoever, search for "book folding" on Etsy for lots of examples).
Since I haven't found any good book-fold creation software on the Mac, I decided to write my own.
I wrote a stack that "reads" vertical slices of a black & white 1-bit graphic and lets me know the screen coordinates when the color changes (from white to black or from black to white).
Currently, I'm doing it by automating the mouse pointer to move down a single column of the graphic in teensy, weensy steps and logs the change into a field when the color change occurs. When it gets to the bottom of that column, it starts over again at the top in the next column and continues until it has covered the entire graphic (depending on the size of the graphic, this can take several hours to finish this process). It works fine but it's tediously slow.
What I would LOVE to do, to speed this up, is to have the "scanning" process happen WITHOUT having to move the mouse pointer. In other words, I'd like to quickly and easily know if the pixel at location x,y is "0,0,0" (black) or "255,255,255" (white).
Is there any way to do this without moving the mouse pointer to that location? I've scoured the LC user dictionary but this basic (to me) function seems to be missing.
Thanks for taking the time to read through this. Much appreciated.
Geoff
- 
				FourthWorld
 - VIP Livecode Opensource Backer

 - Posts: 10065
 - Joined: Sat Apr 08, 2006 7:05 am
 - Contact:
 
Re: How to determine the color of a pixel?
First, I didn't know what book folding was until your post - it's awesome! thanks.
https://www.etsy.com/market/book_folding
Changing the mouseLoc is pretty fast for one-offs, but if you need to check every pixel in an entire image I would imagine it's much faster to get the imageData and walk through it in a loop.
			
			
									
									https://www.etsy.com/market/book_folding
Changing the mouseLoc is pretty fast for one-offs, but if you need to check every pixel in an entire image I would imagine it's much faster to get the imageData and walk through it in a loop.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
						LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to determine the color of a pixel?
Wow, that was fast! Thanks, Richard!
I'll dig deeper into working with the imageData.
Geoff
			
			
									
									
						I'll dig deeper into working with the imageData.
Geoff
Re: How to determine the color of a pixel?
Hi.
Did you do something like this:
That would indeed take a while. Just like writing to a field, the fact that LC has to interact with the screen takes a whole lot of time.
I have always wanted to have a function "the pixelColor", that would not require the actual cursor to be placed somewhere, only to specify a loc. Maybe we can propose an enhancement request.
Craig Newman
			
			
									
									
						Did you do something like this:
Code: Select all
on mouseUp
   put the botRight of this cd into lastPixel
   
   repeat with x = 1 to item 1 of lastPixel
      repeat with y = 1 to item 2 of lastPixel
         set the screenMouseloc to x & "," & y
         put mouseColor() & return after colorData
      end repeat
   end repeat
end mouseUpI have always wanted to have a function "the pixelColor", that would not require the actual cursor to be placed somewhere, only to specify a loc. Maybe we can propose an enhancement request.
Craig Newman
Re: How to determine the color of a pixel?
That would indeed be great! I've just scoured the LC dictionary for something like that and ended up searching here. I'm actually a little surprised that this isn't already part of the language. Something like the pixelColor of [image pixel coordinate] or [screen loc coordinate] to query the RGB value of a pixel from screen or within an image would be extremely useful.
k
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
						University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
Re: How to determine the color of a pixel?
Go for it in QCC.
Craig
			
			
									
									
						Craig
Re: How to determine the color of a pixel?
Hi All,
I see we sometimes get differents results if we use imagedata or mouseColor as you may see on this stack. (LC 9.01) Best
Jean-Marc
			
			
													I see we sometimes get differents results if we use imagedata or mouseColor as you may see on this stack. (LC 9.01) Best
Jean-Marc
					Last edited by jmburnod on Wed Jun 26, 2019 6:36 pm, edited 1 time in total.
									
			
									https://alternatic.ch
						Re: How to determine the color of a pixel?
Added to QCC: bug (not really a bug) 22212.
			
			
									
									Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
						University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
Re: How to determine the color of a pixel?
I feel silly asking, but any pointers on getting this data out in a recognisable form? If I put it into a field it just displays a long single string like so:FourthWorld wrote: ↑Tue May 14, 2019 9:22 pmI would imagine it's much faster to get the imageData and walk through it in a loop.
Code: Select all
^^^^^^^^^^^^^^^^^^^^^^Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
						University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
Re: How to determine the color of a pixel?
[Edit: Saw too late that my first function is essentially Jean-Marc's from his stack, sorry].
This returns the (R,G,B)-value of a single pixel (x,y) of an image.
[1<= col x <= the width of the image, 1<= row y <= the height of the image].
In case you need also the alpha value A of that pixel:
			
			
													This returns the (R,G,B)-value of a single pixel (x,y) of an image.
[1<= col x <= the width of the image, 1<= row y <= the height of the image].
Code: Select all
-- pImg is the name of the img,
-- x is the col of the pixel, y the row of the pixel
function getPixelRGB pImg,x,y
  put the width of img pImg into w
  put 4*((y-1)*w+x) into k
  put byte k-2 to k of the imageData of img pImg into b
  return (byteToNum(byte 1 of b),byteToNum(byte 2 of b), byteToNum(byte 3 of b))
end getPixelRGBCode: Select all
function getPixelRGBA pImg,x,y -- return R,G,B,A
  put the width of img pImg into w
  put 4*((y-1)*w+x) into k
  put byte k-2 to k of the imageData of img pImg into b
  put byteToNum(byte (y-1)*w+x of the alphaData of img pImg) into c
  return (byteToNum(byte 1 of b),byteToNum(byte 2 of b), byteToNum(byte 3 of b), c) 
end getPixelRGBA
					Last edited by [-hh] on Wed Jun 26, 2019 6:47 pm, edited 1 time in total.
									
			
									shiftLock happens
						Re: How to determine the color of a pixel?
Hi,
Please, have a look at the tack I posted above
			
			
									
									Yes, imaged use ascii number of letters to define colorI feel silly asking, but any pointers on getting this data out in a recognisable form? If I put it into a field it just displays a long single string like so:
Please, have a look at the tack I posted above
https://alternatic.ch
						Re: How to determine the color of a pixel?
Best explanation I ever found is part of the SOT site here:thatkeith wrote: ↑Wed Jun 26, 2019 5:13 pmI feel silly asking, but any pointers on getting this data out in a recognisable form? If I put it into a field it just displays a long single string like so:Code: Select all
^^^^^^^^^^^^^^^^^^^^^^
http://www.sonsothunder.com/devres/live ... mag003.htm

Re: How to determine the color of a pixel?
Very helpful, both of you. Thanks! Time for some more pondering and testing... 
			
			
									
									Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist
						University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist