The concept of transparency and the overlay effect: uncovering the formula

I am working on developing a unique system that involves utilizing various div elements and adjusting their opacity levels.

Here are some specific examples I am dealing with: envision all the divs being black. - When two divs overlap, each with an opacity of X, the resulting overlap area will have an overall opacity of 1.

  • Imagine having 10 divs overlapping. These divs have varying opacities - x1, x2, ..., x10. The overlap area where all 10 divs intersect will display an opacity of 1. The overlap areas involving fewer than 10 divs will appear more transparent in comparison.

I am seeking guidance on how to develop an algorithm to determine the optimal "X" opacity parameter for each individual div.

Thank you for your assistance.

SOLVED!

The final opacity formula is calculated as follows: final opacity = 1-(1-op1)(1-op2) or op1+op2-op1*op2.

Another important equation guiding this process is: log 0.01 = x*log(op), where the opacity of the single level (op) plays a crucial role.

This solution was graciously shared by my brilliant friend.

Answer №1

Solution:

The formula for calculating final opacity is 1-(1-opacity1)(1-opacity2) or opacity1+opacity2-opacity1*opacity2

Given that log 0.01 = x*log(opacity), where opacity is the opacity of the single level.

Answer №2

Having trouble grasping the question completely, but my suggestion would be to create a CodePen or jsFiddle for testing purposes.

You could start with something like this as a base for prototyping:

CSS:

#wrapper {position:relative;}
#black-box {background-color:black}

.overlay {
    position:absolute;top:1em;left:0;
    background-color:black; opacity:0.2;
}

HTML:

<div id="wrapper">
<div id="black-box">The background is solid black</div>
<div class="overlay">This is transparent black</div>
<div class="overlay">This is transparent black</div>
<div class="overlay">This is transparent black</div>
<div class="overlay">This is transparent black</div>
</div>​

Experiment with various numbers of transparent divs and their opacities. Create new classes (e.g. "overlay2") with different opacities, etc...

I doubt there's a universal algorithm to handle this that works flawlessly on all browsers, but I'm curious about the outcome (and hoping to be proven wrong about predictability), so please share your findings.

Answer №3

Stumbled upon your intriguing problem. While I'm not a math whiz or physics expert, here's my take.

Could it be that when two divs overlap, the resulting opacity in the overlapping area is the sum of the opacities of the two divs? For instance, if

D1's opacity = 0.1 
and 
D2's opacity = 0.2 
their combined overlap opacity = 0.3. (possible scenario?)

If four additional divs overlap with them and their total opacity exceeds 1, does that mean the overlapping area will turn completely black?

Alternatively, have you considered posting your query on programmer.stackexchange.com as well? The community there thrives on algorithms :)

Maybe my theory doesn't hold up. I've set up a simple jsFiddle which someone here might have already done. Nonetheless, here's the link for you to explore: http://jsfiddle.net/gHjrN/

I've created four divs with varying opacities on the fiddle. Despite their opacities adding up to over 1.0, their overlapping area isn't completely black yet. By the way, why the quest for an algorithm when browsers seem to handle it automatically? :D

Oh, I came across a helpful link that could shed some light on your question: opacity of overlapping partially transparent elements. The calculated opacity may be 1-(D1's opacity X D2's opacity)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way for me to align my div with my header on the same line?

HTML: <h2> RAN shares false news story about Hershey's Oil Commitment</h2> <div class="date"> <div class="number">27</div> <div class="month">Oct</div> </div> <p>The Rainforest Action Netwo ...

What is the best way to create a form with two inputs that redirects me to a different page based on the inputs chosen?

Seeking assistance to resolve a challenging problem that I am currently facing. Any suggestions on the technology or backend skills needed for this task would be greatly appreciated, as well as any advice that can help me progress in the right direction. ...

Reduce the number of divs on a webpage while incorporating animated transitions

I've been attempting to incorporate an animation on the width property for my div .panels. I've tried using transition-property in CSS and also with .animate() in jQuery, but unfortunately, it doesn't seem to be working. I also noticed that ...

What are some ways to improve the precision of longitude and latitude data?

