Using tolerance on GetPixelColor>%x%,%y%,result

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Using tolerance on GetPixelColor>%x%,%y%,result

Post by NickD » Fri Sep 23, 2016 2:27 pm

I would like to know if its possible to specify a match tolerance when using the below:

Let>yellow=36754
Let>yellowx=10
Let>yellowy=50
Label>loop
GetPixelColor>%yellowx%,%yellowy%,result
If>color=yellow
Message>Make a cup of tea
Else
Message>No biscuits for you
Endif
Goto>Loop

So that any colors within a specified range of "yellow" return true.

If this is possible, how is it done?

Thanks.

NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Re: Using tolerance on GetPixelColor>%x%,%y%,result

Post by NickD » Fri Sep 23, 2016 11:35 pm

Just to clarify, this is a serious question, probably a bad idea for me to use the cup of tea and biscuit message examples lol.

Can I use Regex, eg: Yellow=[1][2]/d/d/d/d/d/d to return a positive match on any eight digit color beginning with 12?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Using tolerance on GetPixelColor>%x%,%y%,result

Post by JRL » Sat Sep 24, 2016 5:12 am

First thing you need to know is that the color numbers 0 through 16777215 are only sequential as numbers, In numeric order they do not produce a sequential color stream. For example, 255 is red, 256 is nearly black.

Maybe something like this? A shot in the dark or maybe a starting point. The "VTol" variable would be how much you could be off (Plus or minus)from an exact match with each color component of the specified color number. Tolerance from 0 to 255, where 0 would only match the given color "vTestColor" and 255 would match any color. You are matching within your tolerance if all three flags are "True".

Code: Select all

OnEvent>Key_Down,VK1,0,Process
OnEvent>Key_Down,VK27,0,Quit


Let>vTol=15
Let>vTestColor=36754
ColorToRGB>vTestColor,vTRed,vTGrn,vTBlu

Label>Loop
Wait>0.01
Goto>Loop

SRT>Process
  GetCursorPos>Curx,Cury
  GetPixelColor>Curx,Cury,vColor
  ColorToRGB>vColor,vRed,vGrn,vBlu

  If>{((%vRed%-%vTol%)<=%vTRed%)and((%vRed%+%vTol%)>=%vTRed%)}
    Let>vRedFlag=True
  Else
    Let>vRedFlag=False
  EndIf

  If>{((%vGrn%-%vTol%)<=%vTGrn%)and((%vGrn%+%vTol%)>=%vTGrn%)}
    Let>vGrnFlag=True
  Else
    Let>vGrnFlag=False
  EndIf

  If>{((%vBlu%-%vTol%)<=%vTBlu%)and((%vBlu%+%vTol%)>=%vTBlu%)}
    Let>vBluFlag=True
  Else
    Let>vBluFlag=False
  EndIf

  MDL>Red = %vRedFlag%%crlf%Green = %vGrnFlag%%crlf%Blue = %vBluFlag%
END>Process

SRT>Quit
  Exit>0
END>Quit

NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Re: Using tolerance on GetPixelColor>%x%,%y%,result

Post by NickD » Sat Sep 24, 2016 4:37 pm

Thanks JRL, that's really helpful.

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Using tolerance on GetPixelColor>%x%,%y%,result

Post by JRL » Sun Sep 25, 2016 4:48 am

After thinking about this it occurred to me that the math is not correct. What I proposed was to take the tolerance number and test that far above and below each color component. That in itself might be ok, but I said that you have 256 tolerance choices when in reality, if we are using the entire tolerance number above and below the tested pixel color, we have effectively halved the tolerance choices. Therefore, the tolerance number can only be 0 thru 127. So even a tolerance of 1 allows a match from any of 131,072 colors. Not that 256 tolerance choices is any more acceptable allowing a tolerance of 1 to match 65,536 different colors.

I have a Scripts and Tips entry for creating a one pixel .BMP file. This could be used to leverage Macro Scheduler's image recognition and image recognition tolerance against a single chosen pixel. You would create a one pixel .BMP file of the test color, then use ScreenCapture> to capture one pixel to a second BMP file. The test color BMP file would be used as the needle file for FindImagePos> and the captured pixel BMP would be the haystack in FindImagePos>. Using this method would allow the FindImagePos> function's tolerancing to be used. Although it seems to me that the tolerance choices in FindImagePos> will still lead to a very large number of possible matches. If using the "EXACT" algorithm, in theory a tolerance of one will still match 65,536 different colors.

A better method or algorithm might be available. Anyone with experience in this have any advice?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts