Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript how can I set rgba without specifying the rgb?

I have an HTML element whose background colour is set with rgba()

<div style="background-color: rgba(2,100,100,0);"> </div> 

Then I have a timer that makes the background slowly fade in by changing the opacity value of the element in javascript

myEle.style.backgroundColor = "rgba(x,x,x,0.1)"; // I have to know the rgb values to update the alpha value 

Is there a way to set the a value of rgba() without changing/knowing the rgb values?

Maybe I can do something like this?

var r = myEle.style.r; var g = myEle.style.g; var b = myEle.style.b; myEle.style.backgroundColor = "rgba("+r+","+g+","+b+",0.1)"; 
like image 696
sazr Avatar asked Nov 18 '11 04:11

sazr


People also ask

How do I make rgba transparent?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.

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

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).

What is the difference between rgba and RGB?

An RGB color value represents RED, GREEN, and BLUE light sources. An RGBA color value is an extension of RGB with an Alpha channel (opacity).


2 Answers

You got the string, replace whatever
'rgba(1,1,1,0.3)'.replace(/[^,]+(?=\))/, '0.5')

like image 92
Leonid Avatar answered Sep 18 '22 16:09

Leonid


After some playing around, and the discovery of getComputedStyle, I have put together this.

<!DOCTYPE HTML> <html>   <head>     <style type="text/css">       #element {         background-color: rgb(10,10,10);         background-color: rgba(10,10,10,1);       }     </style>     <script type="text/javascript">             HTMLElement.prototype.alpha = function(a) {         current_color = getComputedStyle(this).getPropertyValue("background-color");         match = /rgba?\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*\d+[\.\d+]*)*\)/g.exec(current_color)         a = a > 1 ? (a / 100) : a;         this.style.backgroundColor = "rgba(" + [match[1],match[2],match[3],a].join(',') +")";       }     </script>   </head>   <body>     <div id="element">       This is some content.     </div>     <script type="text/javascript">       e = document.getElementById('element');       e.alpha(20);     </script>   </body> </html> 
  • Make sure you define in your css your values, and cascade because RGBA is CSS3.
  • Also see that you can pass in a number >1 for alpha and it will divide by 100 for you (I hate working with decimals when thinking percentages).
  • Enjoy!
like image 20
Rob Cooper Avatar answered Sep 17 '22 16:09

Rob Cooper