Color Name

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

tyarmsteadBUSuSfT
Posts: 135
Joined: Sat Aug 25, 2012 1:14 am

Color Name

Post by tyarmsteadBUSuSfT » Thu Dec 10, 2015 9:47 pm

Is there a way in LC to input the RGB and have LC return the closest color name win LC? I've seen and am using the conversion the other way, but would like to display a valid LC name is possible in come cases.
Thanks
Ty

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6189
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Color Name

Post by dunbarx » Fri Dec 11, 2015 12:36 am

Hi.

"Red" is "255,0,0". If you mean that anything within, say 20 units of any or all of those three values would return "red", whereas something a little farther away might return "orange", then you will have to roll your own.

But this should be fun, and likely useful to others. Can you do it?

Craig Newman

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

Re: Color Name

Post by Simon » Fri Dec 11, 2015 3:41 am

Rats, I wish I had more time!
This does sound fun.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

AndyP
Posts: 449
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Color Name

Post by AndyP » Fri Dec 11, 2015 8:54 am

Hi Ty,

Here's a basic list with color name, hex and rgb values

Black,#000000,0,0,0
White,#FFFFFF,255,255,255
Red,#FF0000,255,0,0
Lime,#00FF00,0,255,0
Blue,#0000FF,0,0,255
Yellow,#FFFF00,255,255,0
Cyan,#00FFFF,
Magenta,#FF00FF,255,0,255
Silver,#C0C0C0,192,192,192
Gray,#808080,128,128,128
Maroon,#800000,128,0,0
Olive,#808000,128,128,0
Green,#008000,0,128,0
Purple,#800080,128,0,128
Teal,#008080,0,128,128
Navy,#000080,0,0,128

I thought this question was an interesting challenge, so later today I will post a link to a converter I'm working on right now, which will allow you to enter either a hex or rgb value and come up with the exact color name and the nearest to the basic color name list as above... stay tuned.
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

AndyP
Posts: 449
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Color Name

Post by AndyP » Fri Dec 11, 2015 11:24 am

Ok, after some investigation LC has the the 'colornames' function which returns a long list of all the color names LC uses.

There are multiple names with variations as below

AntiqueWhite
AntiqueWhite1
AntiqueWhite2
AntiqueWhite3
AntiqueWhite4

If you strip out the variations the list you are left with pretty well a match for the CSS3 color names list, so at the moment I working with this.
I have the CSS3 color names and corresponding rgb and hex values in a table so finding the color name for for a corresponding rgb or hex value is easy. If however a rgb or hex value entered does not produce an exact match then the nearest color name match has to be calculated. I'm in the process of doing this now.

This would have been easy in Python as you could use rgb_to_name((0, 0, 0)) to get the rgb value directly.
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

tyarmsteadBUSuSfT
Posts: 135
Joined: Sat Aug 25, 2012 1:14 am

Re: Color Name

Post by tyarmsteadBUSuSfT » Sat Dec 12, 2015 12:15 am

Wow, that is great. I tried to use the Colorname but I must not have coded incorrectly.Thank you for your response, it will go a long way. I'm glad that you found it interesting as I considered not sending it thinking it was too trivial for the board.
Thank you again
Ty

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Color Name

Post by WaltBrown » Sat Dec 12, 2015 2:37 am

I did this very cumbersome process. It basically sets the color of a 20x20 graphic object (graphic "grcRect"), takes a 20x20 snapshot, and then gets the color values from the bytes of the image data of the snapshot at an arbitrary pixel - in this case the 51st out of 400. I couldn't find an easier way.

Code: Select all

