Difference between revisions of "GradientValue"

From Second Life Wiki
Jump to navigation Jump to search
(some general readability improvements and some for non-widescreens when viewing the page specifically)
m (<lsl> tag to <source>)
 
(7 intermediate revisions by one other user not shown)
Line 2: Line 2:
|func=GradientValue
|func=GradientValue
|mode=user
|mode=user
|p1_type=vector|p1_name=beginColor|p1_desc=The Starting Vector Color
|p1_type=vector|p1_name=start|p1_desc=Gradient start color
|p2_type=vector|p2_name=endColor|p2_desc=The Ending Vector Color
|p2_type=vector|p2_name=end|p2_desc=Gradient end color
|p3_type=float|p3_name=percentage|p3_desc=The Percentage from beginColor to endColor.
|p3_type=float|p3_name=percentage|p3_desc=Percentage in the range (0.0 to 100.0) from start to end
|func_desc=Returns a vector between '''beginColor''' and '''endColor''' depending on the percentage param.
|return_type=vector|return_text=that is the color vector between {{LSLP|start}} and {{LSLP|end}} depending upon {{LSLP|percentage}} if the vector between can be calculated, else returns <code>{{LSL VR|-1|-1|-1}}</code>.
|func_footnote='''Percentage''' goes from 0.0 to 1.0 where 0.5 is 50%.
|func_footnote
|caveats
|spec=
|spec=
<lsl>
<source lang="lsl2">
//Created by Ugleh Ulrik
// Created by Ugleh Ulrik


vector GradientValue(vector beginColor, vector endColor, float percentage)
vector GradientValue(vector start, vector end, float percentage)
{
{
     // If vectors are in SL format (1.0 max) then do a multiplication of 255.
     list colorParams = [start.x, start.y, start.z, end.x, end.y, end.z];
    // beginColor = beginColor * 255;
    // endColor = endColor * 255;


     float red = beginColor.x + (integer)(percentage * (endColor.x - beginColor.x));
     if (percentage < 0.0
    float green = beginColor.y + (integer)(percentage * (endColor.y - beginColor.y));
        || 100.0 < percentage
     float blue = beginColor.z + (integer)(percentage * (endColor.z - beginColor.z));
        || llListStatistics(LIST_STAT_MIN, colorParams) < 0.0
        || 1.0 < llListStatistics(LIST_STAT_MAX, colorParams))
     {
        return <-1, -1, -1>;
    }


     return <red,green,blue>/255;
    percentage /= 100.0;// convert to decimal for further use
 
     return start + percentage*(end - start);
}
}
</lsl>
</source>
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=
|examples=
<lsl>
<source lang="lsl2">
//Created by Ugleh Ulrik
vector GradientValue(vector start, vector end, float percentage)
{
    list colorParams = [start.x, start.y, start.z, end.x, end.y, end.z];


vector GradientValue (vector beginColor, vector endColor, float percentage)
    if (percentage < 0.0
{
        || 100.0 < percentage
    // If vectors are in SL format (1.0 max) then do a multiplication of 255.
        || llListStatistics(LIST_STAT_MIN, colorParams) < 0.0
     // beginColor = beginColor * 255;
        || 1.0 < llListStatistics(LIST_STAT_MAX, colorParams))
     // endColor = endColor * 255;
     {
        return <-1, -1, -1>;
     }


     float red = beginColor.x + (integer)(percentage * (endColor.x - beginColor.x));
     percentage /= 100.0;
    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;
     return start + percentage*(end - start);
}  
}


default
default
Line 46: Line 52:
     state_entry()
     state_entry()
     {
     {
         // The following vectors are not SL safe, to use SL Safe Vectors you would do
         vector maroon = <0.522, 0.078, 0.294>;
        // vector Red = <1.0, 0.0, 0.0>;
         vector teal  = <0.224, 0.800, 0.800>;
        // 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(maroon, teal, 50.0);
        vector ColorBetween = GradientValue(Red, Blue, 0.50);
//            ColorBetween = <0.373, 0.439, 0.547>


         llSetColor(ColorBetween, ALL_SIDES);
         if (ColorBetween != <-1, -1, -1>)
            llSetColor(ColorBetween, ALL_SIDES);
     }
     }
}
}
</lsl>
</source>
|helpers
|helpers
|notes
|notes
|also
|also
|also_functions
|also_constants=
|also_articles
{{LSL DefineRow||[[PRIM_COLOR]]}}
{{LSL DefineRow||[[CHANGED_COLOR]]}}
|also_functions=
{{LSL DefineRow||[[llGetColor]]}}
{{LSL DefineRow||[[llGetLinkPrimitiveParams]]}}
{{LSL DefineRow||[[llSetColor]]}}
{{LSL DefineRow||[[llSetLinkColor]]}}
|also_articles=
{{LSL DefineRow||[[Color and Scripting]]}}
{{LSL DefineRow||{{LSLGC|Color}}}}
|cat1=Examples
|cat1=Examples
|cat2=User-Defined_Functions
|cat2=User-Defined_Functions

Latest revision as of 14:15, 22 January 2015

Summary

Function: vector GradientValue( vector start, vector end, float percentage );

Returns a vector that is the color vector between start and end depending upon percentage if the vector between can be calculated, else returns <-1, -1, -1>.

• vector start Gradient start color
• vector end Gradient end color
• float percentage Percentage in the range (0.0 to 100.0) from start to end

Specification

//  Created by Ugleh Ulrik

vector GradientValue(vector start, vector end, float percentage)
{
    list colorParams = [start.x, start.y, start.z, end.x, end.y, end.z];

    if (percentage < 0.0
        || 100.0 < percentage
        || llListStatistics(LIST_STAT_MIN, colorParams) < 0.0
        || 1.0 < llListStatistics(LIST_STAT_MAX, colorParams))
    {
        return <-1, -1, -1>;
    }

    percentage /= 100.0;// convert to decimal for further use

    return start + percentage*(end - start);
}

Examples

vector GradientValue(vector start, vector end, float percentage)
{
    list colorParams = [start.x, start.y, start.z, end.x, end.y, end.z];

    if (percentage < 0.0
        || 100.0 < percentage
        || llListStatistics(LIST_STAT_MIN, colorParams) < 0.0
        || 1.0 < llListStatistics(LIST_STAT_MAX, colorParams))
    {
        return <-1, -1, -1>;
    }

    percentage /= 100.0;

    return start + percentage*(end - start);
}

default
{
    state_entry()
    {
        vector maroon = <0.522, 0.078, 0.294>;
        vector teal   = <0.224, 0.800, 0.800>;

        vector ColorBetween = GradientValue(maroon, teal, 50.0);
//             ColorBetween = <0.373, 0.439, 0.547>

        if (ColorBetween != <-1, -1, -1>)
            llSetColor(ColorBetween, ALL_SIDES);
    }
}

See Also

Constants

•  PRIM_COLOR
•  CHANGED_COLOR

Functions

•  llGetColor
•  llGetLinkPrimitiveParams
•  llSetColor
•  llSetLinkColor

Articles

•  Color and Scripting
•  Color