Difference between revisions of "User:Viktoria Dovgal/Named Color Finder"

From Second Life Wiki
Jump to navigation Jump to search
m
m
Line 46: Line 46:


// An intentionally bad color value we use to report on errors
// An intentionally bad color value we use to report on errors
vector _COLOR_ERROR = <-1., -1., -1>;
vector _COLOR_ERROR = <-1., -1., -1.>;


// Hex RGB values for the SVG subset of X11 named colors, squished into a big string
// Hex RGB values for the SVG subset of X11 named colors, squished into a big string

Revision as of 00:18, 24 June 2008

See http://www.w3.org/TR/SVG/types.html#ColorKeywords for the alphabetical list of colors with samples.

A version sorted visually is at http://en.wikipedia.org/wiki/Web_colors#X11_color_names


<lsl> // ====================================================================== // Summary // ====================================================================== // Helper functions for color-change gadgets // version - 2008-06-23 // // vector Name2Color(string name) - name, #rrggbb or <x,y,z> string to LSL color vector // vector Hex2Vec(string hexcode) - rrggbb to LSL color vector // ======================================================================


// ====================================================================== // Licensing // ====================================================================== // Copyright ©2008 Viktoria Dovgal // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // ======================================================================


// ====================================================================== // constant things // ======================================================================

// An intentionally bad color value we use to report on errors vector _COLOR_ERROR = <-1., -1., -1.>;

// Hex RGB values for the SVG subset of X11 named colors, squished into a big string // These were once called the Netscape named colors. // // See http://www.w3.org/TR/SVG/types.html#ColorKeywords // also http://en.wikipedia.org/wiki/Web_colors#X11_color_names for a pretty table // string _SVG_CODES = "f0f8fffaebd700ffff7fffd4f0fffff5f5dcffe4c4000000ffebcd0000ff8a2be2a52a2adeb8875f9ea07fff00d2691eff7f506495edfff8dcdc143c00ffff00008b008b8bb8860ba9a9a9006400a9a9a9bdb76b8b008b556b2fff8c009932cc8b0000e9967a8fbc8f483d8b2f4f4f2f4f4f00ced19400d3ff149300bfff6969696969691e90ffb22222fffaf0228b22ff00ffdcdcdcf8f8ffffd700daa520808080008000adff2f808080f0fff0ff69b4cd5c5c4b0082fffff0f0e68ce6e6fafff0f57cfc00fffacdadd8e6f08080e0fffffafad2d3d3d390ee90d3d3d3ffb6c1ffa07a20b2aa87cefa778899778899b0c4deffffe000ff0032cd32faf0e6ff00ff80000066cdaa0000cdba55d39370db3cb3717b68ee00fa9a48d1ccc71585191970f5fffaffe4e1ffe4b5ffdead000080fdf5e68080006b8e23ffa500ff4500da70d6eee8aa98fb98afeeeedb7093ffefd5ffdab9cd853fffc0cbdda0ddb0e0e6800080ff0000bc8f8f4169e18b4513fa8072f4a4602e8b57fff5eea0522dc0c0c087ceeb6a5acd708090708090fffafa00ff7f4682b4d2b48c008080d8bfd8ff634740e0d0ee82eef5deb3fffffff5f5f5ffff009acd32";

// SVG color names list _SVG_NAMES = [

   "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque",
   "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue",
   "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan",
   "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey",
   "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred",
   "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkslategrey",
   "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey",
   "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro",
   "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "grey",
   "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender",
   "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral",
   "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen", "lightgrey",
   "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray",
   "lightslategrey", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen",
   "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid",
   "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen",
   "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose",
   "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange",
   "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise",
   "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue",
   "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown",
   "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray",
   "slategrey", "snow", "springgreen", "steelblue", "tan", "teal", "thistle",
   "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow",
   "yellowgreen"

];

string _HEX = "0x"; // ======================================================================


// ====================================================================== // User functions // ======================================================================

