Find and Replace: Vision Component Colors

Here is a recursive script that would probably get quite a bit of the work done on a per window basis:

Prototype Function
from java.awt import Color
from javax.swing.plaf import ColorUIResource
findRGB = [0, 0, 0]
replaceRGB = [255, 255, 255]
def colorFinder(component):
	for component in component.getComponents():
		if component.background == Color(r, g, b) or component.background == ColorUIResource(r, g, b):
			component.background = Color(newR, newG, newB)
		if component.foreground == Color(r, g, b) or component.foreground == ColorUIResource(r, g, b):
			component.foreground = Color(newR, newG, newB)
		colorFinder(component)
r = findRGB[0]
g = findRGB[1]
b = findRGB[2]
newR = replaceRGB[0]
newG = replaceRGB[1]
newB = replaceRGB[2]
parentWindow = system.gui.getParentWindow(event)
colorFinder(parentWindow)

Edit: While the prototype function I initially developed as a solution to this problem does work, it is inefficient. Instead, use Paul Griffith's rewrite below.

The script finds the parent window, and then recursively loops through every subcomponent looking for the foreground and background properties. It compares those properties to the findRGB list in line three of the script, and if they match, it replaces them with a java.awt.Color defined by the replaceRGB list in line four of the script.

If looping through all the windows is needed, wrap the preceding script in the following code:

for name in system.gui.getWindowNames():
	system.nav.openWindow(name)
	#[put the above listed script here]
	system.nav.closeWindow(name)

If there are hundreds of windows, then consider defining a range to limit the for loop, and conduct the update in batches.

1 Like