Color changer - Red-to-Green

About advanced scenes, and the Thyme scripting language used in Algodoo.

Color changer - Red-to-Green

Postby blahblah200 » Thu Oct 15, 2009 6:52 pm

I was wondering how you could get the colour of an object to slowly change from red to green

I thought this would work, but it seems it doesn't;
Code: Select all
(e)=>{
    e.this.color(1) == e.this.color(0) - 0.01;     e.this.color(2) == e.this.color(1) + 0.01 }
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Color changer - Red-to-Green

Postby standardtoaster » Thu Oct 15, 2009 7:03 pm

How's this for you?
Rating: rated 5.3
Filesize: 17.6 kB
Comments: 2
Ratings: 2
download


Code: Select all
(e)=>{
    e.this.color = [e.this.color(0) - 0.01, e.this.color(1) + 0.01, e.this.color(2), e.this.color(3)];     e.this.color(0) <= 0 && e.this.color(1) >= 1 ? {e.this.color = [0, 1, e.this.color(2), e.this.color(3)]} : {} }


This takes the first value of the color and continually subtracts 0.01 from it until it gets to or below 0 and then it stops subtracting. This also takes the second value of the color and continually adds until it gets to or above 1.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Color changer - Red-to-Green

Postby KarateBrot » Thu Oct 15, 2009 11:23 pm

Just to mention:
If you want to give a variable a value you have to use "=" and not "==". "==" is just to check if the variable is equal something.

In other words
== - is equal
= - is defined as


Another question:

So you want to let the color to change from red to green DIRECTLY like this:
Image

or do you want it to go through THE WHOLE COLOR SPECTRUM until it reaches green like this:
Image

For a direct change standardtoasters code is very cool and if you want the spectrum I have another code for you.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Color changer - Red-to-Green

Postby blahblah200 » Sat Oct 24, 2009 11:53 am

The directly red-to-green worked fine, but I am interested about going through the whole spectrum - what is the code for that?
btw thanks for the code! :clap:
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Color changer - Red-to-Green

Postby KarateBrot » Sat Oct 24, 2009 1:55 pm

in general it's not very different from the direct change but instead of decrease red and increase green at the same time it first increases green and after that it decreases red. that's the secret :D

1)
make a new variable for example "scene.my.count = 0"

2)
in the OnCollide you have to type
Code: Select all
(e)=>{scene.my.count = scene.my.count + 0.5;     scene.my.count > 100 ? {scene.my.count = 100} : {} }


3)
for the color of the object you have to type
Code: Select all
{scene.my.count <= 50 ? {[1, scene.my.count / 50, 0, 1]} : {[ - scene.my.count / 50 + 2, 1, 0, 1]}}


Now you're good to go to start the simulation
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Color changer - Red-to-Green

Postby blahblah200 » Sat Oct 24, 2009 3:54 pm

Can you tell me how to start at red and go through the whole colour spectrum until it reaches violet, and then goes back again?
Thanks again :thumbup: :thumbup: :clap:
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Color changer - Red-to-Green

Postby KarateBrot » Sat Oct 24, 2009 11:00 pm

I can do that but first I have to translate it to Thyme so be a bit patient ;) I only need to extend the code a bit but I have to make the colors increase/decrease in the right order.

- - - - - - - - - -

That's how you can go through the whole spectrum:

1)
Make a new Variable like "scene.my.variable = 0"

2)
Put this code into "color =" in the scripting menu of the object
Code: Select all
{scene.my.count >= 250 ? {[1, 0, -(scene.my.count - 250) / 50 + 1, 1]} : {scene.my.count >= 200 ? {[(scene.my.count - 200) / 50, 0, 1, 1]} : {scene.my.count >= 150 ? {[0, -(scene.my.count - 150) / 50 + 1, 1, 1]} : {scene.my.count >= 100 ? {[0, 1, (scene.my.count - 100) / 50, 1]} : {scene.my.count >= 50 ? {[-(scene.my.count - 50) / 50 + 1, 1, 0, 1]} : {scene.my.count >= 0 ? {[1, scene.my.count / 50, 0, 1]} : {}}}}}}}


3)
Put this code into "OnCollide =" or "OnLaserHit =" in the scripting menu of the object
Code: Select all
(e)=>{scene.my.count = scene.my.count + 0.5;     scene.my.count >= 300 ? {scene.my.count = 0} : {};     scene.my.count < 0 ? {scene.my.count = 299.5} : {} }


- - - - - - - - - -

With this code you can go through the whole spectrum but you don't stop at violet and go back. It's looped so it goes like this: red => yellow => green => light blue => blue => violet => red and then it starts again.
But I can also make it stop at violet and then going back.

- - - - - - - - - -

