Page 1 of 1

Color space conversion

Posted: Wed Jul 16, 2014 10:05 am
by kaveh1000
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?

Re: Color space conversion

Posted: Wed Jul 16, 2014 6:40 pm
by dunbarx
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

Re: Color space conversion

Posted: Wed Jul 16, 2014 7:10 pm
by kaveh1000
Hi Craig

So are you saying I need to write a Function to do this? Nothing native in LiveCode?

Re: Color space conversion

Posted: Wed Jul 16, 2014 10:08 pm
by bn
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:

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
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

Re: Color space conversion

Posted: Thu Jul 17, 2014 1:41 pm
by Zryip TheSlug
Another solution which is not requiring to use a "real button" is to use the template button keyword:

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,

Re: Color space conversion

Posted: Thu Jul 17, 2014 6:10 pm
by dunbarx
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:

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
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

Re: Color space conversion

Posted: Thu Jul 17, 2014 11:33 pm
by [-hh]
..........

Re: Color space conversion

Posted: Fri Jul 18, 2014 12:59 am
by dunbarx
Hermann.

Great work.

As usual.

Craig

Re: Color space conversion

Posted: Fri Jul 18, 2014 1:20 am
by [-hh]
..........

Re: Color space conversion

Posted: Fri Jul 18, 2014 1:33 am
by dunbarx
Well, try these in a button script:

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
Craig

Re: Color space conversion

Posted: Fri Jul 18, 2014 1:55 am
by [-hh]
..........

Re: Color space conversion

Posted: Fri Jul 18, 2014 2:58 am
by Simon
Hi Craig,
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
(your code)
But maximize the stack.
Can't be good for the brain, it's the 60's all over again. :)

Simon

Re: Color space conversion

Posted: Fri Jul 18, 2014 5:09 pm
by jacque
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