How to find text against a certain background color?


I am trying to use Eggplant’s OCR to find a desired input text against a certain background color. This is because on the SUT I am testing on, there are many same word selections on the website and things tend to move around a lot so searchRectangles do not always provide the best search functionality.

I understand that you can use a dynamic searchRectangle to narrow search selections down, but I want to add additional search capability using background color as a parameter for OCR.

I could not find any documentation for OCR searching against a particular background color. For example, I was looking for something like

put EveryImageLocation(Text:“My Text”, Image:“Background Color”)

where this would search for text on the page which contained contain the inputted background color and then would return the coordinates of all known locations. Obviously, this line doesn’t work, but I was hoping for an in-built function that can help do this.

Are there any functions that can search text against a certain background color? If not, how can this be accomplished using existing built-in Eggplant functions?

Thank you.

Hello RobbyK,
please have a look to e.g. Working with OCR.
So put EveryImageLocation(text:“My text”, Contrast:On, ContrastColor: ) my solve your issue, if “my color” is either your background or foreground color. Additionally ’ ContrastTolerance’ may improve the search result.

1 Like

I would think you could use Text: ContrastColor to help differentiate but it would depend on your circumstances and I imagine you will have tried that already.
Alternatively, you could probably use a dynamic searchrectangle to define your coordinates in which to search. Something like:
put everyimagelocation(“Scratch_Logan/SCRATCHWORK/WhiteSquare”)
It looks like EPF caps out at 1,000 found images (reasonable), so depending on what your SUT looks like this might be out of the question.

click text:“WebStore”,searchrectangle:[imagelocation(item 1 of everyimagelocation(“Scratch_Logan/SCRATCHWORK/WhiteSquare”)),imagelocation(the last item of everyimagelocation(“Scratch_Logan/SCRATCHWORK/WhiteSquare”))]

I’ll bet I could figure out a reasonable method if I had an example screenshot of what you are working on. Capture a screenshot using Control > Capture Screen so I can get the native resolution/aspect ratio.

I usually sample the background color with colorAtLocation()
then use
if ImageFound(Text:“text to find”, Contrast:“On”, ContrastColor(R,G,B))
click found image Location()
else if ImageFound (Text:“text to find”, Contrast:“On”, ContrastColor(R,G,B)) //<- you can repeat to try different background colors if needed.

@robbyk , I read your question to mean that the text that you are looking for appears many times on the screen but only once against a particular color background. You want to locate the text that appears against that color. If I am correct, there is more information here: Image and OCR Searches

You could write code that looks like this:

set myText to "droid" -- This is the text that you are looking for

set myColor to "0,0,255" -- This is a pure green color

Set myTextRectangles to EveryImageRectangle(text:myText)

Repeat with each myRectangle of myTextRectangles
	set myColorSampleLocation to the topRight of myRectangle + (5,2) -- Choose a point 5 pixels to the right of the rectangle and 2 pixels down for sampling the color
	if ColorAtLocation(myColorSampleLocation) = myColor then
		Log "This is the droid you are looking for."
		Exit Repeat
		Log "This is NOT the droid you are looking for."
	end if
End Repeat