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.
The RGBFromColorName function
This custom function is called with a pColorName parameter and returns the equivalent RGB color value.
get RGBFromColorName("LightGreen")
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.
Checking the pColorName parameter
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
Creating a temporary, invisible button
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
Getting the RGB color equivalent
The handler sets the button's backgroundColor to the color name pColorName, then sets the button's backgroundPixel property to itself:
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
The RGBColorFromName function code
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
torocruzand
Very good, looking at the IDE code I came across something that simplifies the function a little more.
function RGBFromColorName pColor
local tColorRGB
# For it is RGBA
put item 1 to 3 of pColor into pColor
if pColor is a color then
set the colorOverlay["color"] of the templateGraphic to pColor
put the colorOverlay["color"] of the templateGraphic into tColorRGB
reset the templateGraphic
return tColorRGB for value
else
return empty for value
end if
end RGBFromColorName
Tim Bobo
We need a way to go the opposite direction for most users. RGB to Hex.
Tim Bobo
This can be done in reverse or forward by using baseConvert function on each of the RGB values. And it can be done in four lines of code.
Convert from RBG to HEX
put baseConvert(redvalue,8,16) into hexRed
put baseConvert(greenvalue,8,16) into hexGreen
put baseConvert(bluevalue,8,16) into hexBlue
put "#" & hexRed & hexBlue & hexGreen into myHexColor
I think the revers could be done also.
Tim Bobo
fix to the code above.
function convertColor myRGBColor
set itemdel to ","
put item 1 of myRGBColor into myRed
put item 2 of myRGBColor into myGreen
put item 3 of myRGBColor into myBlue
put baseConvert(myRed,10,16) into hexRed
put baseConvert(myGreen,10,16) into hexGreen
put baseConvert(myBlue,10,16) into hexBlue
put "#" & hexRed & hexGreen & hexBlue into myHEXColor
return myHEXColor
end convertColor
Bernd Niggemann
This is another way to get at the HEXcolor
function ConvertRGBToHexColor pRGB
local tHexColor
put "#" into tHexColor
repeat with j = 1 to 3
put format("%02X",item j of pRGB) after tHexColor
end repeat
return tHexColor
end ConvertRGBToHexColor