Jump to content
Macro Express Forums

Larger Region To Look For Color

Recommended Posts

As things now stand we can look for a color at a particular screen pixel.

Please add the allowance to look wider than the single pixel.

For example: Anywhere in a 10x10 pixel region.  Or anywhere in a 100x100 pixel region.

Say, specifically:

<REPEAT UNTIL Variable="%N1%" Condition="\x00" Value="15597568"/>

<GET PIXEL COLOR Option="\x01" Rel_To_Screen="TRUE" X="1000" Y="500" Destination="%N1%"/>

Only covers one single pixel.

I'd like to be able to say, if ANY NAVY BLUE is present in a 100x100 square, then move on.  If not, keep repeating the 'looking for.'

I'd greatly appreciate your assistance.

Nicholas Kormanik


Link to comment
Share on other sites

That would be a handy feature to have.  You could write a macro to do it, that could be run (called) from any number of other macros.  The calling macro would simply pass the starting location and dimensions of the search area to the called macro (in variables), along with the color to be searched for; with results returned in another variable.  One scan of a 100x100 square is fairly time-consuming -- perhaps a second on my PC running Windows 7 and ME 3.  There have been recent threads on this forum complaining of MUCH longer pixel-check times on Windows 10 systems, indicating such an extended search must be done in some other computer language than Macro Express and might not be feasible even there. 

Link to comment
Share on other sites

The area being searched for a particular color doesn't necessarily have to be a square, thinking more about it.  Could be a line.  A horizontal line.  What is actually on screen is TEXT.  So a line passing through the text would do perfectly.  A full square or rectangle would be overkill.


Link to comment
Share on other sites

I have written many scripts to search for a particular pixel colour within a region. There are several ways to approach the problem. Here are five methods I have tried:


1. Examine a single coordinate at regular intervals.

2. Check a vertical or horizontal line.

3. Check a rectangular region, one line at a time. 

4. Check the entire screen or a specific window, one line at a time.

5. Check each pixel in a spiral pattern from a single coordinate.


My experience is that only [1] and [2] execute quickly enough to be practical. [3] works fine if the rectangle isn't too large. [4] can take too long to execute: a 1366 x 768 screen would require 1,049,088 pixel colour tests! [5] is elegant and works nicely, but only if you have a rough idea of where to start the search. I have never found a practical application for [5], at least not yet.

A series or combination of these methods is possible. I have, for example, searched a vertical line until I have found a pixel, and then hunted horizontally for a second pixel colour. I have done this for applications that have large rectangles consisting of only a few colours, e.g., a web app that has two or three frames. My most sophisticated macro of this ilk does four searches one after another: along a vertical line, then horizontal, then vertical and then horizontal again. During the last search, the macro counts the number of times a certain colour is found. It was challenging to make this macro work reliably.

