Difference between revisions of "Category:LSL Color"

From Second Life Wiki
Jump to navigation Jump to search
m (fixed typo reasone)
m (comment isn't strictly needed.)
Line 15: Line 15:
===Useful functions for storing/retrieving color and alpha values to/from integers===
===Useful functions for storing/retrieving color and alpha values to/from integers===
<lsl>integer ColorAlphatoRGBA(vector color, float alpha) {
<lsl>integer ColorAlphatoRGBA(vector color, float alpha) {
return (((integer)(alpha * 255.0) & 0xFF) << 24) |
    return (((integer)(alpha   * 255.0) & 0xFF) << 24) |
(((integer)(color.x * 255.0) & 0xFF) << 16) |
          (((integer)(color.x * 255.0) & 0xFF) << 16) |
(((integer)(color.y * 255.0) & 0xFF) << 8) |
          (((integer)(color.y * 255.0) & 0xFF) << 8) |
((integer)(color.z * 255.0) & 0xFF);
            ((integer)(color.z * 255.0) & 0xFF);
}
}


vector RGBAtoColor(integer rgba) {
vector RGBAtoColor(integer rgba) {
return < (rgba >> 16) & 0xFF, (rgba >> 8) & 0xFF, (rgba & 0xFF) > / 255.0; // for some reason parentheses are needed around the Z part
    return < (rgba >> 16) & 0xFF, (rgba >> 8) & 0xFF, (rgba & 0xFF) > / 255.0;
}
}


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


{{#vardefine:examples|
{{#vardefine:examples|
<lsl>vector white = <1.0, 1.0, 1.0>;
<lsl>vector white   = <1.0, 1.0, 1.0>;
vector grey = <0.5, 0.5, 0.5>;
vector grey   = <0.5, 0.5, 0.5>;
vector black = <0.0, 0.0, 0.0>;
vector black   = <0.0, 0.0, 0.0>;
vector red = <1.0, 0.0, 0.0>;
vector red     = <1.0, 0.0, 0.0>;
vector green = <0.0, 1.0, 0.0>;
vector green   = <0.0, 1.0, 0.0>;
vector blue = <0.0, 0.0, 1.0>;
vector blue   = <0.0, 0.0, 1.0>;
vector yellow = <1.0, 1.0, 0.0>;
vector yellow = <1.0, 1.0, 0.0>;
vector cyan = <0.0, 1.0, 1.0>;
vector cyan   = <0.0, 1.0, 1.0>;
vector magenta = <1.0, 0.0, 1.0>;</lsl>
vector magenta = <1.0, 0.0, 1.0>;</lsl>
}}
}}

Revision as of 11:39, 2 July 2009

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]

Examples

<lsl>vector white = <1.0, 1.0, 1.0>; vector grey = <0.5, 0.5, 0.5>; vector black = <0.0, 0.0, 0.0>; vector red = <1.0, 0.0, 0.0>; vector green = <0.0, 1.0, 0.0>; vector blue = <0.0, 0.0, 1.0>; vector yellow = <1.0, 1.0, 0.0>; vector cyan = <0.0, 1.0, 1.0>; vector magenta = <1.0, 0.0, 1.0>;</lsl>

Useful Snippets

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

<lsl>integer ColorAlphatoRGBA(vector color, float alpha) {

   return (((integer)(alpha   * 255.0) & 0xFF) << 24)