使用下面的jQuery将获得元素背景颜色的RGB值:
$('#selector').css('backgroundColor');
有没有办法得到十六进制值而不是RGB?
使用下面的jQuery将获得元素背景颜色的RGB值:
$('#selector').css('backgroundColor');
有没有办法得到十六进制值而不是RGB?
当前回答
下面是一个ES6 one liner,它不使用jQuery:
var rgb = document.querySelector('#selector').style['background-color'];
return '#' + rgb.substr(4, rgb.indexOf(')') - 4).split(',').map((color) => parseInt(color).toString(16).padStart(2, '0')).join('');
其他回答
var hexDigits = new Array
("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
//Function to convert rgb color to hex format
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];
}
(源)
这是一个版本,也检查透明,我需要这个,因为我的对象是将结果插入到一个风格属性,其中十六进制颜色的透明版本实际上是“透明”字。
function rgb2hex(rgb) {
if ( rgb.search("rgb") == -1 ) {
return rgb;
}
else if ( rgb == 'rgba(0, 0, 0, 0)' ) {
return 'transparent';
}
else {
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
}
我漂亮的非标准解
HTML
<div id="selector" style="background-color:#f5b405"></div>
jQuery
$("#selector").attr("style").replace("background-color:", "");
结果
#f5b405
博士TL;
使用这个干净的单行函数,同时支持rgb和rgba:
const rgba2hex = (rgba) => `#${rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/).slice(1).map((n, i) => (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n)).toString(16).padStart(2, '0').replace('NaN', '')).join('')}`
2021年更新答案
从我最初回答这个问题到现在已经过去了很久。然后很酷的ECMAScript 5和2015+特性在浏览器上大量可用,比如箭头函数、数组。地图,字符串。padStart和模板字符串。从几年开始,可以编写跨浏览器的单行rgb2hex:
几点rgb2hex = (rgb) = > ' # {rgb美元。竞赛(d - ^ rgb (\), s * (\ d + s), \ * (d +) \)美元/片(1))。地图(n =>帕特森(n, 10), to弦(16)。padStart(2,‘0’)。加入(")的 //用你想要的… 控制台日志(rgb2hex (rgb(0,0,0)’) 控制台.log(rgb2hex('rgb(255, 255) 控制台日志(rgb2hex (rgb(255,0,0)’) 控制台(rgb2十六克('rgb(38,170, 90)
它的工作原理是使用正则表达式来获取rgb字符串中的每个数字,slice(1)只获得数字(匹配的第一个结果是完整的字符串本身),map迭代每个数字,每次迭代转换为parseInt的Number,然后返回到十六进制字符串(通过16进制转换),如果需要通过padStart添加零。最后,将每个转换/调整的数字连接到一个以'#'开头的唯一字符串。
当然,我们可以毫不费力地将其扩展为一行代码rgba2hex:
const rgba2hex = (rgba) => `#${rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/).slice(1).map((n, i) => (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n)).toString(16).padStart(2, '0').replace('NaN', '')).join('')}` // Now it doesn't matter if 'rgb' or 'rgba'... console.log(rgba2hex('rgb(0,0,0)')) console.log(rgba2hex('rgb(255, 255, 255)')) console.log(rgba2hex('rgb(255,0,0)')) console.log(rgba2hex('rgb(38, 170, 90)')) console.log(rgba2hex('rgba(255, 0, 0, 0.5)')) console.log(rgba2hex('rgba(0,255,0,1)')) console.log(rgba2hex('rgba(127,127,127,0.25)'))
就是这样。但如果您想深入了解旧式JavaScript世界,请继续阅读。
原2010年答案
以下是我根据@Matt的建议写的更清晰的解决方案:
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
一些浏览器已经将颜色返回为十六进制(Internet Explorer 8及以下版本)。如果你需要处理这些情况,只需要在函数中添加一个条件,就像@gfrobenius建议的那样:
function rgb2hex(rgb) {
if (/^#[0-9A-F]{6}$/i.test(rgb)) return rgb;
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
如果你正在使用jQuery并且想要一个更完整的方法,你可以使用自jQuery 1.4.3以来提供的CSS Hooks,就像我在回答这个问题时所展示的:我可以强制jQuery. CSS ("backgroundColor")以十六进制格式返回吗?
改进功能“hex”
function hex(x){
return isNaN(x) ? "00" : hexDigits[x >> 4] + hexDigits[x & 0xf];
// or option without hexDigits array
return (x >> 4).toString(16)+(x & 0xf).toString(16);
}