I’m making a telegram bot and I would like for it to be able to give a hex code against a color name. I know there are a heck of a lot of rgb colors but I also know there is a wikipedia article with known color names and hex codes, if I could be able to get the pages as a json it would help. Also, if the json says hex code = color name how can I invert that? How can I search by the value and not the name?
Question posted in Telegram API
A comprehensive official documentation can be found here.
A comprehensive official documentation can be found here.
3
Answers
You could write your own Hash then Hash#invert it:
Or without creating the inverse hash:
You can try following :
The hash can be inverted as follow :
output :
output :
The other answers are fine. If you’re looking for a gem that’s already done the work for you, though, take a look at Color. Its
Color::CSS[]
method looks up a color by name and returns a Color::RGB object, which in turn hashex
andhtml
methods:Color::RGB also has a
by_hex
static method, which will return a named Color::RGB object for the given hex code. Thename
method returns the name (if it has one):You can see it in action on repl.it: https://repl.it/@jrunning/EqualReasonableSpellchecker (If you get an error the first time you hit the run button, hit it again. repl.it sometimes has trouble with loading gems the first time.)