Difference between revisions of "ColorConvert"

From Second Life Wiki
Jump to navigation Jump to search
(Created page)
 
(The external function was not needed. Fixed a number of logic holes. Added some new functionality)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}


This function converts color values from various formats to a vector. Colors can be expressed three ways: as a hexidecimal value preceeded by a # character, as RGB values separated by commas, or as one of the common [http://www.w3schools.com/HTML/html_colornames.asp HTML color names]. This function requires the [[Base2Dec]] function.
This function converts color values from various formats to a vector. Colors can be expressed three ways: as a hexidecimal value (can be preceeded by "#" or "0x" or nothing at all), as RGB values separated by commas, or as one of the common [http://www.w3schools.com/HTML/html_colornames.asp HTML color names].


<lsl>
<lsl>
vector  sbColorConvert(string strData) {
list lstHTML = [
    integer intRed;
         "aliceblue",            0xf0f8ff, "antiquewhite",    0xfaebd7, "aqua",            0x00ffff,
    integer intGrn;
         "aquamarine",          0x7fffd4, "azure",          0xf0ffff, "beige",          0xf5f5dc,
    integer intBlu;
         "bisque",              0xffe4c4, "black",          0x000000, "blanchedalmond",  0xffebcd,
 
         "blue",                0x0000ff, "blueviolet",      0x8a2be2, "brown",          0xa52a2a,
    string  strColor;
         "burlywood",            0xdeb887, "cadetblue",      0x5f9ea0, "chartreuse",      0x7fff00,
 
         "chocolate",            0xd2691e, "coral",          0xff7f50, "cornflowerblue",  0x6495ed,
    integer intHTML;
         "cornsilk",            0xfff8dc, "crimson",        0xdc143c, "cyan",            0x00ffff,
 
         "darkblue",            0x00008b, "darkcyan",        0x008b8b, "darkgoldenrod",  0xb8860b,
    list   lstHTML = [
         "darkgray",            0xa9a9a9, "darkgreen",      0x006400, "darkkhaki",      0xbdb76b,
         "aliceblue",            "f0f8ff", "antiquewhite",    "faebd7", "aqua",            "00ffff",
         "darkmagenta",          0x8b008b, "darkolivegreen",  0x556b2f, "darkorange",      0xff8c00,
         "aquamarine",          "7fffd4", "azure",          "f0ffff", "beige",          "f5f5dc",
         "darkorchid",          0x9932cc, "darkred",        0x8b0000, "darksalmon",      0xe9967a,
         "bisque",              "ffe4c4", "black",          "000000", "blanchedalmond",  "ffebcd",
         "darkseagreen",        0x8fbc8f, "darkslateblue",  0x483d8b, "darkslategray",  0x2f4f4f,
         "blue",                "0000ff", "blueviolet",      "8a2be2", "brown",          "a52a2a",
         "darkturquoise",        0x00ced1, "darkviolet",      0x9400d3, "deeppink",        0xff1493,
         "burlywood",            "deb887", "cadetblue",      "5f9ea0", "chartreuse",      "7fff00",
         "deepskyblue",          0x00bfff, "dimgray",        0x696969, "dodgerblue",      0x1e90ff,
         "chocolate",            "d2691e", "coral",          "ff7f50", "cornflowerblue",  "6495ed",
         "firebrick",            0xb22222, "floralwhite",    0xfffaf0, "forestgreen",    0x228b22,
         "cornsilk",            "fff8dc", "crimson",        "dc143c", "cyan",            "00ffff",
         "fuchsia",              0xff00ff, "gainsboro",      0xdcdcdc, "ghostwhite",      0xf8f8ff,
         "darkblue",            "00008b", "darkcyan",        "008b8b", "darkgoldenrod",  "b8860b",
         "gold",                0xffd700, "goldenrod",      0xdaa520, "gray",            0x808080,
         "darkgray",            "a9a9a9", "darkgreen",      "006400", "darkkhaki",      "bdb76b",
         "green",                0x008000, "greenyellow",    0xadff2f, "honeydew",        0xf0fff0,
         "darkmagenta",          "8b008b", "darkolivegreen",  "556b2f", "darkorange",      "ff8c00",
         "hotpink",              0xff69b4, "indianred",      0xcd5c5c, "indigo",          0x4b0082,
         "darkorchid",          "9932cc", "darkred",        "8b0000", "darksalmon",      "e9967a",
         "ivory",                0xfffff0, "khaki",          0xf0e68c, "lavender",        0xe6e6fa,
         "darkseagreen",        "8fbc8f", "darkslateblue",  "483d8b", "darkslategray",  "2f4f4f",
         "lavenderblush",        0xfff0f5, "lawngreen",      0x7cfc00, "lemonchiffon",    0xfffacd,
         "darkturquoise",        "00ced1", "darkviolet",      "9400d3", "deeppink",        "ff1493",
         "lightblue",            0xadd8e6, "lightcoral",      0xf08080, "lightcyan",      0xe0ffff,
         "deepskyblue",          "00bfff", "dimgray",        "696969", "dodgerblue",      "1e90ff",
         "lightgoldenrodyellow", 0xfafad2, "lightgrey",      0xd3d3d3, "lightgreen",      0x90ee90,
         "firebrick",            "b22222", "floralwhite",    "fffaf0", "forestgreen",    "228b22",
         "lightpink",            0xffb6c1, "lightsalmon",    0xffa07a, "lightseagreen",  0x20b2aa,
         "fuchsia",              "ff00ff", "gainsboro",      "dcdcdc", "ghostwhite",      "f8f8ff",
         "lightskyblue",        0x87cefa, "lightslategray",  0x778899, "lightsteelblue",  0xb0c4de,
         "gold",                "ffd700", "goldenrod",      "daa520", "gray",            "808080",
         "lightyellow",          0xffffe0, "lime",            0x00ff00, "limegreen",      0x32cd32,
         "green",                "008000", "greenyellow",    "adff2f", "honeydew",        "f0fff0",
         "linen",                0xfaf0e6, "magenta",        0xff00ff, "maroon",          0x800000,
         "hotpink",              "ff69b4", "indianred",      "cd5c5c", "indigo",          "4b0082",
         "mediumaquamarine",    0x66cdaa, "mediumblue",      0x0000cd, "mediumorchid",    0xba55d3,
         "ivory",                "fffff0", "khaki",          "f0e68c", "lavender",        "e6e6fa",
         "mediumpurple",        0x9370d8, "mediumseagreen",  0x3cb371, "mediumslateblue", 0x7b68ee,
         "lavenderblush",        "fff0f5", "lawngreen",      "7cfc00", "lemonchiffon",    "fffacd",
         "mediumspringgreen",    0x00fa9a, "mediumturquoise", 0x48d1cc, "mediumvioletred", 0xc71585,
         "lightblue",            "add8e6", "lightcoral",      "f08080", "lightcyan",      "e0ffff",
         "midnightblue",        0x191970, "mintcream",      0xf5fffa, "mistyrose",      0xffe4e1,
         "lightgoldenrodyellow", "fafad2", "lightgrey",      "d3d3d3", "lightgreen",      "90ee90",
         "moccasin",            0xffe4b5, "navajowhite",    0xffdead, "navy",            0x000080,
         "lightpink",            "ffb6c1", "lightsalmon",    "ffa07a", "lightseagreen",  "20b2aa",
         "oldlace",              0xfdf5e6, "olive",          0x808000, "olivedrab",      0x6b8e23,
         "lightskyblue",        "87cefa", "lightslategray",  "778899", "lightsteelblue",  "b0c4de",
         "orange",              0xffa500, "orangered",      0xff4500, "orchid",          0xda70d6,
         "lightyellow",          "ffffe0", "lime",            "00ff00", "limegreen",      "32cd32",
         "palegoldenrod",        0xeee8aa, "palegreen",      0x98fb98, "paleturquoise",  0xafeeee,
         "linen",                "faf0e6", "magenta",        "ff00ff", "maroon",          "800000",
         "palevioletred",        0xd87093, "papayawhip",      0xffefd5, "peachpuff",      0xffdab9,
         "mediumaquamarine",    "66cdaa", "mediumblue",      "0000cd", "mediumorchid",    "ba55d3",
         "peru",                0xcd853f, "pink",            0xffc0cb, "plum",            0xdda0dd,
         "mediumpurple",        "9370d8", "mediumseagreen",  "3cb371", "mediumslateblue", "7b68ee",
         "powderblue",          0xb0e0e6, "purple",          0x800080, "red",            0xff0000,
         "mediumspringgreen",    "00fa9a", "mediumturquoise", "48d1cc", "mediumvioletred", "c71585",
         "rosybrown",            0xbc8f8f, "royalblue",      0x4169e1, "saddlebrown",    0x8b4513,
         "midnightblue",        "191970", "mintcream",      "f5fffa", "mistyrose",      "ffe4e1",
         "salmon",              0xfa8072, "sandybrown",      0xf4a460, "seagreen",        0x2e8b57,
         "moccasin",            "ffe4b5", "navajowhite",    "ffdead", "navy",            "000080",
         "seashell",            0xfff5ee, "sienna",          0xa0522d, "silver",          0xc0c0c0,
         "oldlace",              "fdf5e6", "olive",          "808000", "olivedrab",      "6b8e23",
         "skyblue",              0x87ceeb, "slateblue",      0x6a5acd, "slategray",      0x708090,
         "orange",              "ffa500", "orangered",      "ff4500", "orchid",          "da70d6",
         "snow",                0xfffafa, "springgreen",    0x00ff7f, "steelblue",      0x4682b4,
         "palegoldenrod",        "eee8aa", "palegreen",      "98fb98", "paleturquoise",  "afeeee",
         "tan",                  0xd2b48c, "teal",            0x008080, "thistle",        0xd8bfd8,
         "palevioletred",        "d87093", "papayawhip",      "ffefd5", "peachpuff",      "ffdab9",
         "tomato",              0xff6347, "turquoise",      0x40e0d0, "violet",          0xee82ee,
         "peru",                "cd853f", "pink",            "ffc0cb", "plum",            "dda0dd",
         "wheat",                0xf5deb3, "white",          0xffffff, "whitesmoke",      0xf5f5f5,
         "powderblue",          "b0e0e6", "purple",          "800080", "red",            "ff0000",
         "yellow",              0xffff00, "yellowgreen",    0x9acd32
         "rosybrown",            "bc8f8f", "royalblue",      "4169e1", "saddlebrown",    "8b4513",
         "salmon",              "fa8072", "sandybrown",      "f4a460", "seagreen",        "2e8b57",
         "seashell",            "fff5ee", "sienna",          "a0522d", "silver",          "c0c0c0",
         "skyblue",              "87ceeb", "slateblue",      "6a5acd", "slategray",      "708090",
         "snow",                "fffafa", "springgreen",    "00ff7f", "steelblue",      "4682b4",
         "tan",                  "d2b48c", "teal",            "008080", "thistle",        "d8bfd8",
         "tomato",              "ff6347", "turquoise",      "40e0d0", "violet",          "ee82ee",
         "wheat",                "f5deb3", "white",          "ffffff", "whitesmoke",      "f5f5f5",
         "yellow",              "ffff00", "yellowgreen",    "9acd32"
     ];
     ];


     intHTML = llListFindList(lstHTML, [llToLower(strData)]);
vector  sbColorConvert(string strData) {
     integer mode = llListFindList(lstHTML, [llToLower(strData)]);
    integer intColor = 0;


     if (intHTML > -1) {
     if (~mode) {
         strColor = llList2String(lstHTML, intHTML + 1);
         intColor = llList2Integer(lstHTML, mode | 1);
        mode = 8;
     }
     }
 
    else {
    if (llGetSubString(strData, 0, 0) == "#") {
        string strStart = llGetSubString(strData, 0, 1);
        strColor = llToLower(llGetSubString(strData, 1, -1));
        if (strStart != "0x") {
            if (llGetSubString(strStart, 0, 0) == "#") {
                strData = "0x" + llDeleteSubString(strData, 0, 0);
            else {
                vector vecColor = (vector)("<"+strData+">");
                if (vecColor) {
                    return (vector)("<"+strData+">") / 255;
                }
                strData = "0x" + strData;
            }
        }
        intColor = (integer)strData;
        mode = llStringLength(strData);
     }
     }
 
     if (mode == 8) {//RRGGBB
     if (llStringLength(strColor) > 0) {
         return <(intColor >> 16) & 0xFF, (intColor >> 8) & 0xFF, intColor & 0xFF> / 0xFF;
         intRed = sbBase2Dec(llGetSubString(strColor, 0, 1), 16);
        intGrn = sbBase2Dec(llGetSubString(strColor, 2, 3), 16);
        intBlu = sbBase2Dec(llGetSubString(strColor, 4, 5), 16);
     }
     }
 
     else if (mode == 5) {//RGB (an HTML color format)
     else {
         return <(intColor >> 8) & 0xF, (intColor >> 4) & 0xF, intColor & 0xF> / 0xF;
        list    lstRGB = llParseString2List(strData, [","], []);
 
         intRed = llList2Integer(lstRGB, 0);
        intGrn = llList2Integer(lstRGB, 1);
        intBlu = llList2Integer(lstRGB, 2);
     }
     }
 
     return ZERO_VECTOR;//black
     return <intRed, intGrn, intBlu> / 255;
}
}
</lsl>
</lsl>


{{LSLC|Library}}{{LSLC|Examples}}
{{LSLC|Library}}{{LSLC|Examples}}

Revision as of 10:37, 7 July 2009

This function converts color values from various formats to a vector. Colors can be expressed three ways: as a hexidecimal value (can be preceeded by "#" or "0x" or nothing at all), as RGB values separated by commas, or as one of the common HTML color names.

<lsl> list lstHTML = [

       "aliceblue",            0xf0f8ff, "antiquewhite",    0xfaebd7, "aqua",            0x00ffff,
       "aquamarine",           0x7fffd4, "azure",           0xf0ffff, "beige",           0xf5f5dc,
       "bisque",               0xffe4c4, "black",           0x000000, "blanchedalmond",  0xffebcd,
       "blue",                 0x0000ff, "blueviolet",      0x8a2be2, "brown",           0xa52a2a,
       "burlywood",            0xdeb887, "cadetblue",       0x5f9ea0, "chartreuse",      0x7fff00,
       "chocolate",            0xd2691e, "coral",           0xff7f50, "cornflowerblue",  0x6495ed,
       "cornsilk",             0xfff8dc, "crimson",         0xdc143c, "cyan",            0x00ffff,
       "darkblue",             0x00008b, "darkcyan",        0x008b8b, "darkgoldenrod",   0xb8860b,
       "darkgray",             0xa9a9a9, "darkgreen",       0x006400, "darkkhaki",       0xbdb76b,
       "darkmagenta",          0x8b008b, "darkolivegreen",  0x556b2f, "darkorange",      0xff8c00,
       "darkorchid",           0x9932cc, "darkred",         0x8b0000, "darksalmon",      0xe9967a,
       "darkseagreen",         0x8fbc8f, "darkslateblue",   0x483d8b, "darkslategray",   0x2f4f4f,
       "darkturquoise",        0x00ced1, "darkviolet",      0x9400d3, "deeppink",        0xff1493,
       "deepskyblue",          0x00bfff, "dimgray",         0x696969, "dodgerblue",      0x1e90ff,
       "firebrick",            0xb22222, "floralwhite",     0xfffaf0, "forestgreen",     0x228b22,
       "fuchsia",              0xff00ff, "gainsboro",       0xdcdcdc, "ghostwhite",      0xf8f8ff,
       "gold",                 0xffd700, "goldenrod",       0xdaa520, "gray",            0x808080,
       "green",                0x008000, "greenyellow",     0xadff2f, "honeydew",        0xf0fff0,
       "hotpink",              0xff69b4, "indianred",       0xcd5c5c, "indigo",          0x4b0082,
       "ivory",                0xfffff0, "khaki",           0xf0e68c, "lavender",        0xe6e6fa,
       "lavenderblush",        0xfff0f5, "lawngreen",       0x7cfc00, "lemonchiffon",    0xfffacd,
       "lightblue",            0xadd8e6, "lightcoral",      0xf08080, "lightcyan",       0xe0ffff,
       "lightgoldenrodyellow", 0xfafad2, "lightgrey",       0xd3d3d3, "lightgreen",      0x90ee90,
       "lightpink",            0xffb6c1, "lightsalmon",     0xffa07a, "lightseagreen",   0x20b2aa,
       "lightskyblue",         0x87cefa, "lightslategray",  0x778899, "lightsteelblue",  0xb0c4de,
       "lightyellow",          0xffffe0, "lime",            0x00ff00, "limegreen",       0x32cd32,
       "linen",                0xfaf0e6, "magenta",         0xff00ff, "maroon",          0x800000,
       "mediumaquamarine",     0x66cdaa, "mediumblue",      0x0000cd, "mediumorchid",    0xba55d3,
       "mediumpurple",         0x9370d8, "mediumseagreen",  0x3cb371, "mediumslateblue", 0x7b68ee,
       "mediumspringgreen",    0x00fa9a, "mediumturquoise", 0x48d1cc, "mediumvioletred", 0xc71585,
       "midnightblue",         0x191970, "mintcream",       0xf5fffa, "mistyrose",       0xffe4e1,
       "moccasin",             0xffe4b5, "navajowhite",     0xffdead, "navy",            0x000080,
       "oldlace",              0xfdf5e6, "olive",           0x808000, "olivedrab",       0x6b8e23,
       "orange",               0xffa500, "orangered",       0xff4500, "orchid",          0xda70d6,
       "palegoldenrod",        0xeee8aa, "palegreen",       0x98fb98, "paleturquoise",   0xafeeee,
       "palevioletred",        0xd87093, "papayawhip",      0xffefd5, "peachpuff",       0xffdab9,
       "peru",                 0xcd853f, "pink",            0xffc0cb, "plum",            0xdda0dd,
       "powderblue",           0xb0e0e6, "purple",          0x800080, "red",             0xff0000,
       "rosybrown",            0xbc8f8f, "royalblue",       0x4169e1, "saddlebrown",     0x8b4513,
       "salmon",               0xfa8072, "sandybrown",      0xf4a460, "seagreen",        0x2e8b57,
       "seashell",             0xfff5ee, "sienna",          0xa0522d, "silver",          0xc0c0c0,
       "skyblue",              0x87ceeb, "slateblue",       0x6a5acd, "slategray",       0x708090,
       "snow",                 0xfffafa, "springgreen",     0x00ff7f, "steelblue",       0x4682b4,
       "tan",                  0xd2b48c, "teal",            0x008080, "thistle",         0xd8bfd8,
       "tomato",               0xff6347, "turquoise",       0x40e0d0, "violet",          0xee82ee,
       "wheat",                0xf5deb3, "white",           0xffffff, "whitesmoke",      0xf5f5f5,
       "yellow",               0xffff00, "yellowgreen",     0x9acd32
   ];

vector sbColorConvert(string strData) {

   integer mode = llListFindList(lstHTML, [llToLower(strData)]);
   integer intColor = 0;
   if (~mode) {
       intColor = llList2Integer(lstHTML, mode | 1);
       mode = 8;
   }
   else {
       string strStart = llGetSubString(strData, 0, 1);
       if (strStart != "0x") {
           if (llGetSubString(strStart, 0, 0) == "#") {
               strData = "0x" + llDeleteSubString(strData, 0, 0);
           else {
               vector vecColor = (vector)("<"+strData+">");
               if (vecColor) {
                   return (vector)("<"+strData+">") / 255;
               }
               strData = "0x" + strData;
           }
       }
       intColor = (integer)strData;
       mode = llStringLength(strData);
   }
   if (mode == 8) {//RRGGBB
       return <(intColor >> 16) & 0xFF, (intColor >> 8) & 0xFF, intColor & 0xFF> / 0xFF;
   }
   else if (mode == 5) {//RGB (an HTML color format)
       return <(intColor >> 8) & 0xF, (intColor >> 4) & 0xF, intColor & 0xF> / 0xF;
   }
   return ZERO_VECTOR;//black

} </lsl>