Clarity needed for PixelGetColor and CoordMode

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 11:18

Hi. I have the below code in a script I'm doing and I'm getting pretty confused, due to the "CoordMode" parameters. When I have:
CoordMode, Mouse, Client
in the auto execute section, the script returns an incorrect color and no matter if this color changes under the mouse, the same color is returned.
If I change that to:
CoordMode, Mouse, Window
then all works as expected, but this throws out the rest of my script, as I'm moving the mouse within the "Client" parameter.

If I do a test script, just to get the color under the mouse, I get 2 different results, depending if CoordMode is set to Window or Client:

CoordMode, Mouse, Client
the cursor is currently at 421, 379 and the color is 0x292929. correct coords, wrong color)
CoordMode, Mouse, Window
the cursor is currently at 428, 386 and the color is 0xE0E0E0. (wrong coords, correct color)

I know the coords are relative to which window, but I have to use the "Client" option for compatibility across the hosts in which I'm using the scripts.

I'm confused and would appreciate any clarity on how to proceed. Should CoordMode be changed within specific functions to accommodate the PixelGetColor?, while keeping the other in the auto execute section? or am I missing something pretty obvious.

Example code:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Recommended for catching common errors.
#UseHook
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, Mouse, Client
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 5
Global X, Y, Width, Height, Title, XPos, YPos, Color, UserTab
SetTitleMatchMode 2

; Automatic scan on start up in libraries/users tab
^+U::
If (IsKK() = 1) ; confirms the window
{
MsgBox, "Please open the preferences with control+comma."
Return
}
Else If (IsKKPreferences() = 1)
{
If (IsLibrariesTab() = 1) ; confirms the libraries tab is selected
{
If (UserTab = 0) ; a global to determine which tab is selected (user or factory)
{
MsgBox, Toggling automatic scan on start up in the Libraries/Factory tab is unavailable. You can only toggle this option in the Libraries/User tab."
Return
}
GetReferencePosition() ; places the mouse in the corner of the client window
Sleep, 100
MouseMove, 421, 379, , R ; places the mouse on the button in question
Sleep, 100
Click
MouseGetPos, XPos, YPos
PixelGetColor, Color, %XPos%, %YPos%
If (Color = 0xE0E0E0)
{
oSpVoice.Speak("AutoScan Off", 1)
Return
}
Else
{
oSpVoice.Speak("AutoScan On", 1)
Return
}
}
Else
{
MsgBox, "Press control+shift+6 for the Libraries/User tab in order to use this keystroke"
Return
}
}
Return

THX Steve.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 11:24

Code: Select all

CoordMode, Pixel, Client
Needs to be set to match mouse coords.

Give it a try
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 11:39

Thanks for the suggestion Xtra. Just tried that, but the same key takes the mouse an extra 7X7, which is the same as the Window parameter in CoordMode. It's as if you can't return a color correctly when set to client. Either that or something funky is going on :).
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 11:53

Try this test script:

Code: Select all

#Persistent
CoordMode, Pixel, Client
Coordmode, Mouse, Client
SetTimer, colorme, 250
return


colorme:
    MouseGetPos, posX, posY
    PixelGetColor, BGR, posX, posY         ; returns color in BGR
    PixelGetColor, RGB, posX, posY, RGB    ; returns color in RGB
    ToolTip % "Client Coords`n`nX= " . posX . "`nY= " . posY . "`nBGR= " . BGR "`nRGB= " . RGB
return
Edit added: #Persistent
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 15:19

Hey Xtra, that works a treat! and is correctly reporting the change of color at the correct coords, thanks so much! So, I guess from this code, you're able to put in 2 "CoordMode"'s, 1 for the mouse and 1 for the pixel? Also, would the "#Persistent" have any influence on the result? Thanks again for your help, really appreciated.
BTW, the color was still being reported as hex rather than RGB.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 15:44

Yes you can set all in one script the coordmodes for:
ToolTip: Affects ToolTip.
Pixel: Affects PixelGetColor, PixelSearch, and ImageSearch.
Mouse: Affects MouseGetPos, Click, and MouseMove/Click/Drag.
Caret: Affects the built-in variables A_CaretX and A_CaretY.
Menu: Affects the Menu Show command when coordinates are specified for it.
#Persistent was added to the test script to prevent it from closing because there is no gui or hotkeys etc that automatically make the script persistent.
If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes. This is useful in cases where a script contains timers and/or custom menu items but not hotkeys, hotstrings, or any use of OnMessage() or Gui.
Also note:
The returned value is in BGR hex or RGB hex it just not showing in its decimal form ie: 255 255 255

HTH
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Clarity needed for PixelGetColor and CoordMode

29 Jun 2017, 16:11

Thanks again for all that clarification. I think the docs and forum are excellent...and I've been able to overcome the few stumbling blocks I've had so far. It's a great tool and a great community!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Google [Bot], peter_ahk and 336 guests