AutoHotkey Community

It is currently May 27th, 2012, 4:59 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: WaitPixelColor()
PostPosted: May 1st, 2009, 2:35 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Useful function I develop once to fulfill my needs.

+ 19/july
- Fixed a logical mistake

USAGE + FUNCTION (download)
Code:
;========================================================================
;
; Function:     WaitPixelColor
; Description:  Waits until pixel is a certain color (w/ optional timeout)
; Online Ref.:  http://www.autohotkey.com/forum/topic43807.html
;
; Last Update:  19/July/2009 04:30
;
; Created by:   MasterFocus
;               http://www.autohotkey.net/~MasterFocus/AHK/
;
;========================================================================
;
; p_DesiredColor, p_PosX, p_PosY [, p_TimeOut, p_GetMode, p_ReturnColor]
;
; + Required parameters:
; - p_DesiredColor      The color you are waiting for
; - p_PosX, p_PosY      Pixel coordinates
;
; + Optional parameters:
; - p_TimeOut           Timeout in milliseconds (default is 0, no timeout)
; - p_GetMode           PixelGetColor mode(s) (default is blank)
; - p_ReturnColor       Boolean, see returned values below (deafult is 0)
;
; + Returned values when ReturnColor is 0 (false):
; - 0      The desired color was found
; - 1      There was a problem during PixelGetColor
; - 2      The function timed out
;
; + Returned values when ReturnColor is 1 (true):
; - Blank        There was a problem during PixelGetColor
; - Non-blank    Will be the latest found color, even if not the desired one
;
;========================================================================

WaitPixelColor(p_DesiredColor,p_PosX,p_PosY,p_TimeOut=0,p_GetMode="",p_ReturnColor=0)
{
    l_Start := A_TickCount
    Loop
    {
        PixelGetColor, l_RetrievedColor, %p_PosX%, %p_PosY%, %p_GetMode%
        If ErrorLevel
        {
            If !p_ReturnColor
                Return 1
            Break
        }
        If ( l_RetrievedColor = p_DesiredColor )
        {
            If !p_ReturnColor
                Return 0
            Break
        }
        If ( p_TimeOut ) && ( A_TickCount - l_Start >= p_TimeOut )
        {
            If !p_ReturnColor
                Return 2
            Break
        }
    }
    Return l_RetrievedColor
}


EXAMPLE (download)
Code:
#Include WaitPixelColor.ahk

CoordMode, Pixel, Screen

MsgBox % "This example will wait for pixel at 0,0 to be black."

MsgBox % WaitPixelColor(0,0,0)

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Last edited by MasterFocus on June 8th, 2010, 7:38 pm, edited 7 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 6th, 2009, 6:09 am 
Offline

Joined: October 24th, 2004, 9:32 pm
Posts: 93
Location: NYC
aww your the best!
thanks for sharing with the rest of the community!

_________________
-Julie


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 6th, 2009, 12:47 pm 
Offline

Joined: October 24th, 2004, 9:32 pm
Posts: 93
Location: NYC
What im i doing wrong? i cant figure it out

Quote:
WaitPixelColor(0xBC6A23,458,29,GetMode="",ReturnMode=0,SleepTime=1,TimeoutMult=0)
{
Loop
{
Sleep, %SleepTime%
PixelGetColor, RetrievedColor, %PosX%, %PosY%, %GetMode%
if ErrorLevel
{
if !ReturnMode
MsgBox, Error
break
}
if RetrievedColor = %DesiredColor%
{
if !ReturnMode
MsgBox, got the color
break
}
if A_Index = %TimeoutMult%
{
if !ReturnMode
MsgBox, timed out
break
}
}
return RetrievedColor
}

_________________
-Julie


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2009, 12:40 am 
Offline

Joined: October 24th, 2004, 9:32 pm
Posts: 93
Location: NYC
never mind i got it, thanks tho

_________________
-Julie


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2009, 9:52 pm 
Offline

Joined: August 26th, 2009, 11:15 pm
Posts: 38
Location: Canada
Hey very nice function, adding to my library :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 7th, 2009, 1:48 am 
Offline

Joined: July 12th, 2009, 9:52 pm
Posts: 71
can this be made to work for a particular window such as desktop user picture???


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2009, 2:27 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
You may add WinActivate before the PixelGetColor command for a quick work-around.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2009, 10:54 pm 
Offline

Joined: August 24th, 2009, 4:53 pm
Posts: 14
I want to do something simple using this script.

I want to look at a coordinate of 200,700 for the color 0. And tell me what it found, so:

Code:
#Include WaitPixelColor.ahk

^a::

WaitPixelColor(0,225,700,10000)
If errorlevel = 0
(
Msgbox Found it.
Return
)
Else if errorlevel = 1
(
Msgbox Error
return
)
Else Errorlevel = 2
(
Msgbox Timed out.
Return
)


Or am I just completely misunderstanding this function?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 4th, 2009, 9:26 am 
Offline

Joined: April 17th, 2005, 7:47 pm
Posts: 289
Location: Sauerland
At a first glance:
Code:
If errorlevel = 0
{
Msgbox Found it.
Return
}
Else if errorlevel = 1
{
Msgbox Error
return
}
Else if Errorlevel = 2
{
Msgbox Timed out.
Return
}
:)
__________________________________________
Created with BBCodeWriter 7.0 - the one and only :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 4th, 2009, 5:45 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Ripperjack wrote:
Or am I just completely misunderstanding this function?

Yes, because that's not the way you call a function.
You should use the returned value instead of ErrorLevel, as I specified:
MasterFocus wrote:
; + Return values when ReturnMode is 0:
; - 0 The desired color was found
; - 1 There was a problem during PixelGetColor
; - 2 The function timed out

Read more about about Functions and Blocks.
Here's what your code should look like:
Code:
#Include WaitPixelColor.ahk
Return

^a::
  var := WaitPixelColor(0,225,700,10000)
  If ( var = 0 )
    Msgbox Found it.
  Else If ( var = 1 )
    Msgbox Error!
  Else If ( var = 2 )
    MsgBox Timed out.
Return

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Pixels change colour
PostPosted: January 8th, 2010, 1:07 pm 
Offline

Joined: January 8th, 2010, 12:41 pm
Posts: 9
Masterfocus, Thanks very much for this great little script. It has helped me automate some procedures to make life easier in running monotonous routines.

I do have an issue though. The script works fine when I run locally. However, for some reason when I connect to a machine to run remotely through citrix/Remote desktop, the pixel color is different. I believe this to be because the desktop runs in 32 bit colours locally, whereas remote desktop will only function in 16bit.

Is there a way of getting this script to work by including a wider pallet of colours? I mean, if the colour is within a certain amount of shades it will work?

I appreciate this is my first post, but I do look through this forum and find my answers instead of posting without research like some!

Thanks in advance,
Scott


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 7:47 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Sorry, but I'm far from being the correct person to talk about these Remote Desktop and GetPixelColor coding stuff.
Try creating a topic or visiting the IRC channel.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2010, 10:37 am 
Offline

Joined: January 8th, 2010, 12:41 pm
Posts: 9
Thanks for the reply. I resolved this issue in the end by using Dameware instead of remote desktop.

Scott


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 22nd, 2010, 4:46 pm 
Offline

Joined: March 8th, 2010, 3:55 pm
Posts: 11
it does what i am looking for! million ths~!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2010, 7:38 pm 
Offline

Joined: June 17th, 2010, 2:42 pm
Posts: 73
Might be a silly question but how do add something like this to my library, i.e. where do I save your download code in a file such that in future scripts I can simply begin with #Include WaitPixelColor.ahk ?

Many thanks


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group