Reputation: 1572
I will try to explain my question with an example:
Given: rgb(100,0,0) + x = rgb(100,0,100)
Find: x = rgba(0,0,100,0.5)
My actual case:
Background:rgb(147,150,72), Element rgb(44,100,62)
This is partially solved in this question for the sole case of white backgrounds, I am actually looking for a more general solution. I have tried to adapt the javascript code but I don't really understand how it is done or if a general solution is even possible. I might be able to code it myself is someone understand how it works and tells me. (if solved I will post solution here of course)
Links:
Upvotes: 1
Views: 1626
Reputation: 189
A general exact solution is not existent.
As stated in the answer you linked to
C = X * a + Y * (a-1)
is the formula to calculate the color channel C
out of the colors X
and Y
, while a
is the alpha value of X
.
So, when you try to find out the color + alpha channel, you have to solve this equation 3 times (once for each color). Each of those equation does have 2 unknowns.
So without requiring additional conditions (e.g. the one mentioned in your second link) to apply, there is an infinite amount of different solutions (or in the specific case of one byte per color channel, up to 256).
While determining one exact solution is impossible, you can, of course, determine one or more possible solutions, with the following way:
X = C/a - Y + Y/a // Formula from above solved for X
So the possible solutions are
r3 = r2/a - r1 + r1/a
g3 = g2/a - g1 + g1/a
b3 = b2/a - b1 + b1/a
with
rgb(r1, g1, b1)
being the background color, rgb(r2, g2, b2)
being the mixed color rgba(r3, g3, b3, a)
being the color that was painted on the background. If you now select a
so, that none of r3
, g3
and b3
are below 0 or above 255, you get a valid solution.
Upvotes: 2