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 

Get avg color of portion of the screen

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
infogulch



Joined: 27 Mar 2008
Posts: 130
Location: KC, MO

PostPosted: Thu Jul 17, 2008 3:41 pm    Post subject: Get avg color of portion of the screen Reply with quote

i need to get the average color of a portion of the screen, and i don't want to do a PixelGetColor as that takes a long time (think 50x50 px portion is 2500 pixels) so i thought i'd use Gdi+ to capture a portion of the screen, and perhaps blur it 100% to find the average. (there's probably a better way)

this uses tic's Gdip_ lib. it can be found Here

this is just a test script and it CRASHES ahk, so don't run it as is. Razz i ultimately want to wrap it in a function that maybe returns the color found in hex?

Code:

SetWorkingDir, %A_ScriptDir%

;my desktop is the most colorful part of my computer, give me time to switch to it :P
MsgBox, Ready?

If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

Y1 := 0
Y2 := 50

Loop 5
{ ;loop to get 5 different portions of the screen

   ;construct the coordinates
   X1 := ( A_Index - 1 ) * 50
   X2 := A_Index * 50
   Coord = %X1%|%Y1%|%X2%|%Y2%
   
   pBitmap := Gdip_BitmapFromScreen(Coord)   ; get a 50x50 px square at the coord
   G := Gdip_GraphicsFromImage(pBitmap)   ; this is where i think i've gone wrong
   ;blur it 100% to make it one color - i.e. get the average (i think)
   Gdip_BlurBitmap(G, 100)               ; because this crashes the script!!

   StringReplace, Coord, Coord, |, ., All
   Gdip_SaveBitmapToFile(pBitmap, "BlurredScr_" Coord "_.bmp") ;save the image w/ coord in filename

; The bitmap can be deleted
   Gdip_DisposeImage(pBitmap)
; The graphics may now be deleted
   Gdip_DeleteGraphics(G)
}

; ...and gdi+ may now be shutdown
Gdip_Shutdown(pToken)
ExitApp



Thanks for your help Very Happy
_________________
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1355

PostPosted: Thu Jul 17, 2008 4:49 pm    Post subject: Reply with quote

An interesting challenge.....look at my response on the Gdi+ thread, and hopefully you like the script. ask if you dont understand how ive done anything, as im sure it could be a bit confusing Smile
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1355

PostPosted: Thu Jul 17, 2008 5:06 pm    Post subject: Reply with quote

Mmmm I've done some testing, and it doesn't appear to take the average with the way I have used. I think I could write another way, but it would require machine code to do it fast enough (although the c++ function would be very short)
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 130
Location: KC, MO

PostPosted: Thu Jul 17, 2008 6:28 pm    Post subject: Reply with quote

thanks! i'll take a look Smile ( link to Gdi+ post )
_________________
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 130
Location: KC, MO

PostPosted: Fri Jul 18, 2008 9:41 pm    Post subject: Reply with quote

right, it seems like it gets the color of the top left pixel of the selection. pretty neat anyway Wink

i was wondering about the GetPixel function, is the number it returns relevant/useable by ahk directly, or just gdi+? i noticed it returns ARGB (var) , but the number of digits are different for different colors, e.g. black is -16777216 and white is -1. (i think)

i've done some research, so i think i've got a general idea of how you're doing it. ( lol, yeah right Razz ) but an explaniation of variable prefixes would really help. (like the "p" in "pBitmap") looking thru the lib i saw p, h, maybe w? i guess i'm wanting to know what the functions return, and what ahk is dealing with.

i would really appreciate a solution to the average color. it's too bad you'd have to go to c++ to get it done, but speed is really a priority, oh well. i hope it's not too much trouble for you. Confused Smile

thanks agian for your help Very Happy
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
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