gradientValue

From Second Life Wiki
Revision as of 10:19, 30 September 2012 by Kireji Haiku (talk | contribs) (some general readability improvements and some for non-widescreens when viewing the page specifically)
Jump to navigation Jump to search

Summary

Function: GradientValue( vector beginColor, vector endColor, float percentage );

Returns a vector between beginColor and endColor depending on the percentage param.

• vector beginColor The Starting Vector Color
• vector endColor The Ending Vector Color
• float percentage The Percentage from beginColor to endColor.

Percentage goes from 0.0 to 1.0 where 0.5 is 50%.

Specification

<lsl> //Created by Ugleh Ulrik

vector GradientValue(vector beginColor, vector endColor, float percentage) {

   // If vectors are in SL format (1.0 max) then do a multiplication of 255.
   // beginColor = beginColor * 255;
   // endColor = endColor * 255;
   float red = beginColor.x + (integer)(percentage * (endColor.x - beginColor.x));
   float green = beginColor.y + (integer)(percentage * (endColor.y - beginColor.y));
   float blue = beginColor.z + (integer)(percentage * (endColor.z - beginColor.z));
   return <red,green,blue>/255;

} </lsl> Note in the return we use /255 to convert a RGB value to an SL Safe Vector. The math assumes it is a normal RGB value during the math, so if it isnt you need to uncomment the multiplication lines.

Examples

<lsl> //Created by Ugleh Ulrik

vector GradientValue (vector beginColor, vector endColor, float percentage) {

   // If vectors are in SL format (1.0 max) then do a multiplication of 255.
   // beginColor = beginColor * 255;
   // endColor = endColor * 255;
   float red = beginColor.x + (integer)(percentage * (endColor.x - beginColor.x));
   float green = beginColor.y + (integer)(percentage * (endColor.y - beginColor.y));
   float blue = beginColor.z + (integer)(percentage * (endColor.z - beginColor.z));
   return <red, green, blue>/255;

}

default {

   state_entry()
   {
       // The following vectors are not SL safe, to use SL Safe Vectors you would do
       // vector Red = <1.0, 0.0, 0.0>;
       // vector Blue = <0.0, 0.0, 1.0>;
       vector Red = <255.0, 0.0, 0.0>;
       vector Blue = <0.0, 0.0, 255.0>;
       // I use 0.50 which is half, so I am getting the color between Red and Blue, which is Purple.
       vector ColorBetween = GradientValue(Red, Blue, 0.50);
       llSetColor(ColorBetween, ALL_SIDES);
   }

}

</lsl>