Difference between revisions of "Color conversion scripts"

From Second Life Wiki
Jump to navigation Jump to search
(New page: The following functions convert between color in Red Green Blue (RGB) format and color in Hue Saturation Value (HSV)format. The functions are based on c algorithms from [http://www.cs....)
 
(Color conversion script for conversion to and from RGB to HSV)
Line 1: Line 1:
The following functions convert between [[color]] in Red Green Blue (RGB) format and color in Hue Saturation Value (HSV)format.
{{LSL Header
The functions are based on c algorithms from [http://www.cs.rit.edu/~ncs/color/t_convert.html C color conversion] but required some debugging as the functions there are no particularly robust.  For a discussion of HSV color format please see the wikipedia entry at  [http://en.wikipedia.org/wiki/HSV_color_space HSV Color]
}}{{#vardefine:header_title|Color conversion scripts
}}
 
The following functions convert between LSL [[color]] in Red Green Blue (RGB) format and color in Hue Saturation Value (HSV) format.
The functions are based on "c" algorithms from [http://www.cs.rit.edu/~ncs/color/t_convert.html c color conversion] but required some debugging and extensive re-working to fit them in to LSL.  For a discussion of HSV color format please see the wikipedia entry at  [http://en.wikipedia.org/wiki/HSV_color_space HSV Color]


<pre>
<pre>
// by Sally LaSalle, code released to the public domain under GNU license.
// takes an RGB color as a vector, with range <float R[0,1], float G[0,1], float B[0,1]>
// returns a vector with HSV ranged from <float H[0,360], float S[0,1],  float V[0,1]>
// H ranges smoothly from Red=0, Yellow=60, Green=120, Cyan=180, Blue=240, Violet=300 and back to Red
vector RGBtoHSV( vector rgb )
{
    float R = rgb.x;
    float G = rgb.y;
    float B = rgb.z;
   
    float H;
    float S;
    float V;
   
    list rgbList = [R, G, B]; // list used to get min and max
       
    float min;
    float max;
    float achromatic;  // =1 if R=G=B
    float delta;
   
    vector hsv;  // the return HSV vector
    min = llListStatistics(LIST_STAT_MIN, rgbList); //MIN of ( R, G, B );
    max = llListStatistics(LIST_STAT_MAX, rgbList); //MAX of ( R, G, B );
    if (R==G && G==B)
achromatic= 1;  // it is a shade of grey, white or black
else
achromatic= 0;
    V = max;                    // V = brightness Value form 0 to 1
    delta = max - min;
    if( max != 0 )
        S = delta / max;        // S = saturation from 0 to 1
    else {
        // R = G = B = 0        // S = 0, V = 0, H = 0
        S = 0;                 
        V = 0;                 
        H = 0;
       
        hsv.x = H;
        hsv.y = S;
        hsv.z = V;
        return hsv;            //H = S = V = 0
    }


    if (achromatic == 1)
        H = 0;
    else if( R == max )
        H = 0 + ( G - B ) / delta;    // between red & yellow
    else if( G == max )
        H = 2 + ( B - R ) / delta;    // between yellow & cyan
    else
        H = 4 + ( R - G ) / delta;    // between cyan & red
   


    H *= 60;                      // H is traditionally a figure between 0 and 360 degrees
    if( H < 0 )
        H += 360;


    hsv.x = H;
    hsv.y = S;
    hsv.z = V;
    return hsv;
}
</pre>
</pre>

Revision as of 11:43, 21 August 2007

The following functions convert between LSL color in Red Green Blue (RGB) format and color in Hue Saturation Value (HSV) format. The functions are based on "c" algorithms from c color conversion but required some debugging and extensive re-working to fit them in to LSL. For a discussion of HSV color format please see the wikipedia entry at HSV Color

// by Sally LaSalle, code released to the public domain under GNU license.

// takes an RGB color as a vector, with range <float R[0,1], float G[0,1], float B[0,1]>
// returns a vector with HSV ranged from <float H[0,360], float S[0,1],  float V[0,1]>
// H ranges smoothly from Red=0, Yellow=60, Green=120, Cyan=180, Blue=240, Violet=300 and back to Red

vector RGBtoHSV( vector rgb )
{
    float R = rgb.x;
    float G = rgb.y;
    float B = rgb.z;
    
    float H;
    float S;
    float V;
    
    list rgbList = [R, G, B]; // list used to get min and max
        
    float min;
    float max;
    float achromatic;  // =1 if R=G=B
    float delta;
    
    vector hsv;  // the return HSV vector

    min = llListStatistics(LIST_STAT_MIN, rgbList); //MIN of ( R, G, B );
    max = llListStatistics(LIST_STAT_MAX, rgbList); //MAX of ( R, G, B );
    if (R==G && G==B)
		achromatic= 1;  // it is a shade of grey, white or black
	else
		achromatic= 0;

    V = max;                    // V = brightness Value form 0 to 1
    delta = max - min;

    if( max != 0 )
        S = delta / max;        // S = saturation from 0 to 1
    else {
        // R = G = B = 0        // S = 0, V = 0, H = 0
        S = 0;                  
        V = 0;                  
        H = 0;
        
        hsv.x = H;
        hsv.y = S;
        hsv.z = V;
        return hsv;             //H = S = V = 0
    }

    if (achromatic == 1)
        H = 0;
    else if( R == max )
        H = 0 + ( G - B ) / delta;    // between red & yellow
    else if( G == max )
        H = 2 + ( B - R ) / delta;    // between yellow & cyan
    else
        H = 4 + ( R - G ) / delta;    // between cyan & red
    

    H *= 60;                	      // H is traditionally a figure between 0 and 360 degrees
    if( H < 0 )
        H += 360;

    hsv.x = H;
    hsv.y = S;
    hsv.z = V;
    return hsv;
}