FORUMS: list search recent posts

Making an object change color every 90 degrees

FAQ   •   VIEW ALL
 Making an object change color every 90 degrees on Nov 24, 2013 at 6:41:01 am

I would like to animate a star shape and have it change color every 90 degrees. Would it be necessary to convert to radians first? If my understanding is correct 90 degrees is the equivilant of 1/4 radian. The first part of the expression I am not positive about. That would be converting degrees to radians so any help is very much appreciated.

 Re: Making an object change color every 90 degreeson Nov 24, 2013 at 8:00:28 am

You only need to use radians if you use JavaScript's trig functions. For what you want to do, degrees should be fine. This will increment every 90 degrees

Math.floor(transform.rotation/90);

You didn't mention how you wanted to relate it to color though.

Dan

 Re: Making an object change color every 90 degreeson Nov 24, 2013 at 5:38:14 pm

Thank you. As far as the color scheme would go I would want the color to be as follows:

At a value of 90 degrees red
At a value of 180 degrees blue
At a value of 270 degrees green
At a value of 360 degrees yellow

I want to the process to start over again after the rotation is greater than 360 degrees, if possible. I can get the the expression to work until it goes past 360 degrees then it stays yellow.

There are two expressions below. One is a slider that and the other is for the color.

Probably something I am overlooking is simple. Thank you again for your response.

```For the slider control: r = transform.rotation if (r&lt;0) r*-1 else r This expression will be used to return a positive value even though the rotation is not. This expression will be to create the next expression. Color expression: Math.floor(transform.rotation/90) r = effect("Slider 1")("Slider"); if (r>90 && r&lt;180) [255,0,0,1] else if (r>180 && r&lt;270) [0,255,0,1] else if (r>270 && r&lt;360) [0,0,255,1] else [255,255,0,1] ```

 Re: Making an object change color every 90 degreeson Nov 24, 2013 at 6:10:13 pm

This should work (with positive or negative rotation values):

colors = [[1,0,0,1],[0,0,1,1],[0,1,0,1],[1,1,0,1]]; // red, blue, green, yellow
idx = Math.floor(Math.abs(transform.rotation)/90)%colors.length;
colors[idx]

Dan

 Re: Making an object change color every 90 degreeson Dec 1, 2013 at 7:07:22 pm

Thank you! The expression worked but now I have another question. I would like to add a check box to determine if I want it to change color or have the color set to whatever I choose with the box not checked. I tried the expression below but kept getting an error message.

```change = effect("Change Color")("Checkbox")' if (change == true): { colors = [[1,0,0,1],[0,0,1,1],[0,1,0,1],[1,1,0,1]]; // red, blue, green, yellow idx = Math.floor(Math.abs(transform.rotation)/90)%colors.length; colors[idx] } else { value }```

 Re: Making an object change color every 90 degreeson Dec 1, 2013 at 7:17:08 pm

I think there are just a couple of typos. This should work:

``` change = effect("Change Color")("Checkbox"); if (change){ colors = [[1,0,0,1],[0,0,1,1],[0,1,0,1],[1,1,0,1]]; // red, blue, green, yellow idx = Math.floor(Math.abs(transform.rotation)/90)%colors.length; colors[idx] }else value ```

Dan