regionGetColor [v3.8]

Author: Infogulch, Titan Last Modified: nonexistent


RegionGetColor() function gets the average color of a portion of the screen. RegionWaitColor() function waits either until the average color of a region matches a value, or until the region's current color changes. Included are some generally useful color functions such as InvertColor() and withinVariation().

Using the AvgBitmap() func, you can get the average color of any bitmap in memory.

regionGetColor(x, y, w, h, hwnd=0)
regionGetColor_AvgBitmap(hbmp, pc)
regionGetColor_BitBlt( hdc_dest, x1, y1, w1, h1 , hdc_source, x2, y2 , mode )
regionGetColor_CreateCompatibleBitmap(hdc, w, h)
regionGetColor_CreateCompatibleDC(hdc=0)
regionGetColor_DeleteDC(hdc)
regionGetColor_DeleteObject(hbmp)
regionGetColor_GetDC(hwnd=0)
regionGetColor_invertColor(x, a = "")
regionGetColor_PrintWindow(hwnd, hdc, Flags=0)
regionGetColor_regionCompareColor(color, x, y, w, h, hwnd=0, a="", b="", c="")
regionGetColor_regionWaitColor(color, X, Y, W, H, hwnd=0, interval=100, timeout=5000, a="", b="", c="")
regionGetColor_ReleaseDC(hwnd, hdc)
regionGetColor_SelectObject(hdc, hgdiobj)
regionGetColor_SumIntBytes(bits, pc, ca, cr, cg, cb)
regionGetColor_Variation( x, y )
regionGetColor_withinVariation( x, y, a, b="", c="")

For more details of the functions's parameters and return value, please see it's source code.

Remarks

I added the prefix regionGetColor_ on all functions, except on regionGetColor() itself to make the library stdlibconform.

Infogulch is the author of this function, but borrowed some of Titans codes plus it uses MCode.

For update's details and remarks related to the functions, please see the AutoHotkey Forum: http://www.autohotkey.com/forum/topic36394.html

License

The functions is an open source item under the infogulch`s license license. For details, please see infogulch-license.txt

Example

; Tuncay: I made small changes only.
; Thanks to tic for most of the demonstration script.
; #include regionGetColor.ahk
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;region ;AutoExec; #######################################################################
#SingleInstance, Force
CoordMode, Mouse, Screen
MsgBox Press Alt+LeftMouseButton for selecting new area.`n`nPress Alt+r for rechecking that area and refreshing the gui.
RegionMain:
If !regionInit
{
   OnExit, Exit
   Gui, 1:+AlwaysOnTop +ToolWindow
   Gui, 1:Color, 0xffffff
   Gui, 1:Add, Edit, vGuiTextVar +ReadOnly h160 w180, Color: 0xffffff`nCount: `nTime: `n`n`n`n`n`n`n
   Gui, 1:Show, , regionColor
   Gui, 2:Color, 0xCCCCCC
   Gui, 2:+ToolWindow -Caption +Border +AlwaysOnTop +0x20 ; 0x20=click-thru
   Gui, 2:Add, Text, vGuiTextVar2 w80
   Gui, 2:+LastFound
   2GuiID := WinExist()
   Gui, 2:Show, X-2000 Y-2000 W1 H1
   WinSet, Trans, 150, ahk_id %2GuiID%
;   CoordMode, Mouse, Screen
   Process, Priority,, High
   SetBatchLines, -1
   SetWinDelay, -1
   RegionInit = 1
   GuiX := GuiY := 0
   GuiW := GuiH := 100
}
Gui, 1:Show
return
;end_region

;region ;Labels and Hotkeys; #############################################################
Esc::
Exit:
GuiClose:
   ExitApp

!LButton::
; use gui 2 to create a rectangle for area selection
   If !RegionInit
      GoSub RegionMain
   MouseGetPos, s_MSX, s_MSY, s_ID, s_CID, 2 ;start mouse X and Y
   WinSet, AlwaysOnTop, On, ahk_id %2GuiID%
   Loop
   {
      Sleep 20
      If !GetKeyState("LButton", "P")                  ;break if user releases the mouse
         Break   
      MouseGetPos, c_MSX, c_MSY                     ;current mouse X and Y
      GuiX := (s_MSX < c_MSX ? s_MSX : c_MSX)            ;use whichever smaller for X and Y
      GuiY := (s_MSY < c_MSY ? s_MSY : c_MSY)
      GuiW := Abs(Abs(s_MSX)-Abs(c_MSX))               ;doesn't matter which is bigger,
      GuiH := Abs(Abs(s_MSY)-Abs(c_MSY))               ;the absloute difference will be the same
      WinMove, ahk_id %2GuiID%,, GuiX, GuiY, GuiW, GuiH   ;move the window there
      GuiControl, 2:, GuiTextVar2, % GuiW ", " GuiH
   }
!r::               ;to retry at the last used coord.
   WinMove, ahk_id %2GuiID%,, GuiX, GuiY, GuiW, GuiH      ;to see where it's retrying
   Sleep 100
   WinMove, ahk_id %2GuiID%,, -2000,-2000, 2, 2          ;hide the window away
      WinGetPos, WinX, WinY, WinW, WinH, ahk_id %s_ID%
      ControlGetPos, CtrX, CtrY, CtrW, CtrH, , ahk_id %s_CID%
      regionInfo := "Relative to:`n   Screen: " GuiX "," GuiY
      regionInfo .= "`n   Window: " GuiX-WinX "," GuiY-WinY
      regionInfo .= "`n   Control: " GuiX-WinX-CtrX "," Guiy-WinY-CtrY
      regionInfo .= "`nWidth/Height: " GuiW "," GuiH
   Info1 := "RGB:`t"
   Color1 := regionGetColor(GuiX, GuiY, GuiW, GuiH) ;get the color of the region
   Time1 := "Time: " ErrorLevel
   Gui, 1:Color, %Color1%
   GuiControl, , GuiTextVar, % Info1 Color1 "`n`t" Time1 "`n`n" regionInfo
return
;end_region