Color space conversion

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Color space conversion

Post by kaveh1000 » Wed Jul 16, 2014 10:05 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Color space conversion

Post by dunbarx » Wed Jul 16, 2014 6:40 pm

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Color space conversion

Post by kaveh1000 » Wed Jul 16, 2014 7:10 pm

Hi Craig

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Color space conversion

Post by bn » Wed Jul 16, 2014 10:08 pm

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

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Color space conversion

Post by Zryip TheSlug » Thu Jul 17, 2014 1:41 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Color space conversion

Post by dunbarx » Thu Jul 17, 2014 6:10 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Color space conversion

Post by [-hh] » Thu Jul 17, 2014 11:33 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Color space conversion

Post by dunbarx » Fri Jul 18, 2014 12:59 am

Hermann.

Great work.

As usual.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Color space conversion

Post by [-hh] » Fri Jul 18, 2014 1:20 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Color space conversion

Post by dunbarx » Fri Jul 18, 2014 1:33 am

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Color space conversion

Post by [-hh] » Fri Jul 18, 2014 1:55 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:35 pm, edited 1 time in total.
shiftLock happens

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Color space conversion

Post by Simon » Fri Jul 18, 2014 2:58 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Color space conversion

Post by jacque » Fri Jul 18, 2014 5:09 pm

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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply