Capture and display colors for a block of pixels

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Capture and display colors for a block of pixels

18 Mar 2017, 15:54

Hello,

What is the simplest script to capture a block or range of pixel colors and then display the results in a usable way?

For example, the following script gets the colors for the 16 pixels in the 4 x 4 block at (with its upper-left corner at) 115, 2035:

Code: Select all

F5::
CoordMode, Mouse, Screen
xA:=115
xB:=116
xC:=117
xD:=118

yA:=2035
yB:=2036
yC:=2037
yD:=2038

CoordMode, Pixel, Screen
PixelGetColor, cAA, xA, yA
PixelGetColor, cAB, xA, yB
PixelGetColor, cAC, xA, yC
PixelGetColor, cAD, xA, yD

PixelGetColor, cBA, xB, yA
PixelGetColor, cBB, xB, yB
PixelGetColor, cBC, xB, yC
PixelGetColor, cBD, xB, yD

PixelGetColor, cCA, xC, yA
PixelGetColor, cCB, xC, yB
PixelGetColor, cCC, xC, yC
PixelGetColor, cCD, xC, yD

PixelGetColor, cDA, xD, yA
PixelGetColor, cDB, xD, yB
PixelGetColor, cDC, xD, yC
PixelGetColor, cDD, xD, yD

Msgbox Colors for 4 x 4 range at %xA%, %yA%
	(Join
	`n
	`n%cAA%
	`n%cAB%
	`n%cAC%
	`n%cAD%
	`n
	`n%cBA%
	`n%cBB%
	`n%cBC%
	`n%cBD%
	`n
	`n%cCA%
	`n%cCB%
	`n%cCC%
	`n%cCD%
	`n
	`n%cDA%
	`n%cDB%
	`n%cDC%
	`n%cDD%
	)
Return
In this case, the MsgBox looks like this:
Colors for 4x4 range at 115, 2035

0x0E2112
0x0CIEI0
0x0AlC0F
0x0D2212

0x102212
0x0E2111
0x0D2111
0x0E2414

0x102213
0x0E2213
0x0E2213
0x0E2514

0x122213
0x102212
0x0E2112
0x0D2615
Questions:
1) Is there a simpler (shorter) way to write the script?

2) In place of the single column shown above, how can you get the MsgBox to arrange the results in four columns (to correspond to the actual pixel location)? In other words, is there a character for starting a new column (similar to how `n starts a new line) so that it looks as follows (ignore the vertical bars which are supposed to be aligned):
Colors for 4x4 range at 115, 2035

| 0x0E2112 | 0x102212 | 0x102213 | 0x122213 |
| 0x0CIEI0 | 0x0E2111 | 0x0E2213 | 0x102212 |
| 0x0AlC0F | 0x0D2111 | 0x0E2213 | 0x0E2112 |
| 0x0D2212 | 0x0E2414 | 0x0E2514 | 0x0D2615 |
3) Is there a way to modify the MsgBox so that its contents can be selected, copied, and pasted (into Excel for example)?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

18 Mar 2017, 16:15

Note: by "simpler" in question #1, it would be great if, rather than having to include the coords for every pixel, only a single x and single y coord were required along with the block dimensions. So for the previous example, the entries would be:

x = 115, y = 2035, w = 4, h = 4
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Capture and display colors for a block of pixels

18 Mar 2017, 16:31

WeThotUWasAToad wrote:Note: by "simpler" in question #1, it would be great if, rather than having to include the coords for every pixel, only a single x and single y coord were required along with the block dimensions. So for the previous example, the entries would be:

x = 115, y = 2035, w = 4, h = 4
You can use some simple logic like this,

Code: Select all

x:=115, y:=2035, w:=4, h:=4
str:=" (x,y):`n" 
Loop, % h {
	dy:=A_Index-1
	Loop, % w {
		dx:=A_Index-1
		str.= " (" x+dx  "," y+dy  ") " 
	}
	str.= "`n" 
}
msgbox % str
Output:

Code: Select all

 (x,y):
 (115,2035)  (116,2035)  (117,2035)  (118,2035) 
 (115,2036)  (116,2036)  (117,2036)  (118,2036) 
 (115,2037)  (116,2037)  (117,2037)  (118,2037) 
 (115,2038)  (116,2038)  (117,2038)  (118,2038) 
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

18 Mar 2017, 20:18

Helgef wrote:You can use some simple logic...
Thanks Helgef.

I love logic but as I've been working through this I've found myself chuckling re your adjective (simple) hehe but I think I sort of understand the gist.

1) I'm a bit unsure about the first line: str:=" (x,y):`n" The quotation marks mean it's a string and not a value, right? So I guess (in this particular example) it means the variable str represents (115,2035):`n but what does the colon do?

2) .= is a new operator for me but if I understand the docs then:
. (dot) is concatenate and .= is a short way of adding (concatenating) an additional term to the existing value of a variable. Is that in the ballpark?

3) the inside loop:
Loop, % w {
dx:=A_Index-1
str.= " (" x+dx "," y+dy ") "
}

• Loops w (width) cycles to capture all values for x in a single row
• Counts loops (one for each column) then subtracts 1, so dx is: 0,1,2,3,…
• Creates a new cord set: (xBase + dx), (yBase + dy), but I don't understand how str.= is being applied here nor why it has all the quotation marks.

4) the outside loop:
Loop, % h {
dy:=A_Index-1
[inside loop]
str.= "`n"
}

