0

I have a colour field in form, it saves the colour name as hex value. I want to display the colour name in index and show page. How to convert hex values into colour names? I use gem 'jquery-minicolors-rails'.
In form,

<%= f.color_field :syllab_color, :id => 'Color' %>  

in index and show page,

<%= @syllab.syllab_color.to_s %>

Thanks.

Prabha
  • 21
  • 13
  • What do you mean by color name? Like `red`, `blue`? I'm 90% sure about this, I don't think every hex value has a name on it, so you might probably need to be satisfied with the hex value prepended by `#`. – Ace Dimasuhid Oct 17 '16 at 10:03
  • When I pick colour from field it saves like #d57df0. Now I want display the hex value as colour name. – Prabha Oct 17 '16 at 10:06
  • Can you give an example of colour name? Or is it as `colour_name` field? – Ace Dimasuhid Oct 17 '16 at 10:07
  • If I select #f0f8ff from colour picker, I want to convert it into "Alice Blue", the corresponding colour name – Prabha Oct 17 '16 at 10:10
  • Ahh, then this might help you: http://stackoverflow.com/questions/5486730/convert-rgb-to-colorname-string-javascript – Ace Dimasuhid Oct 17 '16 at 10:14
  • If normal hex value conversion, we can use .to_s(16) or to_byte_string etc. Is there any possible ways? – Prabha Oct 17 '16 at 10:14

1 Answers1

0

jquery-minicolors-rails has a lot of useful colour manipulation methods, but does not contain a database for colour names. Lots of colours do have names assigned to them e.g. Wikipedia List of colors:A-F, but you would have to find or create a database listing them.

And as Ace Dimasuhid mentioned, not every 24-bit colour has a named assigned to it. I think only a few thousand out of 16million+ colours have names, so you may consider writing an algorithm to find the nearest named colours to a given unnamed hex value.

ColorHexa does this very well.