Changing rectangle color based on multiple tag value

Hi,
I am having 4 boolean tags. when either of the tag is ‘1’ i need to change the color of the rectangle .
for example:
when tag 1 is true, the rectangle color should be red
when tag 2 is true, the rectangle color should be green
when tag 3 is true, the rectangle color should be yellow
when tag 4 is true, the rectangle color should be blue

how an i do this using scripting?

Consider using the style customizer, instead.

https://docs.inductiveautomation.com/display/DOC81/Vision+Component+Customizers

After rereading your post, make a custom property with a binEnc function to generate the value. The style customizer should use that property as the driving value.

Jordan pointed out what seems to be the ideal way to do something like this but, out of curiosity, if you really want to do this via scripting, it would look something like this:

Tag1 = 
Tag2 = 
Tag3 = 
if tag 1 == True:
	event.source.fillPaint = system.gui.color(255,0,0) #Red

if Tag2 == True:
	event.source.fillPaint = system.gui.color(0,255,0) #Green

if Tag3 == True:
	event.source.fillPaint = system.gui.color(255,255,0) #Yellow

if Tag4 == True:
	event.source.fillPaint = system.gui.color(0,0,255)  #Blue

event.source.fillPaint” is just a reference to the component you’re changing the color of, then system.gui.color returns the color object you want.
But, you still need to worry about how to set up your trigger, otherwise your script would not execute when you want it to.
If i was’t able to use styles for some reason, i would still use custom properties with the tag values and use the property change method to trigger only when one of the tag changes.

can u be little more brief regarding this?

More brief?
“use binEnc with Style Customiser” :slight_smile:

However, to elaborate instead, Jordan’s suggesting to bind a new custom property to the binEnc expression function e.g. binEnc({tag1}, {tag2}, {tag3}, {tag4}) and then use that property as the driving property in a Style Customiser applied to your component. Then you can set the properties you wish to change based on the combination of tags. binEnc will encode your 4 tags into a single binary number. If tag1 and tag2 are on, then it returns 3 (0011 binary converted to decimal = 3).

If your tags are mutually exclusive, then you could also use binEnum which will then just return the index position of the first “true” tag.

https://docs.inductiveautomation.com/display/DOC81/Vision+Component+Customizers#VisionComponentCustomizers-StyleCustomizer
https://docs.inductiveautomation.com/display/DOC81/binEnc
https://docs.inductiveautomation.com/display/DOC81/binEnum

1 Like

This is my expression for my component to change color to ylw.....
Keep getting the Syntax Error on TOKEN 'BOUND CONST' (Line 1,char 56)
Am i missing something

if {[default]PLC STATUS/M-1 PLC-5/Forces Enabled S2:1_8} == True:
{Root Container.Circle 3.fillPaint} = color(255,255,0) #Yellow

1 Like

Yes, what you're missing is that expressions are different from scripts and it seems that you're mixing a bit of both.
You can take a better look at the difference here: Scripting Vs. SQL Vs. Expressions | Ignition User Manual
Expressions usually are used on bindings so they can return a value to the field you're binding to.They are much simpler and faster than scripts, so
{Root Container.Circle 3.fillPaint} = color(255,255,0) #Yellow
isn't needed since the expression is already supposed to return this value for you.
You can take a better look at the syntax for the IF expression function here:
if | Ignition User Manual
Since the if syntax is if(condition, trueReturn, falseReturn), i believe the correct expression for you is:

If({[default]PLC STATUS/M-1 PLC-5/Forces Enabled S2:1_8},toColor("255,255,0"),
toColor("255,0,0"))

You can read more about the toColor function here, it's not needed in every case. toColor | Ignition User Manual
Note that i've taken the liberty to return the color red if your tag isn't true. I've also removed the == True part, it's in scripting python's syntax which is different from the expression syntax, you only need one = and it's redundant if your tag is already a boolean.
Also, toColor allows you to use the name of the color so you could use:

If({[default]PLC STATUS/M-1 PLC-5/Forces Enabled S2:1_8},toColor("yellow"),
toColor("red"))