What would be the easiest way to transform
什么是最简单的转型方式
$('#my_element').css('backgroundColor')
to object like this:
像这样的对象:
{ r: red_value, g: green_value, b: blue_value, a: alpha_value }
?
6 个解决方案
#1
19
var c = $('body').css('background-color');
var rgb = c.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',');
for(var i in rgb) {
console.log(rgb[i]);
}
Try it here http://jsbin.com/uhawa4
在这里试试http://jsbin.com/uhawa4
Edit :
var c = $('body').css('background-color');
var rgb = c.replace(/^rgba?\(|\s+|\)$/g,'').split(',');
for(var i in rgb) {
console.log(rgb[i]);
}
or even simpler way, just aiming at numbers
甚至更简单的方法,只针对数字
var c = 'rgba(60,4,2,6)';
var rgb = c.match(/\d+/g);
for(var i in rgb) {
console.log(rgb[i]);
}
#2
2
You would do something like:
你会做的事情如下:
$.fn.ToRgb = function()
{
if(this.charAt(0) == "#"){this = this.substring(1,7);} //remove the #
return {
R : parseInt(this.substring(0,2) ,16),
G : parseInt(this.substring(2,4) ,16),
B : parseInt(this.substring(4,6) ,16),
}
}
RGB = $('#my_element').css('backgroundColor').ToRgb();
/*
* console.log(rgb) =>
* {
* R: X
* G: X
* B: X
* }
*/
Pretty simple :)
很简单:)
#3
1
Say you have the following CSS rule:
假设您有以下CSS规则:
#my_element {
background-color: rgba(100, 0, 255, 0.5);
}
Then this is how you could get an RBGA object:
那么这就是你如何得到一个RBGA对象:
var colorStr = $('#my_element').css('backgroundColor'); // "rgba(100, 0, 255, 0.5)"
// using string methods
colorStr = colorStr.slice(colorStr.indexOf('(') + 1, colorStr.indexOf(')')); // "100, 0, 255, 0.5"
var colorArr = colorStr.split(','),
i = colorArr.length;
while (i--)
{
colorArr[i] = parseInt(colorArr[i], 10);
}
var colorObj = {
r: colorArr[0],
g: colorArr[1],
b: colorArr[2],
a: colorArr[3]
}
#4
0
More simple:
//javascript code
var color = $('#my_element').css('backgroundColor');
var rgb = /rgb\((\d+), (\d+), (\d+)\)/.exec(color);
var r = rgb[1],
g = rgb[2],
b = rgb[3];
console.log('Red :' + r);
console.log('Green:' + g);
console.log('Blue :' + b);
#5
0
To convert rgba string to object with keys:
要使用键将rgba字符串转换为对象:
convertRGBtoOBJ(colorString)
{
const rgbKeys = ['r', 'g', 'b', 'a'];
let rgbObj = {};
let color = colorString.replace(/^rgba?\(|\s+|\)$/g,'').split(',');
for (let i in rgbKeys)
rgbObj[rgbKeys[i]] = color[i] || 1;
return rgbObj;
}
console.log(convertRGBtoOBJ('rgba(23,54,230,0.5)'))
/*
Object {r: "23", g: "54", b: "230", a: 0.5}
*/
#6
0
As seen here:
如下所示:
R = hexToR("#FFFFFF");
G = hexToG("#FFFFFF");
B = hexToB("#FFFFFF");
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
This script basically takes each color pair from your hexcolor code (for example #F0556A) and switches it to a integer using parseInt with base 16 .
此脚本基本上从您的hexcolor代码中获取每个颜色对(例如#F0556A),并使用带有基数16的parseInt将其切换为整数。
#1
19
var c = $('body').css('background-color');
var rgb = c.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',');
for(var i in rgb) {
console.log(rgb[i]);
}
Try it here http://jsbin.com/uhawa4
在这里试试http://jsbin.com/uhawa4
Edit :
var c = $('body').css('background-color');
var rgb = c.replace(/^rgba?\(|\s+|\)$/g,'').split(',');
for(var i in rgb) {
console.log(rgb[i]);
}
or even simpler way, just aiming at numbers
甚至更简单的方法,只针对数字
var c = 'rgba(60,4,2,6)';
var rgb = c.match(/\d+/g);
for(var i in rgb) {
console.log(rgb[i]);
}
#2
2
You would do something like:
你会做的事情如下:
$.fn.ToRgb = function()
{
if(this.charAt(0) == "#"){this = this.substring(1,7);} //remove the #
return {
R : parseInt(this.substring(0,2) ,16),
G : parseInt(this.substring(2,4) ,16),
B : parseInt(this.substring(4,6) ,16),
}
}
RGB = $('#my_element').css('backgroundColor').ToRgb();
/*
* console.log(rgb) =>
* {
* R: X
* G: X
* B: X
* }
*/
Pretty simple :)
很简单:)
#3
1
Say you have the following CSS rule:
假设您有以下CSS规则:
#my_element {
background-color: rgba(100, 0, 255, 0.5);
}
Then this is how you could get an RBGA object:
那么这就是你如何得到一个RBGA对象:
var colorStr = $('#my_element').css('backgroundColor'); // "rgba(100, 0, 255, 0.5)"
// using string methods
colorStr = colorStr.slice(colorStr.indexOf('(') + 1, colorStr.indexOf(')')); // "100, 0, 255, 0.5"
var colorArr = colorStr.split(','),
i = colorArr.length;
while (i--)
{
colorArr[i] = parseInt(colorArr[i], 10);
}
var colorObj = {
r: colorArr[0],
g: colorArr[1],
b: colorArr[2],
a: colorArr[3]
}
#4
0
More simple:
//javascript code
var color = $('#my_element').css('backgroundColor');
var rgb = /rgb\((\d+), (\d+), (\d+)\)/.exec(color);
var r = rgb[1],
g = rgb[2],
b = rgb[3];
console.log('Red :' + r);
console.log('Green:' + g);
console.log('Blue :' + b);
#5
0
To convert rgba string to object with keys:
要使用键将rgba字符串转换为对象:
convertRGBtoOBJ(colorString)
{
const rgbKeys = ['r', 'g', 'b', 'a'];
let rgbObj = {};
let color = colorString.replace(/^rgba?\(|\s+|\)$/g,'').split(',');
for (let i in rgbKeys)
rgbObj[rgbKeys[i]] = color[i] || 1;
return rgbObj;
}
console.log(convertRGBtoOBJ('rgba(23,54,230,0.5)'))
/*
Object {r: "23", g: "54", b: "230", a: 0.5}
*/
#6
0
As seen here:
如下所示:
R = hexToR("#FFFFFF");
G = hexToG("#FFFFFF");
B = hexToB("#FFFFFF");
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
This script basically takes each color pair from your hexcolor code (for example #F0556A) and switches it to a integer using parseInt with base 16 .
此脚本基本上从您的hexcolor代码中获取每个颜色对(例如#F0556A),并使用带有基数16的parseInt将其切换为整数。