2014-01-07 83 views

回答

0

嘗試以下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 colorhex,那麼你應該得到colors後嘗試site source使用,

alert(rgb2hex($('.swatch').css('background-color'))); 

讓所有swatch background-color使用$.each()

+0

謝謝,但我該如何將它添加到我的代碼? – user3140235

+0

嘗試像'alert(rgb2hex($('#yourelement').css('background-color')));' –

+0

btw。函數hex(x){return('0'+(+ x || 0).toString(16))。slice(-2); } –

0

你可以得到的背景顏色和顯示十六進制,這裏是一個JavaScript代碼段:

$('.swatch').on('click', 
    function(){ 
     var context = document.createElement('canvas').getContext('2d'); 
     context.strokeStyle = $(this).css('backgroundColor'); 
     alert(context.strokeStyle); 
}).css('cursor', 'pointer'); 

我們將利用帆布,因爲是簡單的獲得實際的十六進制顏色。