Category:LSL Color

From Second Life Wiki
Jump to navigation Jump to search

Color in LSL

Color in LSL

LSL has its own special format for color. LSL uses a vector to store color. Unlike traditional RGB where each channel is 0 -> 255, LSL's color channels are 0 -> 1.

Format: <R, G, B>

• float x Red value [0, 1]
• float y Green value [0, 1]
• float z Blue value [0, 1]

Light colors

Unlike most other functions relating to color in LSL, lights operate in a slightly different color space and thus do not accept a regular RGB value. (Or, they will, however the displayed color will not be the expected color).

Fortunately, this is easily resolved with 2 functions:

  • llsRGB2Linear - Convert regular LSL color into linear color. (for use in lights)
  • llLinear2sRGB - Convert linear color to regular LSL color.

Examples

Color Code
NAVY 5|0|right}}, {{#pad:0.122|5|0|right}}, {{#pad:0.247|5|0|right}}>
BLUE 5|0|right}}, {{#pad:0.455|5|0|right}}, {{#pad:0.851|5|0|right}}>
AQUA 5|0|right}}, {{#pad:0.859|5|0|right}}, {{#pad:1.|5|0|right}}>
TEAL 5|0|right}}, {{#pad:0.8|5|0|right}}, {{#pad:0.8|5|0|right}}>
OLIVE 5|0|right}}, {{#pad:0.6|5|0|right}}, {{#pad:0.439|5|0|right}}>
GREEN 5|0|right}}, {{#pad:0.8|5|0|right}}, {{#pad:0.251|5|0|right}}>
LIME 5|0|right}}, {{#pad:1.|5|0|right}}, {{#pad:0.439|5|0|right}}>
YELLOW 5|0|right}}, {{#pad:0.863|5|0|right}}, {{#pad:0.|5|0|right}}>
ORANGE 5|0|right}}, {{#pad:0.522|5|0|right}}, {{#pad:0.106|5|0|right}}>
RED 5|0|right}}, {{#pad:0.255|5|0|right}}, {{#pad:0.212|5|0|right}}>
MAROON 5|0|right}}, {{#pad:0.078|5|0|right}}, {{#pad:0.294|5|0|right}}>
FUCHSIA 5|0|right}}, {{#pad:0.071|5|0|right}}, {{#pad:0.745|5|0|right}}>
PURPLE 5|0|right}}, {{#pad:0.051|5|0|right}}, {{#pad:0.788|5|0|right}}>
WHITE 5|0|right}}, {{#pad:1.|5|0|right}}, {{#pad:1.|5|0|right}}>
SILVER 5|0|right}}, {{#pad:0.867|5|0|right}}, {{#pad:0.867|5|0|right}}>
GRAY 5|0|right}}, {{#pad:0.667|5|0|right}}, {{#pad:0.667|5|0|right}}>
BLACK 5|0|right}}, {{#pad:0.|5|0|right}}, {{#pad:0.|5|0|right}}>
color picker

Equivalent color vectors to the default color picker palette. These are defined in the viewer program directory's skins/default/colors.xml while custom user-saved palette entries will be in user_settings/colors.xml.

vector black               = <0, 0, 0>;          //ColorPaletteEntry01, "Black"
vector white_A             = <1, 1, 1>;          //ColorPaletteEntry17, "White"
vector gray                = <0.5, 0.5, 0.5>;    //ColorPaletteEntry02, "Gray"
vector light_gray          = <0.75, 0.75, 0.75>; //ColorPaletteEntry18, "LtGray"
vector dark_red            = <0.5, 0, 0>;        //ColorPaletteEntry03
vector red                 = <1, 0, 0>;          //ColorPaletteEntry19, "Red"
vector dark_yellow         = <0.5, 0.5, 0>;      //ColorPaletteEntry04
vector yellow              = <1, 1, 0>;          //ColorPaletteEntry20, "Yellow"
vector dark_green          = <0, 0.5, 0>;        //ColorPaletteEntry05
vector green               = <0, 1, 0>;          //ColorPaletteEntry21, "Green"
vector dark_cyan           = <0, 0.5, 0.5>;      //ColorPaletteEntry06
vector cyan                = <0, 1, 1>;          //ColorPaletteEntry22
vector dark_blue           = <0, 0, 0.5>;        //ColorPaletteEntry07
vector blue                = <0, 0, 1>;          //ColorPaletteEntry23, "Blue"
vector dark_magenta        = <0.5, 0, 0.5>;      //ColorPaletteEntry08
vector magenta             = <1, 0, 1>;          //ColorPaletteEntry24, "Purple"
vector dirty_yellow        = <0.5, 0.5, 0>;      //ColorPaletteEntry09
vector light_yellow        = <1, 1, 0.5>;        //ColorPaletteEntry25
vector dark_green_to_blue  = <0, 0.25, 0.25>;    //ColorPaletteEntry10
vector green_to_blue       = <0, 1, 0.5>;        //ColorPaletteEntry26
vector light_green_to_blue = <0, 0.5, 1>;        //ColorPaletteEntry11
vector light_blue_to_green = <0.5, 1, 1>;        //ColorPaletteEntry27
vector dark_blue_to_cyan   = <0, 0.25, 0.5>;     //ColorPaletteEntry12
vector cyan_to_pink        = <0.5, 0.5, 1>;      //ColorPaletteEntry28
vector indigo              = <0.5, 0, 1>;        //ColorPaletteEntry13
vector violet              = <1, 1, 0.5>;        //ColorPaletteEntry29
vector dark_brown          = <0.5, 0.25, 0>;     //ColorPaletteEntry14
vector brown               = <1, 0.5, 0>;        //ColorPaletteEntry30
vector white_B             = <1, 1, 1>;          //ColorPaletteEntry15, "White"
vector white_C             = <1, 1, 1>;          //ColorPaletteEntry31
vector pale_yellow         = <1, 1, 0.79>;       //ColorPaletteEntry16, "LtYellow"
vector white_D             = <1, 1, 1>;          //ColorPaletteEntry32, "White"

// Floating point conversions, ambient inworld light and color profile variances
// can show up as small differences in stored settings and externally picked samples

Useful Snippets

Useful functions for storing/retrieving color and alpha values to/from integers

integer ColorAlphatoRGBA(vector color, float alpha) {
    return (((integer)(alpha   * 255.0) & 0xFF) << 24) |
           (((integer)(color.x * 255.0) & 0xFF) << 16) |
           (((integer)(color.y * 255.0) & 0xFF) << 8) |
            ((integer)(color.z * 255.0) & 0xFF);
}

vector RGBAtoColor(integer rgba) {
    return < (rgba >> 16) & 0xFF, (rgba >> 8) & 0xFF, (rgba & 0xFF) > / 255.0;
}

float RGBAtoAlpha(integer rgba) {
    return ((rgba >> 24) & 0xFF) / 255.0;
}