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?
Thanks!
[SOLVED] Problem getting old function regiongetcolor to work
[SOLVED] Problem getting old function regiongetcolor to work
Last edited by queixume on 01 Apr 2015, 03:09, edited 1 time in total.
- LinearSpoon
- Posts: 156
- Joined: 29 Sep 2013, 22:55
Re: Problem getting old function regiongetcolor to work
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.
If you're curious, here is the new Mcode source:
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
1,x86:8b44240833c9568b742408c7442408000000008d04868bd02bd683c203c1ea023bf00f47d185d2745f538b5c241c55
8b6c241c578b0e8d76048b7c241c8bc1c1e81801078bc78b7c2428835004008bc1c1e81025ff0000000145008bc183550400
c1e80825ff00000001030fb6c18353040001078b4424148357040040894424143bc275af5f5d5b5ec3,x64:48895c2408488
97c24104533db8bc2498bf8488d14814c8bd1488bda482bd94883c30348c1eb02483bca490f47db4885db7443488b5424304
c8b4424280f1f00418b0a49ffc34d8d52048bc148c1e8184801078bc148c1e8100fb6c04901018bc148c1e8080fb6c049010
00fb6c14801024c3bdb75ca488b5c2408488b7c2410c3
)")
}
DllCall( f, "ptr", &arr, "uint", len
, "int64*", a, "int64*", r, "int64*", g, "int64*", b
, "CDecl")
}
MCode(mcode)
{
static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
return
if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
return
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)
}
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;
}
}
Re: Problem getting old function regiongetcolor to work
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.

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