## Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

# Xylor's rgb to hsl

The Librarian
Join date: 4 Nov 2002
Posts: 1,423
06-12-2003 14:12
Here are a couple utility functions for converting
RGB vectors to HSL vectors, and vice versa. HSL is
more useful for color coordinated things, like
creating a pallette.

The included script displays the color of the object
in RGB and HSL whenever the object color is changed.

CODE
`////////////////////////////////////// RGB To HSL and HSL To RGB functions /////////////////////////////////////integer SIDE_TO_CHECK_COLOR = 0;float MetaToColor( float M1, float M2, float Hue ) {    float Color;    // Calculate Color from Meta values    if (Hue < 0.0)        Hue += 1.0;    if (Hue > 1.0)        Hue -= 1.0;            if (Hue * 6.0 < 1.0)        Color = M1 + ((M2 - M1) * Hue * 6.0);    else if (Hue * 2.0 < 1.0)        Color = M2;    else if (Hue * 3.0 < 2.0)        Color = M1 + ((M2 - M1) * ((2.0/3.0) - Hue) *6.0);    else        Color = M1;            return Color;}vector HSL_To_RGB(vector HSL) {    float Hue = HSL.x;    float Sat = HSL.y;    float Lum = HSL.z;        float Red;    float Green;    float Blue;        if (Sat == 0.0) {        Red = Lum;        Green = Lum;        Blue = Lum;    }    else {        float Meta1;        float Meta2;                if (Lum <= 0.5)            Meta2 = Lum * (1.0 + Sat);        else            Meta2 = Lum + Sat - (Lum * Sat);                Meta1 = 2.0 * Lum - Meta2;                Red     = MetaToColor( Meta1, Meta2, Hue +1.0/3.0 );        Green   = MetaToColor( Meta1, Meta2, Hue );        Blue    = MetaToColor( Meta1, Meta2, Hue -1.0/3.0 );    }        return <Red, Green, Blue>;}vector RGB_To_HSL(vector RGB) {    float Red = RGB.x;    float Green = RGB.y;    float Blue = RGB.z;        float Max;    float Min;            Max = Red;    Min = Red;        if (Green > Max)    Max = Green;    if (Blue > Max)     Max = Blue;        if (Green < Min)    Min = Green;    if (Blue < Min)     Min = Blue;        float Hue;    float Sat;    float Lum;        float Diff = Max - Min;    float Tot  = Max + Min;        Lum = Tot / 2.0;        // Check for gray-scale    if (Lum == 0.0 || Lum == 1.0 || Diff == 0.0) {        Hue = 2.0 / 3.0;        Sat = 0.0;    }    // Not gray-scale    else {        if (Lum < 0.5)            Sat = Diff / Tot;        else            Sat = Diff / (2.0 - Tot);                    if (Max == Red)            Hue = (Green - Blue) / Diff;        else if (Max == Green)            Hue = 2.0 + (Blue - Red) / Diff;        else            Hue = 4.0 + (Red - Green) / Diff;                    Hue = Hue / 6.0;                if (Hue < 0.0)            Hue = Hue + 1.0;    }        return <Hue, Sat, Lum>;}ShowColors() {    vector RGB = llGetColor(SIDE_TO_CHECK_COLOR);    vector HSL = RGB_To_HSL(RGB);        integer Hue = llRound(HSL.x * 240.0);    integer Sat = llRound(HSL.y * 240.0);    integer Lum = llRound(HSL.z * 240.0);    integer Red     = llRound(RGB.x * 255.0);    integer Green   = llRound(RGB.y * 255.0);    integer Blue    = llRound(RGB.z * 255.0);    llSay(0, "RGB: <" + (string)Red + ", " +             (string)Green + ", " + (string)Blue +">");    llSay(0, "HSL: <" + (string)Hue + ", " +             (string)Sat + ", " + (string)Lum + ">");}default {    state_entry() {        ShowColors();    }        changed(integer change) {        if (change == CHANGED_COLOR) {            ShowColors();        }    }}`

created by Xylor
6-12-2003
_____________________
i've got nothing.
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
04-25-2005 09:03
/54/8d/44201/1.html
_____________________
i've got nothing.