Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Hue/staturation fading on command

Jesrad Seraph
Nonsense
Join date: 11 Dec 2004
Posts: 1,463
01-03-2005 09:50
I composed this script for an outfit, and since it has evolved enough that I'm now satisfied with it I figured I'd let others profit from it.

This script fades hue gradually over time, going from red to green to blue and back to red for another cycle. Transition is very smooth and color vectors are generated with cos and sin functions (I think a list of vectors would be too messy for little - if any - improvement).

It can also freeze on a specific color, alter saturation of the tint and set transparency on command.

I consider it final version, so if I gave you a "Surprise - see contents" ask me for an update ;)

CODE
// linkset color variation over time
// can be random or sequential in hue
// random is NOT advised as it strains the Sim's resources

// Author: Jesrad Seraph
// use, modify and redistribute freely


// commands:
// /23 toggle => toggles between sequential and random color change
// /23 reset => sets color back to pure red, useful to sync multiple items
// /23 start => starts the color change effect
// /23 stop => freezes the color change effect
// /23 clear => stops the effect and sets color to white
// /23 blue => stops the effect and sets color to pure blue
// /23 green => stops the effect and sets color to pure green
// /23 red => stops the effect and sets color to pure red
// /23 saturate => augments the saturation up 10%
// /23 desaturate => decreases saturation by 10%
// /23 faster => makes the effect twice as fast (WARNING may cause lag on the whole Sim at high speeds)
// /23 slower => makes the effect half as fast
// /23 alpha => makes the object 10% more transparent
// /23 dealpha => makes the object 10% less transparent
// /23 transparent => makes the object fully transparent
// /23 opaque => makes the object fully opaque
// /23 change => manually triggers the color change
// saturation control only affects the sequential fade effect

key owner;

integer ISSEQUENTIAL = 1;

integer listen_handle;

float colordelay = 2.0;
// this is the default color update frequency

float unsaturation = 0.0;
float opacity = 1.0;

vector currentcolor = <1.0, 0.0, 0.0>;
// switch to pure red on reset

integer currentangle = 0;
integer currentdir = 0;
// 0 for red to green
// 1 for green to blue
// 2 for blue to red

integer angle_increment = 3;
// determines the number of steps around the hue wheel.
// the lower the more steps, doesn't go over 90
// Always use a factor of 90 for better results
// 3 or 5 are good values



colorupdate()
{
// change current color
if (ISSEQUENTIAL < 0)
{
// set a random color:
currentcolor = <llFrand(1),llFrand(1),llFrand(1)>;
}
else
{
// rotate the color vector along X Y Z to vary between red, green and blue
if (currentangle >= 90)
{
currentdir += 1;
currentangle = 0;
if ((currentdir >= 3) || (currentdir < 0))
currentdir = 0;
}
currentangle += angle_increment;
}
}

colorchange()
{
float float_angle = ((float)currentangle) * DEG_TO_RAD;
float sinelement = llSin(float_angle) * (1.0 - unsaturation) + unsaturation;
float coselement = llCos(float_angle) * (1.0 - unsaturation) + unsaturation;

if (ISSEQUENTIAL > 0)
{
if (currentdir == 0)
{
currentcolor = <coselement, sinelement, unsaturation>;
}
else if (currentdir == 1)
{
currentcolor = <unsaturation, coselement, sinelement>;
}
else if (currentdir == 2)
{
currentcolor = <sinelement, unsaturation, coselement>;
}
}

llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}

