<p>Problem is that people do not really agree on a 1:1 mapping of color names. But yes such tools exists, this is a simple nearest neighborhood test. You could use any spatial query module to do this. Here is a simple (naive) python example: </p>

<pre class="lang-py prettyprint-override"><code>from __future__ import print_function
import webcolors
from scipy.spatial import KDTree

# lets populate some names into spatial name database
hexnames = webcolors.css3_hex_to_names
names = []
positions = []

for hex, name in hexnames.iteritems():
    names.append(name)
    positions.append(webcolors.hex_to_rgb(hex))

spacedb = KDTree(positions)

# query nearest point
querycolor = (10,88, 200)
dist, index = spacedb.query(querycolor)

print('The color %r is closest to %s.'%(querycolor, names[index]))
</code></pre>

<p>Which results in: <code>The color (10, 88, 200) is closest to royalblue.</code></p>

<p>I'm using here the 140 css3 defined colors. Bigger and more comprehensive lists can be found <a href="https://en.wikipedia.org/wiki/Lists_of_colors" rel="nofollow">online</a>, if you want a smallerlist that would also work. Theres all kinds of possible things you could do, you could for example linearize the sRGB space before calculating distances to name. You could ask for 3 closest names and give double names if the names are halfway.</p>

CC BY-SA 3.0
2015-09-26T14:23:44.540Z
This will certainly help. Are you aware of any simpler hextocolor conversions, much narrower? Was thinking more like 8 or 16 different color names
David Lerner
CC BY-SA 3.0
2015-09-26T14:33:20.363Z
This would work with any number of colors.
joojaa
CC BY-SA 3.0
2015-09-26T16:11:13.527Z
No, no, I understand that. But I want to limit the results to an extremely limited palette consisting of red, blue, green, white, orange, purple, brown and grey, for example
David Lerner
CC BY-SA 3.0
2015-09-26T16:11:49.663Z
Web or CSS colors have a very high number of possible matches
David Lerner
CC BY-SA 3.0
2015-09-26T16:52:15.577Z
Yes, whats the problem? Instead of taking the color names form web colors supply your own
joojaa
CC BY-SA 3.0
2015-09-26T16:55:00.963Z
The problem? Brain not working properly, I think.   Makes perfect sense,  thanks!
David Lerner
CC BY-SA 3.0
2015-09-26T09:19:28.477Z
2015-09-26T14:34:19.023Z
2015-09-26T14:34:19.023Z
joojaa
<p>Forgive me in advance for my ignorance.</p>

<p>Using the PHP function imagecolorat, I have a solution that presently delivers a set of the most dominant hex color codes available inside of an image.</p>

<p>Is there a known library that will allow me to then take the hex color code, RGB code (or any other standard color code) and simplify them down, or rather "match" them to a limited set of "common colors", such as red, brown, green, yellow, grey, blue, etc?</p>

<p>For example, #944D70, which is a shade of purple, would be matched to the color 'purple'.</p>

<p><strong>Alternatively:</strong></p>

<p>Is there a particular pattern or range to RGB colors that would allow me to write such a library myself?</p>

<p>For example, 0,0,0 thru 20,20,20 might all be considered a "black" color. However, it seems as if organizing such a library would be very exhausting if done manually.</p>

CC BY-SA 3.0
2015-09-26T21:19:58.773Z
Not a full answer, but [color surveys like this one from xkcd](http://blog.xkcd.com/2010/05/03/color-survey-results/) may give some approximation if you're prepared to do a fair bit of preprocessing to exclude meaningless results and narrow down to the number of colors you require.
trichoplax is on Codidact now
<conversion>
Convert RGB, Hex or any other color format to "Standard Color" Programmatically
David Lerner
# Convert RGB, Hex or any other color format to "Standard Color" Programmatically

(Asked by: username_0 on 2015-09-26)

Forgive me in advance for my ignorance.

Using the PHP function imagecolorat, I have a solution that presently delivers a set of the most dominant hex color codes available inside of an image.

Is there a known library that will allow me to then take the hex color code, RGB code (or any other standard color code) and simplify them down, or rather "match" them to a limited set of "common colors", such as red, brown, green, yellow, grey, blue, etc?

For example, #944D70, which is a shade of purple, would be matched to the color 'purple'.

**Alternatively:**

Is there a particular pattern or range to RGB colors that would allow me to write such a library myself?

For example, 0,0,0 thru 20,20,20 might all be considered a "black" color. However, it seems as if organizing such a library would be very exhausting if done manually.

**Tags:** conversion

> **Comments:**
> **username_1**: Not a full answer, but [color surveys like this one from xkcd](http://blog.xkcd.com/2010/05/03/color-survey-results/) may give some approximation if you're prepared to do a fair bit of preprocessing to exclude meaningless results and narrow down to the number of colors you require.  

---

## 1 answers

### Answer by username_2 on 2015-09-26. Score: 5 | Accepted answer

Problem is that people do not really agree on a 1:1 mapping of color names. But yes such tools exists, this is a simple nearest neighborhood test. You could use any spatial query module to do this. Here is a simple (naive) python example:

```py
from __future__ import print_function
import webcolors
from scipy.spatial import KDTree

# lets populate some names into spatial name database
hexnames = webcolors.css3_hex_to_names
names = []
positions = []

for hex, name in hexnames.iteritems():
    names.append(name)
    positions.append(webcolors.hex_to_rgb(hex))

spacedb = KDTree(positions)

# query nearest point
querycolor = (10,88, 200)
dist, index = spacedb.query(querycolor)

print('The color %r is closest to %s.'%(querycolor, names[index]))

```

Which results in: `The color (10, 88, 200) is closest to royalblue.`

I'm using here the 140 css3 defined colors. Bigger and more comprehensive lists can be found [online](https://en.wikipedia.org/wiki/Lists_of_colors), if you want a smallerlist that would also work. Theres all kinds of possible things you could do, you could for example linearize the sRGB space before calculating distances to name. You could ask for 3 closest names and give double names if the names are halfway.

> **Comments:**
> **username_0**: This will certainly help. Are you aware of any simpler hextocolor conversions, much narrower? Was thinking more like 8 or 16 different color names  
> **username_2**: This would work with any number of colors.  
> **username_0**: No, no, I understand that. But I want to limit the results to an extremely limited palette consisting of red, blue, green, white, orange, purple, brown and grey, for example  
> **username_0**: Web or CSS colors have a very high number of possible matches  
> **username_2**: Yes, whats the problem? Instead of taking the color names form web colors supply your own  
> **username_0**: The problem? Brain not working properly, I think.   Makes perfect sense,  thanks!  

---

