A color picker component for vision

I prefer system.gui.chooseColor() as recommended in post 3, but while working on a different problem, I developed a way to manipulate the cell renderer of the dropdown and subsequently began exploring alternate uses for the script. Here is an alternative approach that converts a dropdown component to a color picker:

• Replace the dropdown's string datatype Label column with a color datatype label column:
image

• Then, use the following script on the dropdown's propertyChange event handler:

from com.inductiveautomation.plaf import ComboListCellRenderer
if event.propertyName == 'componentRunning':
	# Override the cell renderer for custom functionality
	class ColorCellRenderer(ComboListCellRenderer):
		def getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus):
			ComboListCellRenderer.getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus)
			if index != -1:
				color = event.source.data.getValueAt(index, 1)
				self.background = color
				self.foreground = color
			if index != -1 and event.source.selectedIndex == index:
				event.source.background = self.background
				event.source.foreground = self.foreground
			return self
	renderer = ColorCellRenderer()
	event.source.setRenderer(renderer)

Here is the result:
image
Note: for this script to run in the designer, preview mode must be on when the window is opened

I imagine that this could be useful if there is a need to limit the color choices

Edit: Updated this code, so it has proper interactions with the Ignition look and feel in versions 8.0.x and 8.1.x

3 Likes