default
{
state_entry()
{
llListenRemove(listen_handle);
owner=llGetOwner();
listen_handle = llListen(23,"",owner,"");
}

timer()
{
colorupdate();
colorchange();
}

listen(integer channel, string name, key id, string message)
{
if (message == "toggle")
{
ISSEQUENTIAL *= -1;
}
else if (message == "reset")
{
currentcolor = <1.0, 0.0, 0.0>;
unsaturation = 0.0;
currentangle = 0;
currentdir = 0;
ISSEQUENTIAL = 1;
colordelay = 2.0;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
llSetTimerEvent(colordelay);
}
else if (message == "start")
{
llSetTimerEvent(colordelay);
}
else if (message == "stop")
{
llSetTimerEvent(0);
}
else if (message == "clear")
{
llSetTimerEvent(0);
currentcolor = <1.0, 1.0, 1.0>;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}
else if (message == "blue")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 2;
colorchange();
}
else if (message == "green")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 1;
colorchange();
}
else if (message == "red")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 0;
colorchange();
}
else if (message == "black")
{
llSetTimerEvent(0);
currentcolor = <0.0, 0.0, 0.0>;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}
else if (message == "faster")
{
colordelay /= 2.0;
if (colordelay < 0.25)
colordelay = 0.25;
}
else if (message == "slower")
{
colordelay *= 2.0;
}
else if (message == "saturate")
{
unsaturation -= 0.1;
if (unsaturation < 0.0)
unsaturation = 0.0;
colorchange();
}
else if (message == "desaturate")
{
unsaturation += 0.1;
if (unsaturation > 1.0)
unsaturation = 1.0;
colorchange();
}
else if (message == "alpha")
{
opacity -= 0.1;
if (opacity < 0.0)
opacity = 0.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "dealpha")
{
opacity += 0.1;
if (opacity > 1.0)
opacity = 1.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "transparent")
{
opacity = 0.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "opaque")
{
opacity = 1.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "change")
{
colorupdate();
colorchange();
}
}
}


Here's the snippet of code to put into linked prims so that the main script can alter the color and opacity of these prims:

CODE

state default
{
link_message(integer sender_num, integer num, string str, key id)
{
if(num == 105)
llSetColor((vector) str, ALL_SIDES);
if(num == 106)
llSetAlpha((float) str, ALL_SIDES);
}
}
Laure Seraph
Registered User
Join date: 30 Dec 2004
Posts: 3
01-08-2005 12:15
Thanks to Ksharra Manimal for finding a bug, here's the updated version:

CODE
// linkset color variation over time
// can be random or sequential in hue
// random is NOT advised as it strains the Sim's resources

// Author: Jesrad Seraph
// use, modify and redistribute freely


// commands:
// /23 toggle => toggles between sequential and random color change
// /23 reset => sets color back to pure red, useful to sync multiple items
// /23 start => starts the color change effect
// /23 stop => freezes the color change effect
// /23 clear => stops the effect and sets color to white
// /23 blue => stops the effect and sets color to pure blue
// /23 green => stops the effect and sets color to pure green
// /23 red => stops the effect and sets color to pure red
// /23 saturate => augments the saturation up 10%
// /23 desaturate => decreases saturation by 10%
// /23 faster => makes the effect twice as fast (WARNING may cause lag on the whole Sim at high speeds)
// /23 slower => makes the effect half as fast
// /23 alpha => makes the object 10% more transparent
// /23 dealpha => makes the object 10% less transparent
// /23 transparent => makes the object fully transparent
// /23 opaque => makes the object fully opaque
// /23 change => manually triggers the color change
// saturation control only affects the sequential fade effect

key owner;

integer ISSEQUENTIAL = 1;

integer listen_handle;

float colordelay = 2.0;
// this is the default color update frequency

float unsaturation = 0.0;
float opacity = 1.0;

vector currentcolor = <1.0, 0.0, 0.0>;
// switch to pure red on reset

integer currentangle = 0;
integer currentdir = 0;
// 0 for red to green
// 1 for green to blue
// 2 for blue to red

integer angle_increment = 3;
// determines the number of steps around the hue wheel.
// the lower the more steps, doesn't go over 90
// Always use a factor of 90 for better results
// 3 or 5 are good values



colorupdate()
{
// change current color
if (ISSEQUENTIAL < 0)
{
// set a random color:
currentcolor = <llFrand(1),llFrand(1),llFrand(1)>;
}
else
{
// rotate the color vector along X Y Z to vary between red, green and blue
if (currentangle >= 90)
{
currentdir += 1;
currentangle = 0;
if ((currentdir >= 3) || (currentdir < 0))
currentdir = 0;
}
currentangle += angle_increment;
}
}

colorchange()
{
float float_angle = ((float)currentangle) * DEG_TO_RAD;
float sinelement = llSin(float_angle) * (1.0 - unsaturation) + unsaturation;
float coselement = llCos(float_angle) * (1.0 - unsaturation) + unsaturation;

if (ISSEQUENTIAL > 0)
{
if (currentdir == 0)
{
currentcolor = <coselement, sinelement, unsaturation>;
}
else if (currentdir == 1)
{
currentcolor = <unsaturation, coselement, sinelement>;
}
else if (currentdir == 2)
{
currentcolor = <sinelement, unsaturation, coselement>;
}
}

llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}

