2016-05-05 14 views
6
<label for="background-color">Choose a color for background :</label> 
<input id="background-color" type="color" /> 

Tôi muốn điều này đổi tên màu thay vì mã màu ví dụ: "Màu xanh" thay vì "# 0000ff".Cách tạo bộ chọn màu HTML5 để trả về tên màu thay vì mã màu?

+4

điều gì sẽ xảy ra nếu màu được chọn không có tên thân thiện? –

+0

Nếu bạn muốn chỉ cho phép một tập hợp các màu cụ thể, hãy sử dụng menu thả xuống ... – SamGhatak

+0

Bạn đã thử những gì cho đến nay? Mã nguồn trả lại mã màu ở đâu? – Hassaan

Trả lời

2

$(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>

+0

Cảm ơn đã giúp tôi –

+0

nếu bạn hài lòng bỏ phiếu cho tôi. –

1

Bạn có thể sử dụng một java script thư viện "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> 
Các vấn đề liên quan