// ====================================================================== // Turn a 6-character hex RGB code into SL style. // Return COLOR_ERROR if not 6 hex digits // ====================================================================== vector Hex2Vec (string hexcode) {

   if (llStringLength(hexcode) != 6)
       return _COLOR_ERROR;
   hexcode = llToLower(hexcode);
   integer i;
   for (i = 0; i < 6; i++) {
       if (llSubStringIndex("0123456789abcdef", llGetSubString(hexcode, i, i)) == -1)
           return _COLOR_ERROR;
   }
   return <
       (integer)(_HEX + llGetSubString(hexcode, 0, 1)) / 255.,
       (integer)(_HEX + llGetSubString(hexcode, 2, 3)) / 255.,
       (integer)(_HEX + llGetSubString(hexcode, 4, 5)) / 255.
   >;

}


// ====================================================================== // Return the RGB vector matching the requested color, or _COLOR_ERROR on error. // Mash out the white space so that requests for things like "light pink" will work. // Can handle CSS/SVG named colors (a subset of the X11 named colors), #rrggbb // HTML-style RGB, and LSL vector strings. // ====================================================================== vector GetColor(string name) {

   integer p;
   // squeeze out white space
   name = llStringTrim(llToLower(name), STRING_TRIM);
   name = llDumpList2String(llParseString2List(name, [" ", "\t"], []), "");
   string firstchar = llGetSubString(name, 0, 0);
   // Attempt to interpret as RGB hex, web style #rrggbb
   // Badly formed ones will return _COLOR_ERROR
   if (firstchar == "#") {
       if (name == "#") {
           return _COLOR_ERROR;
       }
       return Hex2Vec(llGetSubString(name, 1, -1));
   }
   
   // Attempt to interpret vectors
   // Badly formed ones we *do* catch will return _COLOR_ERROR
   // Badly formed ones we *don't* catch will return ZERO_VECTOR
   if (firstchar == "<") {
       if (llGetSubString(name, -1, -1) != ">") {
           // llWhisper(0, "missing >");
           return _COLOR_ERROR;
       }
       if (llGetListLength(llParseString2List(name, [","], [])) != 3) {
           // llWhisper(0, "not a vector");
           return _COLOR_ERROR;
       }
       vector v = (vector) name;
       if (v.x > 1. || v.y > 1. || v.z > 1. || v.x < 0. || v.y < 0. || v.z < 0.) {
           // llWhisper(0, "vector not in color range");
           return _COLOR_ERROR;
       }
       return v;
   }
   // Try it as a named SVG color
   integer v = llListFindList(_SVG_NAMES, [name]) * 6;
   if (v != -6) {
       return Hex2Vec(llGetSubString(_SVG_CODES, v, v + 5));
   }
   // We didn't match anything =(
   // llWhisper(0, "default to error");
   return _COLOR_ERROR;

}


// ====================================================================== // Test script // say /5 colorname or /5 #rrggbb or /5 <r.r,g.g,b.b> in chat // ====================================================================== default {

   state_entry()
   {
       llListen(5, "", "", "");
       integer numcolors = llGetListLength(_SVG_NAMES);
       // _SVG_CODES should be number of names * 6
       llWhisper(
           0, (string)numcolors + " names, SVG defines 147, codes should be " +
           (string)(numcolors * 6) + " chars long, real length " +
           (string) llStringLength(_SVG_CODES)
       );
       llWhisper(0, "free: " + (string) llGetFreeMemory());
   }
   listen(integer channel, string name, key id, string message) {
       vector newcolor = GetColor(message);
       if (newcolor == _COLOR_ERROR) {
           llWhisper(0, "couldn't translate \"" + message + "\" to a color");
       }
       else {
           llWhisper(0, "setting color to " + (string) newcolor);
           llSetColor(newcolor, ALL_SIDES);
           llWhisper(0, "free: " + (string) llGetFreeMemory());
       }
   }

} </lsl>