I have encountered an issue while using MapBox. When a user clicks on the map, I am receiving longitude and latitude coordinates that are not very accurate. For example, when I clicked in the United Kingdom, the values pointed me to the middle of the sea a ...

Modify the class's height by utilizing props

Using Vue 3 and Bootstrap 5, I have a props named number which should receive integers from 1 to 10. My goal is to incorporate the number in my CSS scrollbar class, but so far it's not working as expected. There are no error messages, it just doesn&a ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

The JavaScript code created in CodePen doesn't seem to function properly when integrated into my website

During a learning program, I created a basic random quote generator. The code worked flawlessly on codepen.io. Using a simple JavaScript (jQuery) function, I displayed random quotes along with their authors, which were stored in an array. I ensured that t ...

Incorporating mousewheel functionality by utilizing DOMmouseScroll and mousewheel to trigger functions

Looking to create a function that triggers other functions based on the direction of mousewheel movement, but unsure how to proceed. Initially coded for mousewheel event, however, Firefox does not support this event. Need to find a way to incorporate DOMm ...

Incorporate JavaScript values into an HTML form to submit them to PHP

How can I successfully POST the values of 'vkey' and 'gene+varient' when submitting a form in HTML? The values are retrieved using JS code and displayed correctly on the form, but are not being sent upon submission. <form action="ac ...

Delete the HTML line break

I need some help figuring out how to eliminate the line break in HTML. Essentially, I want to display two "Hello World" lines next to each other instead of stacked on top of each other. <p>Hello World</p> <p>Hello World</p> ...

Adding color to characters, digits in an HTML file

Is it possible to individually style each letter, number, and symbol in an HTML document with a unique color? I am interested in creating a text editor that applies specific colors to each glyph for those who have grapheme-color synesthesia. While there ar ...

When the "change" event listener is added to an input element, the innerHTML is updated as the value increases but not when it decreases

My div block contains a value that I need to adjust as the input value changes. While the adjustment works when the input value increases, it does not change when the input value is decreased. It seems like there may be an oversight in my logic that I am m ...

Proper handling of retrieving POST method data on the current page

I'm uncertain if I'm handling my "same page" forms effectively. My current method involves adding a hidden input named "action" with value='1' to the form. Then, at the start of the page, I check if $_POST["action"] has a value, and ...

Initiating the ngOnInit lifecycle hook in child components within Angular

I am facing an issue with controlling the behavior of child components in my Angular application. The problem arises when switching between different labels, causing the ngOnInit lifecycle hook of the children components not to trigger. The main component ...

What are the steps for populating a table cell with data using AJAX, MySQL, and PHP in the given situation?

The challenge is to create an HTML table with two columns where the first column gets its data from PHP and MySQL. The goal is to turn the values in the first column into clickable links that, when clicked, trigger an AJAX call to fetch information from th ...

Develop a C# application that scans for specific HTML links and saves the extracted results into an Excel spreadsheet

One day, the professor approached me with a project idea for a website that required a significant amount of data. He requested that I copy and paste links' content into Excel, which seemed like a daunting task. As a result, I decided to create a pro ...

I cannot seem to receive the confirmation of success!

Trying to store user details in a database and display a success message that fades in. I have attempted some code, but unfortunately it's not working. Please help me out. Apologies if I am mistaken. Here is my register.php code: <?php require ...

Error message: When the mouse hovers over, display the chart(.js) results in TypeError: t is

I encountered a persistent error that I just can't seem to resolve. My goal is to showcase a chart using Chart.js when the mouse hovers over the canvas. However, upon hovering over the canvas, I keep getting a TypeError: t is null error. Error: { ...

Upon calling the createModalAddPost() function, a single window is triggered to open

Hey there, I'm having a JavaScript question. So, I have a panel that opens a window, and it works fine. But the issue arises when I close the window and try to open it again - it doesn't work. I have to reload the page every time in order to open ...

Enter the value into the AngularJS textbox

I am currently working on the Edit Profile page using AngularJS Typically, a list of details will be displayed. When one item in the list is clicked, a specific method is called. $scope.editContact = function(user){ $('#editContactSecti ...