158 javascript rgb jquery hex colors
使用以下jQuery将获得元素背景颜色的RGB值:
$('#selector').css('backgroundColor');
Run Code Online (Sandbox Code Playgroud)
有没有办法获得十六进制值而不是RGB?
Eri*_*lli 154
这是我根据@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]);
}
Run Code Online (Sandbox Code Playgroud)
某些浏览器已将颜色返回为十六进制(从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]);
}
Run Code Online (Sandbox Code Playgroud)
如果你正在使用jQuery并想要一个更完整的方法,你可以使用自jQuery 1.4.3以来可用的CSS Hook,正如我在回答这个问题时所表明的那样:我可以强制jQuery.css("backgroundColor")以十六进制格式返回吗?
Dan*_*ott 135
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];
}
Run Code Online (Sandbox Code Playgroud)
(来源)
小智 59
使用时,大多数浏览器似乎都会返回RGB值:
$('#selector').css('backgroundColor');
Run Code Online (Sandbox Code Playgroud)
只有IE(目前只测试了6个)返回Hex值.
要避免IE中的错误消息,可以将函数包装在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]);
}
}
Run Code Online (Sandbox Code Playgroud)
Zac*_*atz 21
更新了@ErickPetru以获得rgba兼容性:
function rgb2hex(rgb) {
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]);
}
Run Code Online (Sandbox Code Playgroud)
我更新了正则表达式以匹配alpha值(如果已定义),但不使用它.
这是一个不使用jQuery的ES6 one liner:
var rgb = document.querySelector('#selector').style['background-color'];
return '#' + rgb.substr(4, rgb.indexOf(')') - 4).split(',').map((color) => parseInt(color).toString(16)).join('');
Run Code Online (Sandbox Code Playgroud)
// c - color str e.g."rgb(12,233,43)", result color hex e.g. "#0ce92b"
let rgb2hex=c=>'#'+c.match(/\d+/g).map(x=>(+x).toString(16).padStart(2,0)).join``
Run Code Online (Sandbox Code Playgroud)
// c - color str e.g."rgb(12,233,43)", result color hex e.g. "#0ce92b"
let rgb2hex=c=>'#'+c.match(/\d+/g).map(x=>(+x).toString(16).padStart(2,0)).join``
Run Code Online (Sandbox Code Playgroud)
这是一个也检查透明的版本,我需要这个,因为我的对象是将结果插入样式属性,其中十六进制颜色的透明版本实际上是“透明”这个词。
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
以十六进制返回元素背景颜色的函数。
function getBgColorHex(elem){
var color = elem.css('background-color')
var hex;
if(color.indexOf('#')>-1){
//for IE
hex = color;
} else {
var rgb = color.match(/\d+/g);
hex = '#'+ ('0' + parseInt(rgb[0], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[2], 10).toString(16)).slice(-2);
}
return hex;
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
$('#div1').click(function(){
alert(getBgColorHex($(this));
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个使用可读基本函数且没有正则表达式的函数。
该函数接受十六进制、rgb或 rgba CSS 格式的颜色并返回十六进制表示形式。
编辑:解析 rgba() 格式时存在错误,已修复...
function getHexColor( color ){
//if color is already in hex, just return it...
if( color.indexOf('#') != -1 ) return color;
//leave only "R,G,B" :
color = color
.replace("rgba", "") //must go BEFORE rgb replace
.replace("rgb", "")
.replace("(", "")
.replace(")", "");
color = color.split(","); // get Array["R","G","B"]
// 0) add leading #
// 1) add leading zero, so we get 0XY or 0X
// 2) append leading zero with parsed out int value of R/G/B
// converted to HEX string representation
// 3) slice out 2 last chars (get last 2 chars) =>
// => we get XY from 0XY and 0X stays the same
return "#"
+ ( '0' + parseInt(color[0], 10).toString(16) ).slice(-2)
+ ( '0' + parseInt(color[1], 10).toString(16) ).slice(-2)
+ ( '0' + parseInt(color[2], 10).toString(16) ).slice(-2);
}
Run Code Online (Sandbox Code Playgroud)
与 @Jim F 答案相同的答案 ,但使用ES6语法,因此,指令较少:
const rgb2hex = (rgb) => {
if (rgb.search("rgb") === -1) return rgb;
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
const hex = (x) => ("0" + parseInt(x).toString(16)).slice(-2);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
};
Run Code Online (Sandbox Code Playgroud)