If you want to make it stop at violet and going back to red you have to make a new variable (for example scene.my.backwards = true) and use this OnCollide code instead of 3)
Code: Select all
(e)=>{ scene.my.backwards ? {scene.my.count = scene.my.count - 0.5} : {scene.my.count = scene.my.count + 0.5};     scene.my.count >= 250 ? {scene.my.backwards = true} : {};     scene.my.count <= 0 ? {scene.my.backwards = false} : {} }
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Color changer - Red-to-Green

Postby algadoodle123 » Wed Feb 24, 2010 3:20 am

hi, i made a code to change from red to blue and back, it is for phun, so no lazors :cry:
i loved the demo of algodoo... viewtopic.php?f=27&t=1668

but now i have to face reality and give you my faulty script

Code: Select all
(e)=>{e.other.color == [1.0, 0.0, 1.0, 0.0] ? {e.other.color = [e.other.color(0) <= 1.0 ? {1.0} : {e.other.color(0) + 0.0099999998}, 0.0, e.other.color(2) >= 0.0 ? {0.0} : {e.other.color(2) - 0.0099999998}, 1.0]} : {e.other.color = [e.other.color(0) <= 0.0 ? {0.0} : {e.other.color(0) - 0.0099999998}, 0.0, e.other.color(2) >= 1.0 ? {1.0} : {e.other.color(2) + 0.0099999998}, 1.0]}}


in the console it says that i had already declared the variable "color" in this scope

since i used no scene.my variables, i have no idea wat that means, so i cannot troubleshoot it myself :'(
**/\ /\
>( ' ; ' )<***<---------- cat
*((")("))
i'm right-side-up
uʍop-ǝpısdn ɯ,ı
http://www.sherv.net/flip.html
User avatar
algadoodle123
 
Posts: 66
Joined: Tue Sep 08, 2009 6:04 pm

Re: Color changer - Red-to-Green

Postby KarateBrot » Wed Feb 24, 2010 3:53 am

maybe try not to make if-structures in the color array. Or you made a declare sign ( := ) somewhere. because color is an already declared variable you will get an error message for trying to declare color.
oh and by the way color changing can be achieved a lot easier. when i made my function above i didn't know of the useful color functions in algodoo.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Color changer - Red-to-Green

Postby algadoodle123 » Wed Feb 24, 2010 5:52 am

well then, what are any easier methods. i would love to find out, as this code seemed a bit long just to change colors. even if it does not work in phun, i will stil use it when/if i buy algodoo.

the current script i am using that yeilds no result is this:
Code: Select all
(e)=>{
    e.other.color == [0.0, 0.0, 1.0, 1.0] ? {         e.other.color(0) >= 1.0 ? {1.0} : {scene.my.r = (e.other.color(0) + 0.0099999998)};         e.other.color(2) <= 0.0 ? {0.0} : {scene.my.b = (e.other.color(0) - 0.0099999998)};         e.other.color = [scene.my.r, 0.0, scene.my.b, 1.0]     } : {         e.other.color(0) <= 1.0 ? {1.0} : {scene.my.r = (e.other.color(0) - 0.0099999998)};         e.other.color(2) >= 0.0 ? {0.0} : {scene.my.b = (e.other.color(0) + 0.0099999998)};         e.other.color = [scene.my.r, 0.0, scene.my.b, 1.0]     } }
**/\ /\
>( ' ; ' )<***<---------- cat
*((")("))
i'm right-side-up
uʍop-ǝpısdn ɯ,ı
http://www.sherv.net/flip.html
User avatar
algadoodle123
 
Posts: 66
Joined: Tue Sep 08, 2009 6:04 pm

Re: Color changer - Red-to-Green

Postby KarateBrot » Wed Feb 24, 2010 7:27 am

The functions are already available in phun.

math.rgb2hsl
math.rgb2hsv
math.hsl2rgb
math.hsv2rgb

http://en.wikipedia.org/wiki/HSL_and_HSV

- - - - -

Your function doesn't work because there is at least one mistake.
Code: Select all
e.other.color(2) >= 0.0 ? {0.0} : {scene.my.b = (e.other.color(0) + 0.0099999998)};

when the other color(2) is bigger or equal 0 then do 0 (<= that doesn't make sense) else do...
You can't just type in {0.0}. You also have to tell phun what it should do with this number. 2 examples:
Code: Select all
e.other.color(2) >= 0.0 ? {scene.my.whatever = 0.0} : {scene.my.b = (e.other.color(0) + 0.0099999998)};

e.other.color(2) >= 0.0 ? { } : {scene.my.b = (e.other.color(0) + 0.0099999998)};

the first one changes scene.my.whatever to 0.0 and the second one doesn nothing if e.other.color(2) is bigger or equal 0
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest