【发布时间】:2014-01-24 17:58:46
【问题描述】:
请参考www.granjacreativa.com/damepaleta 将图像拖入框中后,我们会显示主色以及调色板。如何显示每种显示颜色的 HEX 值? 我想将它们显示在每个的颜色框下。
谢谢
【问题讨论】:
标签: javascript php jquery colors palette
请参考www.granjacreativa.com/damepaleta 将图像拖入框中后,我们会显示主色以及调色板。如何显示每种显示颜色的 HEX 值? 我想将它们显示在每个的颜色框下。
谢谢
【问题讨论】:
标签: javascript php jquery colors palette
下面试试functions
var hexDigits = new Array
("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
阅读Convert jQuery RGB output to Hex Color
更新从你site source你想在hex中得到swatch background color,那么你应该在得到colors之后尝试,
alert(rgb2hex($('.swatch').css('background-color')));
要获取所有swatch background-color,请使用$.each()
【讨论】:
alert(rgb2hex($('#yourelement').css('background-color')));
你可以获取背景颜色和显示十六进制,这里是一个javascript sn-p:
$('.swatch').on('click',
function(){
var context = document.createElement('canvas').getContext('2d');
context.strokeStyle = $(this).css('backgroundColor');
alert(context.strokeStyle);
}).css('cursor', 'pointer');
我们将使用画布,因为更容易获得实际的十六进制颜色。
【讨论】: