# Perspective Dynamic Button COLOR

**URL:** <https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271>\
**Category:** Ignition\
**Created:** [October 23, 2021, 12:04pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271 "2021-10-23T12:04:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrcharl](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@byrcharl](https://forum.inductiveautomation.com/u/byrcharl)\
**Post date:** [October 23, 2021, 12:04pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/1 "2021-10-23T12:04:52Z")

</div>

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.

 ![IGNITION SLUGFINDER](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/5/1533344c3b6d6646b611dcd8c814774a61b21397.png)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [October 23, 2021, 3:35pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/2 "2021-10-23T15:35:51Z")

</div>

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](https://www.w3schools.com/cssref/css_colors_legal.asp), including RGB, Hex, and HSL.

---

<div class="post-metadata">

**Author:** ![byrcharl](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@byrcharl](https://forum.inductiveautomation.com/u/byrcharl)\
**Post date:** [October 23, 2021, 3:53pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/3 "2021-10-23T15:53:51Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [October 23, 2021, 5:56pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/4 "2021-10-23T17:56:16Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/2/0298185cc42d047136c83a2e400948f1a436f919.png)

---

<div class="post-metadata">

**Author:** ![byrcharl](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@byrcharl](https://forum.inductiveautomation.com/u/byrcharl)\
**Post date:** [December 8, 2021, 6:08pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/5 "2021-12-08T18:08:56Z")

</div>

thank you jespinmartin1 for sharing your knowledge it worked perfect.

---

<div class="post-metadata">

**Author:** ![Samuel\_Porter](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samuel_porter/32/52836_2.png) [@Samuel\_Porter](https://forum.inductiveautomation.com/u/Samuel_Porter)\
**Post date:** [January 17, 2024, 2:44pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/6 "2024-01-17T14:44:39Z")

</div>

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.

 ![2024-01-17 (1)](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/8/08f610a66e1579bc628ade86fcb0ac1e246ea5ed.png)

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [January 17, 2024, 3:13pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/7 "2024-01-17T15:13:06Z")

</div>

Move the mouse over the red text "Error\_ScriptEval", wait till the tooltip appear and share.

---

<div class="post-metadata">

**Author:** ![Samuel\_Porter](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samuel_porter/32/52836_2.png) [@Samuel\_Porter](https://forum.inductiveautomation.com/u/Samuel_Porter)\
**Post date:** [January 17, 2024, 3:27pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/8 "2024-01-17T15:27:58Z")

</div>

No problem. Here's the details:

 ![Screenshot 2024-01-17](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/d/0d5d27dcc2715de309bc906dbc41b42151f6e238.png)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [January 17, 2024, 3:49pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/9 "2024-01-17T15:49:59Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [January 17, 2024, 3:52pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/10 "2024-01-17T15:52:42Z")

</div>

pretty clear to me, the first output of the expression above is returning a string (unicode). Make sure to always return a [Color](https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html), then the script transform would work.

or try this

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Samuel\_Porter](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samuel_porter/32/52836_2.png) [@Samuel\_Porter](https://forum.inductiveautomation.com/u/Samuel_Porter)\
**Post date:** [January 17, 2024, 4:16pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/11 "2024-01-17T16:16:31Z")

</div>

👏 👏 👏

Wish I could mark that as a second solution.

---

<div class="post-metadata">

**Author:** ![Ervin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ervin/32/57644_2.png) [@Ervin](https://forum.inductiveautomation.com/u/Ervin)\
**Post date:** [July 30, 2024, 12:35pm UTC](https://forum.inductiveautomation.com/t/perspective-dynamic-button-color/52271/12 "2024-07-30T12:35:21Z")

</div>

found a much simpler expression solution, and it works fine for me:

> replace('color(0,255,0,255)', 'color', 'rgba')

I get from OPC UA the different states of a device as an int and I defined in a custom UDT a derived variable that reads from a memory dataset the corresponding meaning as TextDescription and and color as colorDataType. so I searched for the same solution, but found this quick way. especially that in UDT expresions I saw no way of using scripts only expressions
