Page 1 of 1

How to determine the color of a pixel?

Posted: Tue May 14, 2019 9:07 pm
by MagicBoy
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

Re: How to determine the color of a pixel?

Posted: Tue May 14, 2019 9:22 pm
by FourthWorld
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.

Re: How to determine the color of a pixel?

Posted: Tue May 14, 2019 10:21 pm
by MagicBoy
Wow, that was fast! Thanks, Richard!

I'll dig deeper into working with the imageData.


Geoff

Re: How to determine the color of a pixel?

Posted: Wed May 15, 2019 2:25 pm
by dunbarx
Hi.

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 mouseUp
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

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 1:45 pm
by thatkeith
dunbarx wrote:
Wed May 15, 2019 2:25 pm
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
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

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 3:13 pm
by dunbarx
Go for it in QCC.

Craig

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 4:43 pm
by jmburnod
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)
ComparePixelColorImagedataMousecolor.zip
(5.03 KiB) Downloaded 26 times
Best
Jean-Marc

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 5:01 pm
by thatkeith
Added to QCC: bug (not really a bug) 22212.

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 5:13 pm
by thatkeith
FourthWorld wrote:
Tue May 14, 2019 9:22 pm
I would imagine it's much faster to get the imageData and walk through it in a loop.
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:

Code: Select all

^^^^^^^^^^^^^^^^^^^^^^

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 6:38 pm
by [-hh]
[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].

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 getPixelRGB
In case you need also the alpha value A of that pixel:

Code: 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

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 6:41 pm
by jmburnod
Hi,
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:
Yes, imaged use ascii number of letters to define color
Please, have a look at the tack I posted above

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 7:15 pm
by bogs
thatkeith wrote:
Wed Jun 26, 2019 5:13 pm
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:

Code: Select all

^^^^^^^^^^^^^^^^^^^^^^
Best explanation I ever found is part of the SOT site here:
http://www.sonsothunder.com/devres/live ... mag003.htm

Re: How to determine the color of a pixel?

Posted: Wed Jun 26, 2019 7:45 pm
by thatkeith
Very helpful, both of you. Thanks! Time for some more pondering and testing... :)