What is the math for painting one pixel with another (non-opaque) one?
If I have one color (colorOne) for which I know the RGB values, and another color (colorTwo) for which I know the RGBA values, how do I calculate the color I would get if I "painted" colorOne with colorTwo, as though colorTwo was a "layer" on top of colorOne, with A being the transparency of the layer? I tried Mathf.Lerp(colorOne.r, colorTwo.r, colorTwo.a), etc. but it's not giving me the results I expect--some colors are right and others are not.
Here is a mock-up of what I'm trying to do.
[1]: /storage/temp/74052-paint-example.png
Answer by Jessespike · Jul 14, 2016 at 09:06 PM
alpha-blend
//out = alpha * new + (1 - alpha) * old
result = c2.a * c2.rgb + (1 - c2.a) * c1.rgb;
Yahtzee! That last comment from @Jessespike contained what I needed. I want to mark this as the answer, but not the first/main answer with the additive blend. Sorry, I'm new--can someone tell me what to do here?
For a HelpRoom question (which is just personal help for you,) marking as an answer or upvoting doesn't have any purpose other than thanking the person who helped you. Just do whatever seems easiest to give some points to them.
Also, this is the same as your first Lerp attempt, but with color 1 and 2 switched.
Thanks for the info. Regarding Lerp, I think my unexpected results were due to dividing by 255 ins$$anonymous$$d of 255.0f. Thanks for pointing out the similarity in the math so I didn't mistrust poor Lerp in the future.
Answer by Jessespike · Jul 14, 2016 at 06:36 PM
Depends on what you mean by painted. I'm guessing you mean blend two colors additively.
public Color c1, c2, result;
void Update() {
result = c1 + c2;
}
Probably not a blend, since the OP makes a point that one color is under, and the other over. Sounds like black over white is different than white over black, else why mention under/over?
Thank you for your reply, Jessespike. I don't want add the two color values (which seems to make all resulting pixels brighter), but layer one on top of the other. I've added an image above to (hopefully) make what I'm looking for clearer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084817im_/https://answers.unity.com/themes/thub/images/avi.jpg)