Color Name
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 151
- Joined: Sat Aug 25, 2012 1:14 am
Color Name
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
Thanks
Ty
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Color Name
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
"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
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Color Name
Rats, I wish I had more time!
This does sound fun.
Simon
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!
Re: Color Name
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.
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
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
Re: Color Name
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.
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
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
-
- Posts: 151
- Joined: Sat Aug 25, 2012 1:14 am
Re: Color Name
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
Thank you again
Ty
Re: Color Name
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.
If anyone knows an easier way, I'd love to know.
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
Walt Brown
Omnis traductor traditor
Omnis traductor traditor
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Color Name
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
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
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Color Name
Craig, what you said.
RaspberryPi Stacks collection #40 = hhClosestColorNames2b.livecode
Simon, it is interesting, especially because the result is not unique.
RaspberryPi Stacks collection #40 = hhClosestColorNames2b.livecode
Simon, it is interesting, especially because the result is not unique.
shiftLock happens
Re: Color Name
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.
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
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search
-
- Posts: 151
- Joined: Sat Aug 25, 2012 1:14 am
Re: Color Name
I was away, but thank you very much for your help with this issue.
Ty
Ty
-
- Livecode Opensource Backer
- Posts: 9287
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Color Name
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.
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.
-
- Livecode Opensource Backer
- Posts: 9287
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
-
- VIP Livecode Opensource Backer
- Posts: 3990
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Color Name
this will give you the RGB values of a color name
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
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
Kind regards
Bernd
-
- Livecode Opensource Backer
- Posts: 9287
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Color Name
I am getting thoroughly fed up as don't quite see why this only seems to work for "White":
- Attachments
-
- COLOR Q.zip
- Here's the stack to play with.
- (5.15 KiB) Downloaded 220 times