Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RGB color value to RGBA at 0.75 alpha

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?

like image 426
Tom Avatar asked May 12 '14 12:05

Tom


People also ask

Can you convert RGB to rgba?

Convert your rgb files to rgba online & free Each color can have up to 255 gradations, allowing for color depths of up to 48 bits. RGB supports the display of 16,777,216 colors.

What is alpha value in rgba?

RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

What does rgba 255 0 0 0.2 color code in CSS means?

RGB Value. Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0. To display black, set all color parameters to 0, like this: rgb(0, 0, 0).


2 Answers

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');
}
like image 60
Cerbrus Avatar answered Oct 23 '22 08:10

Cerbrus


Another regex try http://jsfiddle.net/hc3BA/

var colour = 'rgb(123,123,123)',
new_col = colour.replace(/rgb/i, "rgba");
new_col = new_col.replace(/\)/i,',0.75)');
like image 39
nicolallias Avatar answered Oct 23 '22 07:10

nicolallias