Creator Resource: Writing colors for scripts and/or configuration notecards

At times, we want to modify a simple script, to change a color that's written in it. At times, we have to do some changes in a configuration notecard, and said notecard expects that we write the colors down in a certain format.

But likely, we're not scripters. Likely, we don't know how to obtain that color "code". The case, more often than not, is that we have the color we want on a prim's face, and want the "code" for it. The Firestorm viewer has a tab in the color picker indicating the values a script would want, but not all of us use it, or know what to do with those numbers.

Fortunately, there are simple ways of obtaining said "color code". I will give here a script that will help, explaining how to use it. After it, I will give a long explanation, in case that you want to know more about how colors are written in scripts :-)

The quick way

To create this script, right click in your inventory, in a folder of your choice, and select the "New Script" option. If you don't see this option show: you have to right click over a folder.

Rename this script to something that makes sense to you. For example, rename the script to Tell me the color of a face.

Now double click on it and open it. Select all the text that you will see (the default script) and delete it. Yes, delete it! Fear not :-)

Is your script empty? Completely empty?

Perfect. Now select the code below, which I've colored dark green, so you see clearly where it begins and ends. Copy it into your clipboard (CTRL C), then paste it into the completely empty script's window (CTRL V).

    touch_start(integer total_number)
        llRegionSayTo(llDetectedKey(0), 0
            , "Color in the touched face is:"
                + "\n" + (string)llGetColor(llDetectedTouchFace(0))
                + "\nDon't forget to delete me when you're done!"

Now save this script. If you've performed the previous steps correctly, you should see a Save complete. message showing at the bottom of the script window. Otherwise, you'll see a dreaded script error. We're not bothering in trying to fix it this time. Delete the script and start again, making sure you select all the text :-)

With the script saved, that means that we can now use it.

How to use the script

Rez a box. Change the texture to Blank, so you will see the colors easily. Change any of its sides to the color whose code you want to obtain. Then drop the script inside the box.

Click the face with the color you want to know. You should get a message in chat like:

[09:02] Object: Color in the touched face is:
<0.50196, 0.50196, 1.00000>
Don't forget to delete me when you're done!

That means:

<0.50196, 0.50196, 1.00000>

is the code you were looking for :-)

(As the script instructs you, once you're done, delete the script from the prim. Otherwise, it will always send those messages to whoever clicks the prim.)

The long explanation

Any color is always a combination of Red, Green and Blue values. Usually, these values go from 0 to 255. The higher the value, the more you have of that component. They're usually written it this order: R, G, B.

To mention a few examples:

   <255, 0, 0>         RED
   <255, 255, 0>       YELLOW (= RED + GREEN)
   <128, 128, 128>     GRAY 50%
   <0, 128, 0>         GREEN 50%
   <0, 0, 0>           BLACK
   <255, 255, 255>     WHITE

But scripts in SL need this written in a very specific way. We need to convert this. Scripts expect the numbers in a different range, so instead of writing, for example, the color red like this:

<255, 0, 0>

the three numbers have to be within the range from 0 to 1, so the color red would be written as follows:

<1.0, 0.0, 0.0>

What do we do, then, when we have a RGB code for a color we like, to "translate it" to LSL (the scripting language)?

We have to take the RGB, like here: 255, 255, 0
Put the three numbers between < and > and separate by commas, adding a decimal point:

<255.0, 255.0, 0.0>

and then we have to divide the three numbers by 255.0, so we get:

<255.0/255.0, 255.0/255.0, 0.0/255.0>

which gives as result:

<1.0, 1.0, 0.0>

So, in general, once we have the values for R, G and B, we need to "translate" them to LSL, which expects the color as a vector, <R, G, B>, but values for R, G and B have to be from 0 to 1. So, if we have values from 0 to 255, to convert them into 0 to 1, we need to divide them by 255.

This is, we have <R, G, B>, being R, G and B from 0 to 255, as usual, so LSL needs that we perform the following operation:

<R/255.0, G/255.0, B/255.0>

for the definitive values for the color.


<R/255.0, G/255.0, B/255.0>

is basically the same as:

<R, G, B>/255.0

being the last one preferred if you typed it like that in a script.

There are a lot of resources in the web to find the RGB components of a color. The two following links are good resources for this:

EXAMPLE: Suppose we have the blue tone <6, 172, 255>, as it will be written with the standard 0-255 range for RGB. To convert this <6, 172, 255> to a valid color for LSL, we have to perform the following calculation:

<6.0, 172.0, 255.0>/255.0

which means that the value we'll use for the color will be:

<0.023529, 0.67451, 1.0>

(Yes, we could leave it as <6.0, 172.0, 255.0>/255.0, but it's better if we save the script to do unnecessary calculations.)

The colors mentioned above will also be written this way:

   <1.0, 0.0, 0.0>         RED
   <1.0, 1.0, 0.0>         YELLOW (= RED + GREEN)
   <0.5, 0.5, 0.5>         GRAY 50%
   <0.0, 0.5, 0.0>         GREEN 50%
   <0.0, 0.0, 0.0>         BLACK
   <1.0, 1.0, 1.0>         WHITE