• Loops h (height) cycles to capture all the rows
• Counts loops (one for each row) then subtracts 1, so dy is: 0,1,2,3,…
• Sets a new value for str (ie the previous str concatenated with `n)

Finally, I don't know how to put your code(s) together so that I get a MsgBox.

Is the first code box meant to stand alone?
Is the second (Output) code box showing what the MsgBox should display or is it like a SubRoutine to be included in the script with the first box?

Thanks again and especially for your patience. I really appreciate it.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Capture and display colors for a block of pixels

19 Mar 2017, 09:43

Hi. I'll try to answer some of your questions.
WeThotUWasAToad wrote: 1) I'm a bit unsure about the first line: str:=" (x,y):`n" The quotation marks mean it's a string and not a value, right? So I guess (in this particular example) it means the variable str represents (115,2035):`n but what does the colon do?
Yes, that is just a string. It will only add (x,y): to str. It is meant as an explaining header for the rest of the string. :o
WeThotUWasAToad wrote: 2) .= is a new operator for me but if I understand the docs then:
. (dot) is concatenate and .= is a short way of adding (concatenating) an additional term to the existing value of a variable. Is that in the ballpark?
Yes, that is correct, str.="a" is the same as str:= str . "a".
WeThotUWasAToad wrote: 3) the inside loop:
Loop, % w {
dx:=A_Index-1
str.= " (" x+dx "," y+dy ") "
}

• Loops w (width) cycles to capture all values for x in a single row
• Counts loops (one for each column) then subtracts 1, so dx is: 0,1,2,3,…
• Creates a new cord set: (xBase + dx), (yBase + dy), but I don't understand how str.= is being applied here nor why it has all the quotation marks.
You analysis seems correct. The str.=" (" x+dx "," y+dy ") " is appending the value of the expressions x+dx and y+dy to the string str, spearetad by a comma and enclosing it in paranthesis, for the sake of visual enjoyment :geek:
WeThotUWasAToad wrote: 4) the outside loop:
Loop, % h {
dy:=A_Index-1
[inside loop]
str.= "`n"
}

• Loops h (height) cycles to capture all the rows
• Counts loops (one for each row) then subtracts 1, so dy is: 0,1,2,3,…
• Sets a new value for str (ie the previous str concatenated with `n)
Correct,str.="`n" just adds a line break to the string str.
WeThotUWasAToad wrote: Finally, I don't know how to put your code(s) together so that I get a MsgBox.
Is the first code box meant to stand alone?
Is the second (Output) code box showing what the MsgBox should display or is it like a SubRoutine to be included in the script with the first box?
There is a MsgBox included the double-loop code, you should note that the code boxes are equipped with the following controls, at the top, Code: [Select all] [Expand]. I recommend you either click expand, to view the whole code box, or select all and copy-paste to your prefered editor. The second (Output) is copied from the msgbox in the first code box.

The double-loop code is just meant for showing how to visit each pair of coordinates, without explicitly typing out each pair in the code. In the case of w=4,h=4, hard coding the coordinates aren't too bad, but if h and/or w where to be larger, it would be a hassle. Also, the loop is easily maintained, in case you want to change something.

Cheers! :)
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

24 Mar 2017, 15:39

Thanks for the detailed comments.
Helgef wrote:I recommend you either click expand, to view the whole code box, or select all and copy-paste to your prefered editor. The second (Output) is copied from the msgbox in the first code box.
Yes, I am familiar with the box buttons (ie Expand, Select All, etc) and use them frequently. I don't know what was causing a problem prior to my previous post but it was definitely on my end because I just tried the script again and it works fine.

Questions:
How do you get MsgBox content to display in multiple columns?
How do you "[copy content] from a msgbox"?
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

24 Mar 2017, 16:08

My next question is:

Once the block of coords is stored as the "str" variable, how do you apply the PixelGetColor command (rather than treating each pixel separately like this — which is only for the first column):

Code: Select all

x_01:=115, y_01:=2035
CoordMode, Pixel, Screen
PixelGetColor, color_0101, x_01, y_01
MsgBox %color_0101%

