使用以下jQuery将获得元素背景颜色的RGB值:
$('#selector').css('backgroundColor');
有没有办法得到十六进制值而不是RGB?
发布于 2009-11-16 15:55:45
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];
}
(Source)
发布于 2010-09-02 21:52:22
TLDR
使用这个干净的单行函数,同时支持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+特性就可以在arrow functions、Array.map、String.padStart和template strings等浏览器上大量使用了。所以现在可以编写一个单行rgb2hex
const rgb2hex = (rgb) => `#${rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/).slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, '0')).join('')}`
// Use as you wish...
console.log(rgb2hex('rgb(0,0,0)'))
console.log(rgb2hex('rgb(255, 255, 255)'))
console.log(rgb2hex('rgb(255,0,0)'))
console.log(rgb2hex('rgb(38, 170, 90)'))
基本上,我们使用正则表达式来获取rgb
字符串中的每个数字,使用正则表达式slice(1)
来仅获取数字( match
的第一个结果是完整的字符串本身),使用map
来迭代每个数字,每次迭代使用parseInt
转换为Number
,然后返回到十六进制的String
(通过基数16转换),如果需要通过padStart
添加零。最后,只需将每个转换/调整后的数字join
为以'#'
开头的唯一String
。
当然,我们可以毫不费力地将其扩展为一行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 suggestion写的更简洁的解决方案:
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,正如我在回答这个问题时所展示的那样:Can I force jQuery.css("backgroundColor") returns on hexadecimal format?
发布于 2010-11-04 03:01:22
大多数浏览器在使用以下命令时似乎都会返回RGB值:
$('#selector').css('backgroundColor');
只有I.E (到目前为止只测试了6个)返回十六进制值。
为了避免在i.e.中出现错误消息,您可以将函数包装在if语句中:
function rgb2hex(rgb) {
if ( rgb.search("rgb") == -1 ) {
return rgb;
} 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]);
}
}
https://stackoverflow.com/questions/1740700
复制相似问题