When I need to search large regions for a pixel colour, I sometimes opt for other scripting tools. :(

Search rectangle for a pixel colour would be a dynamite feature to add to a future release of Macro Express! At one point I submitted a feature request to the good folks at Insight... I would encourage others to do the same.

Link to comment
Share on other sites

Thanks for the encouragement!  Hope the feature makes it into the next release.

In the meantime, someone here please show the most efficient way of checking a HORIZONTAL LINE, say 10 pixels across.

Actual case I'm dealing with involves navy blue text on a white background, on a particular web page.  Trouble is, page gets refreshed, and the text moves slightly from where it was before.  A horizontal line could catch it, though.

Link to comment
Share on other sites

This macro will search a rectangular area.  The "rectangle" can be as simple as a single horizontal
or vertical row of pixels depending on how you set the upper-left and lower-right boundaries.
There is no error checking in the macro -- if you exceed the limits of the screen, or specify
a bottom-right point that is above or left of the top-left point, results are not guaranteed.  

The first Mouse Move command makes the mouse track the pixels being checked, which is handy for
testing because you can watch the search pattern.  That Mouse Move should be inactivated for
normal usage as it slows the macro down considerably.


Log Message to Default Error Log
Log Errors
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Keystroke Speed: 30 Milliseconds
Mouse Speed: 30 Milliseconds
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// This macro is intended to be called from another macro
// Macro searches rectangular area for specifie pixel color, puts mouse on first pixel found
// Variables passed from caller:
// .... %N90% and %N91% Top left corner of area to be searched (screen relative)
// .... %N92% and %N93% Bottom right corner of area to be searched (screen relative)
// .... %N94% Pixel color to be located
// For testing set up coordinates normally passed from calling macro
Variable Set Integer %N90% to 800
Variable Set Integer %N91% to 500
Variable Set Integer %N92% to 850
Variable Set Integer %N93% to 500
Variable Set Integer %N94% to 6956042
// Copy search area coordinates to working variables
Variable Modify Integer: Copy %N90% to %N20%
Variable Modify Integer: Copy %N91% to %N21%
// Search until pixel color found or until area boundaries exceeded
// Search proceeds left to right in a row, then drops to next row, etc.
Repeat Until %T1% <> %T1%
  // For testing move mouse to next pixel to be checked
  Mouse Move Screen %N20%, %N21%

  Get Pixel: Screen Coords: %N20%,%N21% into %N24%
  If Variable %N24% = variable %N94%
    Mouse Move Screen %N20%, %N21%
    Repeat Exit
  End If
  // Bump pixel coordinates
  Variable Modify Integer: Inc (%N20%)
  If Variable %N21% = variable %N93%
  If Variable %N20% > variable %N92%
    Repeat Exit
  End If
  If Variable %N20% > variable %N92%
    Variable Modify Integer: Inc (%N21%)
    Variable Modify Integer: Copy %N90% to %N20%
    Variable Modify Integer: Inc (%N20%)
  End If
Repeat End
Macro Return



Link to comment
Share on other sites

If you don't understand what it means to call one macro from another, don't worry. Suffice to say it is a technique for recycling Macro Express scripts.

For now, focus your energies on perfecting a script that searches a line of, say, 400 pixels, maybe something like this...

Variable Set Integer %x% to 100 // Initial x coordinate
Variable Set Integer %y% to 300 // Initial y coordinate
Variable Set String %TargetColour% to "255" // Target pixel colour 255 = RED of the words "Macro Stop" in the MEP "Script Editor"
Repeat Start (Repeat 400 times)
  Delay: 10 milliseconds // Slow down the macro so you can see what's happening
  Mouse Move: %x%, %y% Relative to Screen
  Get Pixel Color from Beneath the Mouse into %PixelColour%
  If Variable %PixelColour% Equals "%TargetColour%"
    Text Box Display: Pixel found!
    Macro Stop
  End If
  Variable Modify Integer %x%: Increment // Increase the value of x by 1
End Repeat
Text Box Display: Pixel NOT found!


<VARIABLE SET INTEGER Option="\x00" Destination="%x%" Value="100" _COMMENT="Initial x coordinate"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%y%" Value="300" _COMMENT="Initial y coordinate"/>
<VARIABLE SET STRING Option="\x00" Destination="%TargetColour%" Value="255" NoEmbeddedVars="FALSE" _COMMENT="Target pixel colour 255 = RED of the words \"Macro Stop\" in the MEP \"Script Editor\""/>
<REPEAT START Start="1" Step="1" Count="400" Save="FALSE"/>
<DELAY Flags="\x02" Time="10" _COMMENT="Slow down the macro so you can see what's happening"/>
<MOUSE MOVE Option="\x01" X="%x%" Y="%y%" _PROMPT="0x000A"/>
<GET PIXEL COLOR Option="\x00" Rel_To_Screen="TRUE" Destination="%PixelColour%"/>
<IF VARIABLE Variable="%PixelColour%" Condition="\x00" Value="%TargetColour%" IgnoreCase="FALSE"/>
<TEXT BOX DISPLAY Title="Pixel found!" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang4105{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%x%" _COMMENT="Increase the value of x by 1"/>
<TEXT BOX DISPLAY Title="Pixel NOT found!" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang4105{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>


Link to comment
Share on other sites

12 hours ago, nkormanik said:

Wow, what a beauty.  Not sure how to actually use it, so someone hurry and do that.  What does it mean to call from another macro?


See if you can struggle through making my macro or acantor's work for you.  You will learn a lot.  

If you give a man a fish you feed him for a day ....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...