[SOLVED] Problem getting old function regiongetcolor to work

Get help with using AutoHotkey and its commands and hotkeys
Posts: 2
Joined: 23 Mar 2015, 11:12

[SOLVED] Problem getting old function regiongetcolor to work

31 Mar 2015, 09:44

Hi everybody. I am trying to get the regiongetcolor function of This topic to work. It seems to be exactly what I need, since I already made code that determines the average color of a region, but it is painfully slow.
I haven't pasted the code here, as it consists of a relatively large number of lines.

The problem I encounter when trying the regiongetcolor function is that it keeps giving me 0x0 (black) as the average color. I tried it on two different computers, so it is not pc-related. Maybe it has to do with compatibility, since it was written in 2008.
I have been experimenting with the hwnd argument to get an average color for a region of the active window (not black), but that didn't seem to help, unless I didn't understand something correctly.

Should the code work when copying the demo and the code region, or do I have to modify something in order to get it to work on my computer?

Last edited by queixume on 01 Apr 2015, 03:09, edited 1 time in total.
User avatar
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Problem getting old function regiongetcolor to work

31 Mar 2015, 18:29

Everything works fine as posted on 32 bit AHK. I'm guessing you're running 64 bit AHK, which is a problem here since machine code differs between them. I couldn't find the C code infogulch used, but based on the function name and parameters I think I have guessed the intent.

Replace SumIntBytes with this. Note it requires the MCode function, which is included here. This should work with both 32 and 64 bit AHK.

Code: Select all

SumIntBytes( ByRef arr, len, ByRef a, ByRef r, ByRef g, ByRef b ) {
; by infogulch
; 32 bit:             16,843,009 px ||       4,104 x       4,104 px screen
; 64 bit: 72,340,172,838,076,673 px || 268,961,285 x 268,961,285 px screen
  static f
  if !f
   f := MCode("
   (LTrim Join
   DllCall( f, "ptr", &arr, "uint", len
    , "int64*", a, "int64*", r, "int64*", g, "int64*", b
    , "CDecl")

  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
If you're curious, here is the new Mcode source:

Code: Select all

typedef long long LL;
typedef unsigned int UINT;
void f(UINT* arr, UINT len, LL* a, LL* r, LL* g, LL* b)
	for (UINT* end = arr + len; arr != end; arr++)
		UINT color = *arr;
		*a += color >> 24;
		*r += color >> 16 & 0xFF;
		*g += color >> 8 & 0xFF;
		*b += color & 0xFF;
Posts: 2
Joined: 23 Mar 2015, 11:12

Re: Problem getting old function regiongetcolor to work

01 Apr 2015, 03:08

Thanks so much! It works like a charm now :)

It would have taken me a long time to figure this out, let alone solve it.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], LeonAskin, wineguy, yoooooo and 63 guests