How to determine the color of a pixel?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
MagicBoy
Posts: 3
Joined: Wed Feb 06, 2019 12:35 am

How to determine the color of a pixel?

Post by MagicBoy » Tue May 14, 2019 9:07 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6836
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to determine the color of a pixel?

Post by FourthWorld » Tue May 14, 2019 9:22 pm

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

MagicBoy
Posts: 3
Joined: Wed Feb 06, 2019 12:35 am

Re: How to determine the color of a pixel?

Post by MagicBoy » Tue May 14, 2019 10:21 pm

Wow, that was fast! Thanks, Richard!

I'll dig deeper into working with the imageData.


Geoff

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6170
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to determine the color of a pixel?

Post by dunbarx » Wed May 15, 2019 2:25 pm

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

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 257
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: How to determine the color of a pixel?

Post by thatkeith » Wed Jun 26, 2019 1:45 pm

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
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6170
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to determine the color of a pixel?

Post by dunbarx » Wed Jun 26, 2019 3:13 pm

Go for it in QCC.

Craig

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2376
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: How to determine the color of a pixel?

Post by jmburnod » Wed Jun 26, 2019 4:43 pm

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 20 times
Best
Jean-Marc
Last edited by jmburnod on Wed Jun 26, 2019 6:36 pm, edited 1 time in total.

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 257
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: How to determine the color of a pixel?

Post by thatkeith » Wed Jun 26, 2019 5:01 pm

Added to QCC: bug (not really a bug) 22212.
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 257
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: How to determine the color of a pixel?

Post by thatkeith » Wed Jun 26, 2019 5:13 pm

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

^^^^^^^^^^^^^^^^^^^^^^
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2122
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to determine the color of a pixel?

Post by [-hh] » Wed Jun 26, 2019 6:38 pm

[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
Last edited by [-hh] on Wed Jun 26, 2019 6:47 pm, edited 1 time in total.
shiftLock happens

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2376
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: How to determine the color of a pixel?

Post by jmburnod » Wed Jun 26, 2019 6:41 pm

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

bogs
Posts: 3802
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to determine the color of a pixel?

Post by bogs » Wed Jun 26, 2019 7:15 pm

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
Image

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 257
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: How to determine the color of a pixel?

Post by thatkeith » Wed Jun 26, 2019 7:45 pm

Very helpful, both of you. Thanks! Time for some more pondering and testing... :)
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”