TL;DR: Just use this library. It is a mixed of what I said but with the complexity mentioned by Kaiido resolved: http://chir.ag/projects/ntc/
Keeping the original answer here but it won't work completely. See above.
Well, every color is a combination of 6 digits in hexa.
If you convert that number into an int, let's say, BEBEBE is 12500670.
You could then have a small table with every key/pair of known name and their corresponding integer value.
Then is just a matter of finding the closest integer number in your table.
Edit:
Basically, if you have your color picker and it says that the person chose 'BEBEBE' you would have to:
- Convert the hexa to decimal. There are multiple tutorials and online tools, I won't go into detail here. It is not very hard.
- You would then have a table, object, array, whatever you want sorted by decimal value and their correspondent name. Example:
http://cloford.com/resources/colours/500col.htm You could use this table that has the hexa codes and convert them into decimal.
[{
name: 'grey', hexa: 'bebebe', decimal: '12500670'
},
[...] //Insert more colors
]
- You could then iterate over your list comparing the decimal input by the user against the one you have and stop when you reach a number highers than yours, and you can store the one you just found and the previous one.
- You find the difference between the one you just found and the previous one, and the one with the less difference is the closest one
- Profit