將圖像拖入框中後,請參考www.granjacreativa.com/damepaleta ,我們將顯示主色和調色板。如何顯示每種顯示顏色的HEX值? 我想在每個的顏色框下顯示它們。顯示圖像調色板的HEX值
謝謝
將圖像拖入框中後,請參考www.granjacreativa.com/damepaleta ,我們將顯示主色和調色板。如何顯示每種顯示顏色的HEX值? 我想在每個的顏色框下顯示它們。顯示圖像調色板的HEX值
謝謝
嘗試以下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
更新從你你想swatch background color
在hex
,那麼你應該得到colors
後嘗試site source
使用,
alert(rgb2hex($('.swatch').css('background-color')));
讓所有swatch background-color
使用$.each()
你可以得到的背景顏色和顯示十六進制,這裏是一個JavaScript代碼段:
$('.swatch').on('click',
function(){
var context = document.createElement('canvas').getContext('2d');
context.strokeStyle = $(this).css('backgroundColor');
alert(context.strokeStyle);
}).css('cursor', 'pointer');
我們將利用帆布,因爲是簡單的獲得實際的十六進制顏色。
謝謝,但我該如何將它添加到我的代碼? – user3140235
嘗試像'alert(rgb2hex($('#yourelement').css('background-color')));' –
btw。函數hex(x){return('0'+(+ x || 0).toString(16))。slice(-2); } –