Color space conversion
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Color space conversion
I want to get the backgroundcolor of a button. I find if I set it to "blue", then it returns "blue" when I interrogate it. But if I set it using
answer color
(on a Mac), then the color is always returned as RGB, e.g. {143,143,143}. How can I control the color space of the returned color? For instance always in RGB or Hex values?
answer color
(on a Mac), then the color is always returned as RGB, e.g. {143,143,143}. How can I control the color space of the returned color? For instance always in RGB or Hex values?
Kaveh
Re: Color space conversion
Hi.
You want a function that will translate the constant "red" to its RGB (or HTML style) equivalent (pseudoCode).
set the colorFormat to "RGB"
The engine will accept any of these formats, but all the examples I see in the dictionary show the various properties only as being "set" with a chosen format. In other words, you choose the format going in. If you set the backColor of a btn to "red", you will get "red" back. If you set it to "255,0,0," you will get that back instead.
Sort of like the "numberFormat". Can't wait to see if this is built-in to LC. Anyone?
Craig Newman
You want a function that will translate the constant "red" to its RGB (or HTML style) equivalent (pseudoCode).
set the colorFormat to "RGB"
The engine will accept any of these formats, but all the examples I see in the dictionary show the various properties only as being "set" with a chosen format. In other words, you choose the format going in. If you set the backColor of a btn to "red", you will get "red" back. If you set it to "255,0,0," you will get that back instead.
Sort of like the "numberFormat". Can't wait to see if this is built-in to LC. Anyone?
Craig Newman
Re: Color space conversion
Hi Craig
So are you saying I need to write a Function to do this? Nothing native in LiveCode?
So are you saying I need to write a Function to do this? Nothing native in LiveCode?
Kaveh
Re: Color space conversion
Hi Kaveh,
this comes up every once so often. How to get RGB values from colornames. If you search the use-list for "effective backpixel" you will find many threads over the years.
It is a bit tricky and there is no built-in function to e.g. always return RGB values.
If you don't care about the colorname of an object whose backgroundColor was set using a colorname you can do this:
otherwise you put the backgroundColor of the object into a variable and set the backgroundColor of the object to the variable after above code. This will then return the colorname for the backgroundColor if queried.
Same works for e.g. foreColor.
The resoning for this seems to be historic and I don't understand it.
Kind regards
Bernd
this comes up every once so often. How to get RGB values from colornames. If you search the use-list for "effective backpixel" you will find many threads over the years.
It is a bit tricky and there is no built-in function to e.g. always return RGB values.
If you don't care about the colorname of an object whose backgroundColor was set using a colorname you can do this:
Code: Select all
set the backPixel of btn 1 to the effective backpixel of btn 1
put the backgroundColor of btn 1 into field 1
Same works for e.g. foreColor.
The resoning for this seems to be historic and I don't understand it.
Kind regards
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 163
- Joined: Tue Jan 26, 2010 10:15 pm
- Contact:
Re: Color space conversion
Another solution which is not requiring to use a "real button" is to use the template button keyword:
Best,
Code: Select all
function _ColorNameToRGBValue pTheColorName
local tTheResult
if (pTheColorName is not among the lines of the colorNames) then
put 255,255,255 into tTheResult
else
set the backColor of templateButton to pTheColorName
set the backPixel of templateButton to the backPixel of templateButton
put the backColor of templateButton into tTheResult
reset the templateButton
end if
return tTheResult
end _ColorNameToRGBValue
Best,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel
Re: Color space conversion
You make a point about writing your own function. This could reside in a library somewhere, perhaps along with a custom property of that stack or as text in a field:
Where the colornames are arranged in a list ("the colorList of this stack") with the RGB values as the second item for each. Tedious to build, especially if you want all 500-odd colornames, but you only need do it once. You have to watch out for common color names in such a comprehensive list, so this needs work.
BUT. How do you associate the RGB values in the first place? "Red" is easy, "255,0,0". But you would need to use the inspector to find, say "Azure 2". You would have to set the backColor of a button to "Azure 2", and then go into the inspector to see the RGB Slider values. Tedious, as I say.
If the inspector can do it, why can't the engine do it?
The parameter "tFormat" could distinguish whether you return RGB or HTML values. Hey, may as well do the reverse as well, return the colorName with an RGB.
Craig Newman
Code: Select all
function colorNameToRGB tColorName,tFormat -- for example, "red,RGB"
return item 2 of line lineOffSet(tColorname,the ColorList of this stack)
end colorNameToRGB
BUT. How do you associate the RGB values in the first place? "Red" is easy, "255,0,0". But you would need to use the inspector to find, say "Azure 2". You would have to set the backColor of a button to "Azure 2", and then go into the inspector to see the RGB Slider values. Tedious, as I say.
If the inspector can do it, why can't the engine do it?
The parameter "tFormat" could distinguish whether you return RGB or HTML values. Hey, may as well do the reverse as well, return the colorName with an RGB.
Craig Newman
Re: Color space conversion
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens
Re: Color space conversion
Hermann.
Great work.
As usual.
Craig
Great work.
As usual.
Craig
Re: Color space conversion
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens
Re: Color space conversion
Well, try these in a button script:
Craig
Code: Select all
on mouseUp
repeat 50
lock screen
set the backcolor of me to random(100)
wait 10
unlock screen with dissolve
end repeat
end mouseUp
on mouseUp
repeat with y = 1 to 40
lock screen
set the backcolor of me to y
wait 5
unlock screen with dissolve
end repeat
end mouseUp
Re: Color space conversion
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens
Re: Color space conversion
Hi Craig,
I did this to see what it did to my eyeballs;
(your code)
But maximize the stack.
Can't be good for the brain, it's the 60's all over again.
Simon
I did this to see what it did to my eyeballs;
Code: Select all
on mouseUp
repeat 50
lock screen
set the backcolor of this stack to random(100)
wait 2
unlock screen with dissolve
end repeat
end mouseUp
But maximize the stack.
Can't be good for the brain, it's the 60's all over again.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Color space conversion
Trivia: the LiveCode color names have been in the engine since MetaCard and are the standard X11 color list :
http://en.m.wikipedia.org/wiki/X11_color_names
http://en.m.wikipedia.org/wiki/X11_color_names
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com