on mouseUp
   local tColorNames, tImageData, tWindow,tRect, tLine
   
   put the colorNames into tColorNames
   put empty into fld "fColors"
   put the windowID of this stack into tWindow
   put the rect of grc "gRect" into tRect
   repeat for each line tLine in tColorNames
      set the backColor of grc "gRect" to tLine
      import snapshot from rectangle tRect of window tWindow
      put the imageData of the last image into tImageData
      delete the last image
      put tLine && comma & space after fld "fColors"

 //  Get pixel 201 for Alpha data if desired
      put byteToNum(byte 202 of tImageData) & comma & space after fld "fColors"
      put byteToNum(byte 203 of tImageData) & comma & space after fld "fColors"
      put byteToNum(byte 204 of tImageData) & return after fld "fColors"
   end repeat
end mouseUp
If anyone knows an easier way, I'd love to know.
Walt Brown
Omnis traductor traditor

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6189
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Color Name

Post by dunbarx » Sat Dec 12, 2015 5:35 am

I think the OP wanted simply to input an RGB value, and find the closest color match. I may have this wrong.

But if so, the solution is relatively straightforward. Compare triplets of values with a small offset through the list of colors, and return one if all three get close.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2125
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Color Name

Post by [-hh] » Sat Dec 12, 2015 12:09 pm

Craig, what you said.

RaspberryPi Stacks collection #40 = hhClosestColorNames2b.livecode

Simon, it is interesting, especially because the result is not unique.
shiftLock happens

AndyP
Posts: 449
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Color Name

Post by AndyP » Sat Dec 12, 2015 10:38 pm

Mental note: must check out the Raspberry Pi stacks.

This is the formula I was working with sqrt( (r1 - r2)2 + (g1 - g2)2 + (b1 - b2)2 )
which I found here http://stackoverflow.com/questions/4057 ... of-colours which is an interesting read.
And this is the function converted to LiveCode as in the hh Raspberry Pi stack for comparison.

function hhColorDistance2 c1,c2
# This is sumOfAbs --> easy to interpret numerically
-- return abs((item 1 of c1) - (item 1 of c2)) \
-- + abs((item 2 of c1) - (item 2 of c2)) \
-- + abs((item 3 of c1) - (item 3 of c2))
# This is distance in 3D:
# Easy to interpret geometrically (2-norm = euclidian norm)
return round(sqrt( ((item 1 of c1) - (item 1 of c2))^2 \
+ ((item 2 of c1) - (item 2 of c2))^2 \
+ ((item 3 of c1) - (item 3 of c2))^2 ))
end hhColorDistance2

So thanks to hh .. job done.
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

tyarmsteadBUSuSfT
Posts: 135
Joined: Sat Aug 25, 2012 1:14 am

Re: Color Name

Post by tyarmsteadBUSuSfT » Wed Dec 16, 2015 6:20 pm

I was away, but thank you very much for your help with this issue.
Ty

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3639
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Color Name

Post by richmond62 » Wed Dec 16, 2015 7:43 pm

Why, if I set the backGroundColor of an object using a 3 number code (RGB)
am I able to do a:

put the backGroundColor and get those 3 values,

while if I set the backGroundColor of an object using a colorName
am I UNABLE to get the RGB values using a:

put the backGroundColor ???

This is the monkey in the woodpile to RGB <> colorNames.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3639
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Color Name

Post by richmond62 » Wed Dec 16, 2015 7:59 pm


bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3317
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Color Name

Post by bn » Wed Dec 16, 2015 8:14 pm

this will give you the RGB values of a color name

Code: Select all

function getRGB pColorName 
   set the backColor of the templateGraphic to pColorName
   set the backPixel of the templateGraphic to (the effective backpixel of the templateGraphic)
   put the backColor of the templateGraphic into tRGB
   return tRGB
end getRGB
it is a slight variation on code by Ken Ray, there is also a version by Sarah Reichelt on the use-list, I think they all go back to a suggestion by Dr Raney. Here it uses the templateGraphic, the original created a graphic on the fly and deleted it.

Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3639
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Color Name

Post by richmond62 » Wed Dec 16, 2015 8:19 pm

I am getting thoroughly fed up as don't quite see why this only seems to work for "White":
argyBEE.png
Attachments
COLOR Q.zip
Here's the stack to play with.
(5.15 KiB) Downloaded 48 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”