Strip R, G, and B Components from Color as String in Expression

I have a color String that I’m using to format the color of objects (i.e. “RRR,GGG,BBB”). How can I split this into an individual RRR, GGG, or BBB value using Ignition’s expression language?

I believe I have answered my own question.

red: split({Template.color},",")[0,0]

green: split({Template.color},",")[1,0]

blue: split({Template.color},",")[2,0]

yes, no other way

There is almost always another way. :crazy_face:

red:

left({Template.color},
     indexOf({Template.color}, ',')
    )

green:

substring({Template.color}, 
          indexOf({Template.color}, ',') + 1, 
          lastIndexOf({Template.color}, ',')
         )

blue:

substring({Template.color}, 
          lastIndexOf({Template.color}, ',') + 1)

I never said it was a good way...

That said, if you have many templates, you may want to consider using a custom property to hold a single dataset created by the split(), as your current method makes three datasets, once for each expression.

3 Likes

Or, since this is sort of begging the question: Why do you need to split RGB values? Why not have 3 separate custom properties for RGB, and use an expression (color) to create the actual Color value when you need it?

3 Likes

Using cell update on a style customizer to easily change the color of a fading animation (allow user to change the color using the color picker).

However, I’m running into a bug now with finalizing the bindings. Here’s the setup:

Template named test

String tag with value of 255,0,0 bound to internal property svgColor

Rectangle component named Rectangle

String properties on the rectangle of color_R, color_G, and color_B that are derived from from the internal svgColor property using toInteger(split({test.svgColor},",")[0,0]) / [1,0] / [2,0] expressions, respectively.

Initially, all of the bindings seem to work. However, once I save, close, and reopen the template I’m getting some errors with color_G and color_B. They throw this error:

(but not color_R?)

Checking the dataset manually by making a temporary dataset property and binding to an expression of split({[default]svgColor},","), I can see that I get 3 rows, 1 column.

It seems like the bindings still work, despite the errors.

What’s very odd, is if I remove the binding of the tag to svgColor (and leave the rest of the bindings the same) I no longer get the errors.

That was my initial thought coming from using Excel so much :sweat_smile:

I have found that the toColor() function works in the Cell Update binding Value column. So I’m able to do a toColor(<color_tag>+",150") which is a much nicer solution