AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WaitPixelColor()
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Fri May 01, 2009 1:35 am    Post subject: WaitPixelColor() Reply with quote

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.
"

Antonio França
My stuff: Google Profile


Last edited by MasterFocus on Tue Jun 08, 2010 6:38 pm; edited 7 times in total
Back to top
View user's profile Send private message Visit poster's website
JulieXP



Joined: 24 Oct 2004
Posts: 93
Location: NYC

PostPosted: Wed May 06, 2009 5:09 am    Post subject: Reply with quote

aww your the best!
thanks for sharing with the rest of the community!
_________________
-Julie
Back to top
View user's profile Send private message
JulieXP



Joined: 24 Oct 2004
Posts: 93
Location: NYC

PostPosted: Wed May 06, 2009 11:47 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
JulieXP



Joined: 24 Oct 2004
Posts: 93
Location: NYC

PostPosted: Wed May 06, 2009 11:40 pm    Post subject: Reply with quote

never mind i got it, thanks tho
_________________
-Julie
Back to top
View user's profile Send private message
R3TR0



Joined: 26 Aug 2009
Posts: 38
Location: Canada

PostPosted: Wed Sep 02, 2009 8:52 pm    Post subject: Reply with quote

Hey very nice function, adding to my library Very Happy
Back to top
View user's profile Send private message
spazpunt



Joined: 12 Jul 2009
Posts: 71

PostPosted: Mon Sep 07, 2009 12:48 am    Post subject: Reply with quote

can this be made to work for a particular window such as desktop user picture???
Back to top
View user's profile Send private message Send e-mail
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Tue Sep 08, 2009 1:27 am    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
Ripperjack



Joined: 24 Aug 2009
Posts: 14

PostPosted: Sat Oct 03, 2009 9:54 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Rabiator



Joined: 17 Apr 2005
Posts: 289
Location: Sauerland

PostPosted: Sun Oct 04, 2009 8:26 am    Post subject: Reply with quote

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
}
Smile
__________________________________________
Created with BBCodeWriter 7.0 - the one and only Very Happy
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Sun Oct 04, 2009 4:45 pm    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
Scottieboy



Joined: 08 Jan 2010
Posts: 9

PostPosted: Fri Jan 08, 2010 12:07 pm    Post subject: Pixels change colour Reply with quote

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
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Fri Jan 15, 2010 6:47 am    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
Scottieboy



Joined: 08 Jan 2010
Posts: 9

PostPosted: Mon Feb 01, 2010 9:37 am    Post subject: Reply with quote

Thanks for the reply. I resolved this issue in the end by using Dameware instead of remote desktop.

Scott
Back to top
View user's profile Send private message
Jinan



Joined: 08 Mar 2010
Posts: 11

PostPosted: Thu Apr 22, 2010 3:46 pm    Post subject: Reply with quote

it does what i am looking for! million ths~!
Back to top
View user's profile Send private message
borgman



Joined: 17 Jun 2010
Posts: 70

PostPosted: Sat Jun 19, 2010 6:38 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group