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?
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.
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).
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).
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');
}
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)');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With