Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HTML5 color picker to return color name instead of color code?

<label for="background-color">Choose a color for background :</label> 
<input id="background-color" type="color" /> 

I want this to return the color name instead of color code e.g. "Blue" instead of "#0000ff".

like image 506
Gael Musi Avatar asked May 05 '16 07:05

Gael Musi


2 Answers

$(document).ready(function(){
$("#showPaletteOnly").spectrum({
    showPaletteOnly: true,
    showPalette:true,
    hideAfterPaletteSelect:true,
    color: 'blanchedalmond',
change: function(color) {
        printColor(color);
    },
    palette:["red", "green", "blue"],
});
});

function printColor(color) {
alert(color.toName());
   //var text = "You chose... " + color.toHexString();    
   //$(".label").text(text);
    
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://bgrins.github.io/spectrum/spectrum.js"></script>
<link rel="stylesheet" type="text/css" href="https://bgrins.github.io/spectrum/spectrum.css">

<h2>Palette Only</h2>
<input type='text' id="showPaletteOnly"/>

<br />
<span class='label'>Choose a color</span>
like image 160
Akram Khan Avatar answered Sep 24 '22 08:09

Akram Khan


You can use a java script library "Name that color".

<script type="text/javascript">

  var n_match  = ntc.name("#6195ED");
  n_rgb        = n_match[0]; // RGB value of closest match
  n_name       = n_match[1]; // Text string: Color name
  n_exactmatch = n_match[2]; // True if exact color match

  alert(n_match);

</script>
like image 39
shah Avatar answered Sep 22 '22 08:09

shah