Question on defining a 'color' variable

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question on defining a 'color' variable

jessaskey

Hiya Folks…

 

Im doing some automation generating OpenSCAD files and I wanted to have my output .png objects colored based upon a value of another parameter…  In my simplistic dream world, I would do like below and put the logic in the .scad file. I could also try and deal with another step in my templating where I output the “myvar” variable directly, but I would prefer not to for various reasons if it is indeed possible in .scad.

 

The ‘_myvar’ parameter may have values of 1, 2 and 3 for example…

 

Im trying to do something like below, but I clearly have scope issues and Im confused…   Is there a way to accomplish this concept or am I barking up an empty tree?

 

_color = “black”;

if (_myvar == 1) _color = “red”;

else if (_myvar == 2) _color=”green”;

else if (_myvar == 3) _color = “blue”;

 

color(_color) cube();

 

NOTE: in this simple example, I would just put the ‘color(_color) cube()’ into the if/elseif blocks, but of course, in the real world, that is much more complex rendering so it doesn’t fit well inside of a large if/elseif tree.

 

Thank you…. Jess

 


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Question on defining a 'color' variable

doug.moen
_color =
    (_myvar == 1) ? "red"
  : (_myvar == 2) ? "green"
  : (_myvar == 3) ? "blue"
  : "black";

color(_color) cube();

On 23 January 2018 at 14:36, Jess Askey <[hidden email]> wrote:

Hiya Folks…

 

Im doing some automation generating OpenSCAD files and I wanted to have my output .png objects colored based upon a value of another parameter…  In my simplistic dream world, I would do like below and put the logic in the .scad file. I could also try and deal with another step in my templating where I output the “myvar” variable directly, but I would prefer not to for various reasons if it is indeed possible in .scad.

 

The ‘_myvar’ parameter may have values of 1, 2 and 3 for example…

 

Im trying to do something like below, but I clearly have scope issues and Im confused…   Is there a way to accomplish this concept or am I barking up an empty tree?

 

_color = “black”;

if (_myvar == 1) _color = “red”;

else if (_myvar == 2) _color=”green”;

else if (_myvar == 3) _color = “blue”;

 

color(_color) cube();

 

NOTE: in this simple example, I would just put the ‘color(_color) cube()’ into the if/elseif blocks, but of course, in the real world, that is much more complex rendering so it doesn’t fit well inside of a large if/elseif tree.

 

Thank you…. Jess

 


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Question on defining a 'color' variable

Parkinbot
In reply to this post by jessaskey
I would use array syntax, like this:

myvar = 1;
color_ = ["red", "green", "black"];
color(color_[myvar]) cube();

or if you want to use a more general conditional scheme, you can use the
conditional operator:

myvar = 1;
color_ = myvar==1?"red":myvar==2?"green": "black";
color(color_) cube();  

The rule is: There are only constants in OpenSCAD. You can define, but not
alter the value of a constant.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Question on defining a 'color' variable

jessaskey
Thank you Doug and Rudolf... that was exactly what I was missing!

-----Original Message-----
From: Discuss [mailto:[hidden email]] On Behalf Of Parkinbot
Sent: Tuesday, January 23, 2018 12:56 PM
To: [hidden email]
Subject: Re: [OpenSCAD] Question on defining a 'color' variable

I would use array syntax, like this:

myvar = 1;
color_ = ["red", "green", "black"];
color(color_[myvar]) cube();

or if you want to use a more general conditional scheme, you can use the conditional operator:

myvar = 1;
color_ = myvar==1?"red":myvar==2?"green": "black";
color(color_) cube();  

The rule is: There are only constants in OpenSCAD. You can define, but not alter the value of a constant.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org