Translating a color name to an RGB numeric triplet
In LiveCode the backgroundColor of an object can be in any of the three equivalent forms of a color reference: a numeric RGB triplet like "255,248,198", an HTML-style color like "#FFF8C6", or a color name like "LemonChiffon".
This lesson will show you how to convert from a color name to an RGB triplet.
This custom function is called with a pColorName parameter and returns the equivalent RGB color value.
put RGBFromColorName(the borderColor of me) into tTestColor
set the textColor of field 1 to RGBFromColorName(tColor)
This handler depends on a subtle trick: setting the backgroundPixel of an object to itself transforms the object's backgroundColor to its RGB equivalent. This also works for the other seven pixel properties and their color equivalents. To accomplish this, the handler creates a temporary button and sets its backgroundColor to the color name. Then it sets the button's backgroundPixel to itself. The result is that the button's backgroundColor is transformed into the RGB equivalent.
The first thing the handler does is to use the is a operator to check whether the pColorName parameter is a color or not. The is a operator also allows the parameter to be an RGB triplet or HTML-style color reference. Passing such a color reference to this function isn't very useful. But it won't cause an execution error, and the result is reasonable, if pColorName is an RGB triplet, the function just returns that triplet, so we don't try to make sure that pColorName is a color name, just that it's some sort of valid color reference. If pColorName is not a color reference, the handler uses the return control structure to stop the handler.
if pColorName is not a color then return "Error: not a color" end if
Next, the handler creates a button. The create invisible form of the create command makes sure the button is hidden. This way, the user doesn't see the button on the screen while the handler executes. You could also create the button outside the window's rectangle, or use the lock screen command to prevent the button from being seen. But using the create invisible form is simpler. If the result function is set, the button couldn't be created for some reason, so the handler uses the return control structure to stop it and return an error message.
create invisible button if the result is not empty then return "Error" end if
set the backgroundPixel of last button to the backgroundPixel of last button
Because the button is the most recently-created control on the card, and we haven't changed its layer since it was created, it is also the topmost control on the card. This makes it the last button, so we can refer to it that way when setting its color.
The button's color is unchanged, but its backgroundColor property is now in RGB triplet form, instead of being a color name. Now we just need to clean up: the handler gets the button's backgroundColor and places it in a variable, deletes the button, switches back to the Browse tool, and returns the background color variable.
## the button's backgroundColor is now RGB: put the backgroundColor of last button into tRGBColor delete last button ## the create command automatically chose the pointer tool ## change it back: send "choose browse tool" to me in 10 milliseconds return tRGBColor
function RGBFromColorName pColorName local tRGBColor if pColorName is not a color then return "Error: not a color" end if ## create a temporary object for the color transformation: create invisible button if the result is not empty then return "Error" end if set the backgroundColor of last button to pColorName ## transform the color using the backgroundPixel trick: set the backgroundPixel of last button to the backgroundPixel of last button ## the button's backgroundColor is now RGB: put the backgroundColor of last button into tRGBColor delete last button ## the create command automatically chose the pointer tool ## change it back: send "choose browse tool" to me in 10 milliseconds return tRGBColor end RGBFromColorName