x_01:=115, y_02:=2036
CoordMode, Pixel, Screen
PixelGetColor, color_0102, x_01, y_02
MsgBox %color_0102%

x_01:=115, y_03:=2037
CoordMode, Pixel, Screen
PixelGetColor, color_0103, x_01, y_03
MsgBox %color_0103%

x_01:=115, y_04:=2038
CoordMode, Pixel, Screen
PixelGetColor, color_0103, x_01, y_04
MsgBox %color_0104%
(Edit: I used two digits for x & y variables to enable a block up to 99 x 99 pixels.)
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Capture and display colors for a block of pixels

24 Mar 2017, 16:25

WeThotUWasAToad wrote: Questions:
How do you get MsgBox content to display in multiple columns?
How do you "[copy content] from a msgbox"?
1) That is due to the way the string is built, i.e., spacing the row elements and adding line break after each row.
2) Simply press ctrl+c. ;)
WeThotUWasAToad wrote:My next question is:

Once the block of coords is stored as the "str" variable, how do you apply the PixelGetColor command
The str variable is only meant to show you what the double-loop does, i.e., visit each pair of coordinate in some range. Instead of building the string, add the PixelGetColor command, store or react to what ever it yields. For storing the pixel colors, you might want to use an array. Give it a try and you are to get back in the matter if you have any further questions.

Cheers.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

24 Mar 2017, 16:49

Helgef wrote:Simply press ctrl+c. ;)
Oh man! How did I not know that? lol Sometimes the simplest things are the most painful.

It brings to mind one of my favorite (and most applicable) sayings:
  • I worry about all the things I know I don't know,
    but what really concerns me
    are all the things I don't know
    and don't even know I don't know them!
In fact, I just added that as my profile signature. ;)

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Capture and display colors for a block of pixels

25 Mar 2017, 14:57

I'm still working at how to incorporate the pixel color commands (and not at the point to just ask for the solution) but I do have a couple more questions re the code you posted earlier:

Code: Select all

a	x:=115, y:=2035, w:=4, h:=4
b	str:=" (x,y):`n" 
c	Loop, % h {
d		dy:=A_Index-1
e		Loop, % w {
f			dx:=A_Index-1
g			str.= " (" x+dx  "," y+dy  ") " 
h		}
i		str.= "`n" 
j	}
k	msgbox % str
• Since x & y are defined in line a and dx & dy are defined within the loops, why is line b needed? You described line b as an explaining header but I'm not sure what that means and I cannot find it in the docs. It makes me think of "explanations" which follow semicolons but I know it is somehow required for the script to execute properly. Also, I'm still not sure what to make of the colon between (x,y) & `n in that line.

• Why is `n needed twice, once in line b and again in line i?

• Re line g, I was looking at the quotation marks completely wrong (as though the first and the last go together enclosing the whole thing and then the 2nd and 2nd-to-last enclose something inside of that and so on). That's why I was so confused. However, I've been bouncing some AHK syntax back and forth with Excel syntax (with which I am much more comfortable) and it dawned on me that they (the quotation marks) are simply enabling the use of the parentheses & comma just as they do in Excel, ie ="(" & 115+0 & "," & 2035+0 & ")".

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Capture and display colors for a block of pixels

25 Mar 2017, 15:32

WeThotUWasAToad wrote: • Since x & y are defined in line a and dx & dy are defined within the loops, why is line b needed? You described line b as an explaining header but I'm not sure what that means and I cannot find it in the docs. It makes me think of "explanations" which follow semicolons but I know it is somehow required for the script to execute properly. Also, I'm still not sure what to make of the colon between (x,y) & `n in that line.
Line b isn't needed, it just add the plain text (x,y): to the first line of the string str, the point is to make it clear that in the subsequent lines, the numbers inside () represent x and y coordinates.
WeThotUWasAToad wrote: • Why is `n needed twice, once in line b and again in line i?
Line i is just there to format the string into rows and columns. Remove `n on line i to see the order of the visited coordinates, where the left-most is the first visited, and the right-most is the last one.
WeThotUWasAToad wrote: • Re line g, I was looking at the quotation marks completely wrong (as though the first and the last go together enclosing the whole thing and then the 2nd and 2nd-to-last enclose something inside of that and so on). That's why I was so confused. However, I've been bouncing some AHK syntax back and forth with Excel syntax (with which I am much more comfortable) and it dawned on me that they (the quotation marks) are simply enabling the use of the parentheses & comma just as they do in Excel, ie ="(" & 115+0 & "," & 2035+0 & ")".
The first to goes together, forming the string ( which is concateneted to the result of x+dx, and then concateneted to the string , and so on.

Cheers :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa, vanove and 175 guests