Detect the color of a pixel anywhere within the window

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
neo42
Posts: 83
Joined: Tue Mar 01, 2011 10:20 pm

Detect the color of a pixel anywhere within the window

Post by neo42 » Mon May 02, 2011 9:00 pm

I thought I could use a combination of "mousecolor" and "click at x,y" to determine the color of pixels anywhere in the window, but it seems to be driven by where the user has the mouse cursor.

Code: Select all

choose browse
click at x, y
answer the mousecolor

How do I detect the color of what has been drawn on screen?

For example, I've used "set the pencolor to black", "choose pencil" and "click at x,y" to do some code based painting but now I want the code to be able to know what the color of any given pixel on screen is.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Detect the color of a pixel anywhere within the window

Post by jmburnod » Mon May 02, 2011 9:19 pm

Hi,

Look at brushColor

You can try also

Code: Select all

on mousemove
   put the mousecolor
end mousemove
It get the current mousecolor only when the mouse is up

Jean-Marc
https://alternatic.ch

neo42
Posts: 83
Joined: Tue Mar 01, 2011 10:20 pm

Re: Detect the color of a pixel anywhere within the window

Post by neo42 » Mon May 02, 2011 9:36 pm

jmburnod wrote:Hi,

Look at brushColor

You can try also

Code: Select all

on mousemove
   put the mousecolor
end mousemove
It get the current mousecolor only when the mouse is up

Jean-Marc

Very confused.

Brushcolor appears to let you change the color you will paint/draw with. - that is not I'm asking about


Also, how would that code segment help? I need to be able to detect the color of a pixel, via code only, and I believe the mousecolor command won't help me at all because that's based on where the mouse cursor is.


Thanks though...

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Detect the color of a pixel anywhere within the window

Post by bn » Tue May 03, 2011 12:00 am

Hi Neo42,

the shurest thing is to use a snapshot. At least I dont know of anything else. I made a little stack that takes a snapshot of 1by1 pixel.

You read out the color RGB value by parsing the imagedata of an image.

Have a look at the script.

I saw your post over at the talking Livecode section. Dont worry, people will answer here as well.

Kind regards

Bernd
Attachments
ColorOfPixel.livecode.zip
(1.44 KiB) Downloaded 345 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Detect the color of a pixel anywhere within the window

Post by FourthWorld » Tue May 03, 2011 12:07 am

You can also set the screenMouseLoc to the specified location - just remember to set it back after you get the color:

Code: Select all

on mouseUp
  put PixelColor(500,500)
end mouseUp

function PixelColor x,y
  put the screenMouseLoc into tSaveLoc
  set the screenMouseLoc to x,y
  put the mouseColor into tColor
  set the screenMouseLoc to tSaveLoc
  return tColor
end PixelColor
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Detect the color of a pixel anywhere within the window

Post by bn » Tue May 03, 2011 12:15 am

Hi Richard,

that works very well. I thought of it but did not even try because I thought the cursor would jump around. But it is so fast that one does not notice.

Thanks, it is a lot easier than taking a snapshot etc.

Kind regards

Bernd

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Detect the color of a pixel anywhere within the window

Post by FourthWorld » Tue May 03, 2011 12:59 am

I avoided it for a long time myself for the same reason, until one day I was desperate enough to try it and was pleased with the result. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

neo42
Posts: 83
Joined: Tue Mar 01, 2011 10:20 pm

Re: Detect the color of a pixel anywhere within the window

Post by neo42 » Wed May 11, 2011 1:57 pm

I'll have to try some of these out. I ended up going a different route in that project but I will still have a use for this later. Thank you!

neo42
Posts: 83
Joined: Tue Mar 01, 2011 10:20 pm

Re: Detect the color of a pixel anywhere within the window

Post by neo42 » Wed May 11, 2011 2:03 pm

I'll have to try some of these out. I ended up going a different route in that project but I will still have a use for this later. Thank you!

Post Reply