Perspective Dynamic Button COLOR

I am having a hard time finding out why when i apply an expression to background color element it will change in the preview on property editor but not in the view itself. Expression :

gradient(toStr(tag("[default]" + "Flat Sorter Collector/OUTERBELT SLUG FINDER/GANTRY 1/TRK_FILES/U180400_Gantry_Sensor_2/" + {this.meta.name} +"_")), {[default]Flat Sorter Collector/OUTERBELT SLUG FINDER/GANTRY_LIMIT.value}, {[default]Flat Sorter Collector/OUTERBELT SLUG FINDER/GANTRY_HEIGHT.value}, toColor("orange"), toColor("black"))

This is looking at the name of the of the component and finding the tag and then gradient between the two limits. Like I stated I see the color change in the preview but the buttons color in the view dont change.

Perspective does not use Java colors, which is what your binding is returning. As a result, you’ll see no change. Perspective uses CSS for displaying components, so any returned color needs to be in a CSS-supported format. This limits you to a select subset of formats, including RGB, Hex, and HSL.

1 Like

Yes i figured that after posting … is there a way to convert to Hex ?? i need the gradient function as the numbers range between 3600 and 0 . i have tried the tohex() with no luck .

Too many ways of doing it. I will take the rgba path.
Just add a script transform

2 Likes

thank you jespinmartin1 for sharing your knowledge it worked perfect.

Maybe I've missed something somewhere, but I'm getting a script evaluation error even before I get to line 8 of the script in the picture.

Did I miss something, or has the Script Transform changed somehow?

I'm running Ignition version 8.1.22 with Perspective Module 2.1.22.

Move the mouse over the red text "Error_ScriptEval", wait till the tooltip appear and share.

No problem. Here's the details:

It's telling you that value is a String (unicode), and therefore does not have functions like getRed() available to invoke upon it. Earlier posts are receiving a Color object, which is why that transform works there. You ca either convert your String to a Color through some manner, or you can act upon it as a string:

values = value.split("(")[-1]
values = values.split(")")[0]
values = values.split(",")
red = values[0]
green = values[1]
blue = values[2]
alpha = values[3] if len(values) > 3 else None
2 Likes

pretty clear to me, the first output of the expression above is returning a string (unicode). Make sure to always return a Color, then the script transform would work.

or try this

from java.lang import String
from java.awt import Color
color = value
hexformat = '#%02X%02X%02X%02X'
if isinstance(color,(str,unicode)):
	colors = color.replace('(','').replace(')','').replace('color','').split(',')
	r,g,b = [int(c) for c in colors[0:3]]
	a = int(colors[-1]) if len(colors) == 4 else 255
	return String.format(hexformat,r,g,b,a)
elif isinstance(color,Color):
	return String.format(hexformat,color.getRed(),color.getGreen(),color.getBlue(),color.getAlpha())
else: return color
2 Likes

:clap: :clap: :clap:

Wish I could mark that as a second solution.