default
{
state_entry()
{
llListenRemove(listen_handle);
owner=llGetOwner();
listen_handle = llListen(23,"",owner,"");
}

timer()
{
colorupdate();
colorchange();
}

listen(integer channel, string name, key id, string message)
{
if (message == "toggle")
{
ISSEQUENTIAL *= -1;
}
else if (message == "reset")
{
currentcolor = <1.0, 0.0, 0.0>;
unsaturation = 0.0;
currentangle = 0;
currentdir = 0;
ISSEQUENTIAL = 1;
colordelay = 2.0;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
llSetTimerEvent(colordelay);
}
else if (message == "start")
{
llSetTimerEvent(colordelay);
}
else if (message == "stop")
{
llSetTimerEvent(0);
}
else if (message == "clear")
{
llSetTimerEvent(0);
currentcolor = <1.0, 1.0, 1.0>;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}
else if (message == "blue")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 2;
colorchange();
}
else if (message == "green")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 1;
colorchange();
}
else if (message == "red")
{
llSetTimerEvent(0);
unsaturation = 0.0;
currentangle = 0;
currentdir = 0;
colorchange();
}
else if (message == "black")
{
llSetTimerEvent(0);
currentcolor = <0.0, 0.0, 0.0>;
llSetColor(currentcolor,ALL_SIDES);
llMessageLinked(LINK_SET,105,(string)currentcolor,NULL_KEY);
}
else if (message == "faster")
{
colordelay /= 2.0;
if (colordelay < 0.25)
colordelay = 0.25;
llSetTimerEvent(colordelay);
}
else if (message == "slower")
{
colordelay *= 2.0;
llSetTimerEvent(colordelay);
}
else if (message == "saturate")
{
unsaturation -= 0.1;
if (unsaturation < 0.0)
unsaturation = 0.0;
colorchange();
}
else if (message == "desaturate")
{
unsaturation += 0.1;
if (unsaturation > 1.0)
unsaturation = 1.0;
colorchange();
}
else if (message == "alpha")
{
opacity -= 0.1;
if (opacity < 0.0)
opacity = 0.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "dealpha")
{
opacity += 0.1;
if (opacity > 1.0)
opacity = 1.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "transparent")
{
opacity = 0.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "opaque")
{
opacity = 1.0;
llSetAlpha(opacity, ALL_SIDES);
llMessageLinked(LINK_SET,106,(string)opacity,NULL_KEY);
}
else if (message == "change")
{
colorupdate();
colorchange();
}
}
}
Kathmandu Gilman
Fearful Symmetry Baby!
Join date: 21 May 2004
Posts: 1,418
02-06-2005 01:39
Can this somehow be used to change an avatar's skin colors? I am absolutly clueless about scripting but I'd like a script that could change the skin color on an av as you describe here. Any help would be great.
Upshaw Underhill
Techno-Hobbit
Join date: 13 Mar 2003
Posts: 293
02-06-2005 10:15
Sadly, no, Kat.

Only prims.

I would love an llSetAvatarParams()! :D

L8r,
UU
DNA Prototype
Mad Scientist
Join date: 8 Aug 2004
Posts: 179
02-06-2005 11:07
Unfortunately, not yet possible.

DNA
_____________________
DNA DEMENTED CIRCUITRY LIGHTING AND DJ SUPPY
SL's finest speakers, DJ gear, lasers, killer club lighting & effects.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
02-06-2005 12:50
:( scripts can't effect changes on the avatars body.

So you can't use this to change skin color unless your av is all prims.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
02-06-2005 14:49
http://secondlife.com/badgeo/wakka.php?wakka=llSetLinkAlpha
http://secondlife.com/badgeo/wakka.php?wakka=llSetLinkColor
Enabran Templar
Capitalist Pig
Join date: 26 Aug 2004
Posts: 4,506
05-04-2005 20:04
This is fantastic. Exactly what I needed. Thank you for releasing this!
Nectere Niven
Gadget Junky
Join date: 12 Jan 2007
Posts: 211
05-05-2008 14:28
I realize this is really really cold but I would like to use something like this that is automatic, I dont want to tell it to do anything and I only want it to fade from red to blue to black and the back to red (rinse repeat)

I tried hacking my way through it removing the listens and if I say this do that but I ended up completely breaking it instead. Any ideas what or where I should be careful to preserve the structure but remove the commands? (so it just does)
_____________________
Every second of your life is a moment of opportunity to make it better than the last...