How to assign variables in formula editor?

Hi @jan.cosmigo and guys,
I was trying to setup a multi-shading formula.

My goal with this formula is almost exactly the same as multi-shading mode, but without the additional step of setting up the gradient and hopefully follows shading principles via HSB.

I’ve made this wonky formula for the Left Mouse Button in HSB tab.
Hue:
if ((fgh >= 45 and fgh <= 55), fgh, if(fgh < 45, if((fgh - 10) < 0, ((fgh - 10) + 359), (fgh - 10)), if(fgh > 55, if((fgh + 10) > 359, ((fgh + 10) - 359), (fgh + 10)), fgh)))
Sat:
if ((fgs >= 0 and fgs <= 100), fgs, if(fgs < 0, if((fgs - 10) < 0, ((fgs - 10) + 100), (fgs - 10)), if(fgs > 100, if((fgs + 10) > 100, ((fgs + 10) - 100), (fgs + 10)), fgs)))
Bright:
if ((fgb >= 0 and fgb <= 100), fgb, if(fgb < 0, if((fgb - 10) < 0, ((fgb - 10) + 100), (fgb - 10)), if(fgb > 100, if((fgb + 10) > 100, ((fgb + 10) - 100), (fgb + 10)), fgb)))

*Then reverse the operators in the formula for right mouse button to shade.

10 here is a sample constant i want to keep tweaking when looking for the amount/level of H/S/B jumps.

I suspect that since PM’s color palette is forever in indexed mode, it’s bound for this formula not to work 100% of the time… Okay, more like it works 10% of the time with some palettes. And 0% for high contrast palettes, especially 1bit. :sweat_smile:

At first glance, you could already tell how messy this looks. So i was hoping i could assign some equations into variables. I found this in the doc:


But it doesn’t seem to do what i thought it would. Which is assigning stuff to a variable. Hence the question, How?

If you guys wanna improve the formula i just posted, please, feel free and post it here! I would love to know an optimized version. And disclaimer: I’m pretty sure there is, but i have no idea what the theoretical formula and average constant is for changing hues/sat/bright from one step to the next while in context to a certain feel :smile:

This formula was inspired by one of the demos I’ve seen from Pyxeledit where there are already shade and light color options available once you choose a base color.

Could you give an example of it fails to do what you expect?

Usage of the assignment operator should be straight forward.

equation = fgh + 10

something like that. So, if used:

if(equation > 123, a, bg)

whole formula is:
equation = (fgh + 10) if(equation > 123, a, bg)

I get:
‘Undefined token “equation” found at position 0.’

That’s syntactically wrong, the if statement doesn’t belong to the assignment statement, not in that order.

Here’s the link to formula editor on-iine documentation:

As you can see, it indicate that the if statement can take up to three arguments (in case of an else).
So, the above attempt should most likely be rewritten as:

if (equation > 123) then equation = (fgh + 10)

I haven’t tested it, but the syntax is definitely more along those lines.

The problem with your original formulation is that if(equation > 123, a, bg) evaluates to true/false, breaking up the whole statement.

Your suggestion
if (equation > 123) then equation = (fgh + 10)
still gave me: 'Undefined token “equation” found at position 4.’

This makes sense to me because: we can’t compare something to another when they’re not defined yet / still unknown. And even more so, defining it after using it as a check only. It’s like a paradox.

In my formula above, the story is that editing a formula doesn’t allow for new lines. So i assumed Jan made it in a way so that the editor knows if we’re actually stating a space in it, or a new line. But from that experiment, now i know that assumption is wrong and is rather seen as an assignment to the variable.

Any others who know how to use the “=” operator? I’ve checked the formulas that came with PM but none of them use it too.

This wasn’t a literal suggestion, but an attempt to spell out semantically what was wrong in your attempt to attach an IF to an assignment statement. I don’t use formulas, so I’m not into details of the correct syntaxes, but appending an IF to an assignment is incorrect, because the whole IF expressions ultimately evaluates to either true or false (or 1 or 0, if the syntax supports that).

I still haven’t understood what you were trying to accomplish with your first example — if you could spell it out it would be clearer.

In my formula above, the story is that editing a formula doesn’t allow for new lines.

You can always uses parenthesis to nest expressions, if that’s what you’re trying to achieve.

You might benefit from reading the full documentation of muParser.
Possibly, you might be seeking for the ?: ternary operator (which might be supported in PM too, although not mentioned):

https://beltoforion.de/article.php?a=muparser&p=features&s=idDef4#idDef4

Ternary Operators

muParser has built in support for the if then else operator.
It uses lazy evaluation in order to make sure only the necessary branch of the expression is evaluated.

Operator Description Remarks
?: if then else operator C++ style syntax

The PM documentation is not very clear on this point, but I’m starting to think that the only allowed variables types are the predefined ones, as indicated in the various formula context, and that you can’t define arbitrary variable names as values placeholder.

If this was the case, you can only use assignments with the listed variables, which have a direct effect on the corresponding elements.

You can always uses parenthesis to nest expressions

Yeah, the attached formulas in my question is full of this. But i also tried to encapsulate the simple formula i made as an example above in parentheses just to make sure and it still gives the same error.
i.e.:
(equation = (fgh + 10)) if(equation > 123, a, bg)

I still haven’t understood what you were trying to accomplish with your first example — if you could spell it out it would be clearer.

I’m not sure how it didn’t come across as, but i did spell it out in the 2nd sentence:

My goal with this formula is almost exactly the same as multi-shading mode, but without the additional step of setting up the gradient and hopefully follows shading principles via HSB.

You might benefit from reading the full documentation of muParser .

Thank you for the recommendation, but no thanks. I went there already before asking this question. And i found that it won’t be of much use at all. In fact, i highly recommend to everyone not to read it all. You don’t need it just to understand the formula editor.

Possibly, you might be seeking for the ?: ternary operator

No, I’m only looking for the answer to the question on the subject title.

you can only use assignments with the listed variables

I suspect this to be the case too. But I’m hoping to get some clarification on that from @jan.cosmigo