Function Repository Resource:

NearestColorName

Source Notebook

Find the color name closest to a given color

Contributed by: Renan Soares Germano

ResourceFunction["NearestColorName"][color]

returns the name of color based on the similarity to the built-in Wolfram Language color symbols.

Details and Options

The function only accepts RGBColor expressions.
Internally, the function implementation uses a list of all named colors of the Wolfram Language to define the nearest color for a given input. A named color is a color that has a specific symbol built into the Wolfram Language (e.g. Red).
The ColorMap option takes an Association with keys of type RGBColor and color Entity values.
The Language option can be used with a String to get the translated color name.
This function is Listable.
The color distance is calculated by Nearest.
The default "ColorMap" option value was built in using the procedure shown in the Author Notes.

Examples

Basic Examples (3) 

Find the nearest built-in color for an RGB color:

In[1]:=
ResourceFunction["NearestColorName"][
RGBColor[0.536757463400344, 0.43489008715454225`, 0.7325689833763229]]
Out[1]=
Image

Get the nearest color name for several colors:

In[2]:=
ResourceFunction["NearestColorName"]@{Red, Green, Blue, Orange, Pink}
Out[2]=
Image

Use random colors:

In[3]:=
TableForm[{#, ResourceFunction["NearestColorName"]@#} & /@ RandomColor[5]]
Out[3]=
Image

Options (4) 

ColorMap (2) 

By default, the "ColorMap" option contains the rules for all the named colors of the Wolfram Language:

In[4]:=
defaultRules = "ColorMap" /. Options@ResourceFunction["NearestColorName"]
Out[4]=
Image

Use the "ColorMap" option to get the nearest color names only for the primary colors:

In[5]:=
primaryRules = <|
  Red -> Entity["WolframLanguageSymbol", "Red"],
  Blue -> Entity["WolframLanguageSymbol", "Blue"],
  Yellow -> Entity["WolframLanguageSymbol", "Yellow"]|>
Out[5]=
Image
In[6]:=
TableForm[{#, ResourceFunction["NearestColorName"][#, ColorMap -> primaryRules]} & /@ RandomColor[10]]
Out[6]=
Image

It also is possible to expand the list of color rules, increasing the accuracy of the function. Define many gray values:

In[7]:=
allGrayColors = RGBColor[#, #, #] & /@ Range[0, 1, 1/255]
Out[7]=
Image

Expand the color rules to add every gray color:

In[8]:=
defaultRules = "ColorMap" /. Options@ResourceFunction["NearestColorName"]; expandedRules = AppendTo[defaultRules, # -> Entity["WolframLanguageSymbol", "Gray"] & /@ allGrayColors];

Using the default rules, random gray colors give several different results:

In[9]:=
randomgrays = Table[ColorConvert[GrayLevel[RandomReal[]], RGBColor], {20}];
In[10]:=
ResourceFunction["NearestColorName"][randomgrays] // DeleteDuplicates
Out[10]=
Image

Including the expanded rules gives more uniform results:

In[11]:=
ResourceFunction["NearestColorName"][randomgrays, ColorMap -> expandedRules] // DeleteDuplicates
Out[11]=
Image

Language (2) 

Get the nearest color name in Portuguese for red, green and blue colors:

In[12]:=
ResourceFunction["NearestColorName"][{Red, Green, Blue}, Language -> "Portuguese"] // TableForm
Out[12]=
Image

Get the nearest color name in Spanish for some random colors:

In[13]:=
ResourceFunction["NearestColorName"][RandomColor[5], Language -> "Spanish"] // TableForm
Out[13]=
Image

Possible Issues (1) 

Some colors do not have translations in all languages. The following code returns the available translations for a color:

In[14]:=
colorName = "Red";
WolframLanguageData[colorName]["Translations"][[All, 1]] // Sort
Out[13]=
Image

Neat Examples (1) 

Create a table of results:

In[15]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/79f89008-2d16-4de1-9287-93cf317acfb7"]
Out[6]=
Image

Publisher

Wolfram Summer School

Version History

  • 1.0.0 – 30 July 2020

Related Resources

Author Notes

Default ColorMap definition

Get every Wolfram Language symbol entity:

In[1]:=
wlSymbols = WolframLanguageData[];

Get the functionality areas value for every Wolfram Language symbol entity:

In[2]:=
wlSymbolsFunctionalityAreas = WolframLanguageData[wlSymbols, EntityProperty["WolframLanguageSymbol", "FunctionalityAreas"]];

Create a new list with two values: a symbol entity and the functionality areas for the respective symbol entity:

In[3]:=
symbolFunctionalityAreasMap = MapThread[{#1, #2} &, {wlSymbols, wlSymbolsFunctionalityAreas}];

Select the symbols that has only the value "ColorSymbols" in the functionality areas:

In[4]:=
colorSymbols = Select[symbolFunctionalityAreasMap, ContainsOnly[Last@#, {"ColorSymbols"}] &]
Image

Select only the entities that are colors:

In[5]:=
namedColors = Select[colorSymbols, ColorQ@ToExpression@First[#]@"Name" &][[All, 1]]
Image

Creating the Association

First, transform each color entity in a RGBColor. Here, two different manners are used to translate a named color symbol entity into a RGBColor: (1) get the Name value for the entity and use it as a parameter to the RGBColor function (stored in c1); (2) get the Name value for the entity and transform it into an Expression that will return the named color symbol ( stored in c2). When c1 is different of c2, both are considered for the next step.

In[6]:=
colors = Block[
    {c1, c2, colors},
    c1 = RGBColor@#@"Name";
    c2 = ToExpression@#@"Name";
    colors = {ColorQ@c1, ColorQ@c2, List @@ c1 == List @@ c2} /. {
       {True, False, _} | {True, True, True} -> {c1},
       {False, True, _} -> {c2},
       {True, True, False} -> {c1, c2}}
    ] & /@ namedColors
Image

Then, map each RGBColor to the respective symbol entity.

In[7]:=
defaultColorMapOptionValue = Association@
  Flatten@MapThread[
    Function[{colors, entity}, # -> entity & /@ colors], {colors, namedColors}]
defaultColorMapOptionValue == ("ColorMap" /. Options@NearestColorName)
Image
Image

License Information