Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate a color with opacity based on it's values over other colors?

For example, I know the color is #a0c5e8 (160,197,232) when over white, and it's #496e91 (73,110,145) when over black.

Is there a way to calculate the "real" color (at 100% opacity)?

like image 984
Sarke Avatar asked Oct 13 '25 08:10

Sarke


1 Answers

Yes you can, by solving a system of linear equations. Let's look at the red channel as an example:

Variables:
c - color (unknown)
f - opacity (unknown)

Equations:
c * f + (1 − f) * 255 = 160. (blending with white)
c * f + (1 − f) * 0 = 73. (blending with black)

Rearrange to get:
c * f − 255 f = −95.
c * f = 73.
255 f = 168.

Therefore:
f = 168/255 ≈ 65.9%.
c = 6205/56 ≈ 110.8.

Computing the other channels, your final color is (111,167,220) and opacity is 66%.

like image 156
Nayuki Avatar answered Oct 15 '25 02:10

Nayuki