Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  115] [ 6]  / answers: 1 / hits: 24344  / 10 Years ago, mon, may 12, 2014, 12:00:00

I have the following code to get the background color of an element.



var currentColor = $(this).css('background-color');


which returns something like rgb(123,123,123)



What I now want to do convert this to rgba and show it at 0.75 alpha



So returning something like rgba(123,123,123,0.75)



Any ideas?


More From » jquery

 Answers
3

Since jQuery always seems to return the color like rgb(r, g, b) for elements that have no alpha, you could simply use:



$(this).css('background-color').replace(')', ', 0.75)').replace('rgb', 'rgba');


Just make sure the background color isn't rgba already:



var bg = $(this).css('background-color');
if(bg.indexOf('a') == -1){
var result = bg.replace(')', ', 0.75)').replace('rgb', 'rgba');
}

[#71063] Saturday, May 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;