tags:

views:

350

answers:

6

Hello all,

I am not sure if its possible but I would like to create some sort of function where you pass in a string such as "yellow" or "green" and it returns its hex code or the range of hex codes that greens or yellows fit into.

Is this possible in PHP? If not possible, I am willing to try anything else that can do this!

Thanks all

+2  A: 

it is possible in PHP, if you have a database that contain all these colors.
So, you just have to get that database somewhere

Col. Shrapnel
Interesting, know where to get the database!
Abs
+4  A: 

There is no way to my knowledge to convert HTML color names or CSS color names to their Hex or RGB value in PHP. You'd have to create a map that knows the names (see Wiki article)

Further reference:

Edit apparently someone has done this work for you already:

Gordon
Wow, awesome! Just need to wrap it around another rgb to hex converter function!...Or just use it as it is!
Abs
In case anyone needs this function, the first contributor (Halit YEL) wrote a great function for rgb to hex on the php.net site: http://php.net/manual/en/function.hexdec.php
Abs
+1  A: 

You may want to use the 16 basic colours defined in the HTML standard.

legoscia
Hmm, that's an idea - I am sure no one will be looking for grey4 - but rather just grey.
Abs
+1  A: 

you can find all color names and their hex value on this site: http://www.iconbazaar.com/color_tables/lepihce.html

with some regexp it should be possible to use this to build a "database" of any kind.

EDIT: http://answers.oreilly.com/topic/453-color-names-and-rgb-values-with-wide-browser-support/

also a nice list, seems to be easier to import to eg. csv.

Rufinus
A: 

I think the easiest solution is to build your own look-up table for the colors you plan on having as inputs.

Also allowing the hex color code as an input will allow the user to enter in any color they want.

Seth M.
A: 

You're a hop, skip and a jump away from doing this.

Hop:
Go to your favorite search engine and search for color name to rgb mapping in order to find a page that displays the info in a barebones format (preferably simple table html or txt).

Skip:
Write a php script to parse the page into a more searchable format (like SQL or an array).

Jump:
Now create a function that takes a string as an argument and returns the appropriate hex codes.

or

Sit:
Just go the site that Gordon graciously searched for and added to his answer.

webbiedave