blend modes formulas ? multiply, add, substract

Started by RemiD, February 06, 2021, 09:39:31

Previous topic - Next topic

RemiD

hi ! :)

i am playing with the blending of pixels of different layers, and i want to know if these formulas are correct ( the basic idea, i don't need the perfect formula...)

add = color of pixel of top layer is added to color of pixel of below layer

substract = color of pixel of top layer is subtracted to color of pixel of below layer

multiply = color of pixel of top layer is added to color of pixel of below layer, then result color is divided by 2

yes ? no ? what do you think ?

thanks,

TomToad

Close.  Multiply is the two colors multiplied together.  The values are between 0 and 1, so if you are using colors from 0 to 255, then they will be scaled color/255.  So source = 255, dest = 128, result is (255/255) * (128/255) = 1.0*0.5 =  0.5 * 255 = 128.  Source = 64, dest = 128, result is (64/255)*(128/255) = 0.25*0.5=0.125*255 = 32
------------------------------------------------
8 rabbits equals 1 rabbyte.


RemiD

#3
@TomToad>>thanks for the explanations !


so the blend mode that i have explained :
color of pixel of top layer is added to color of pixel of below layer, then result color is divided by 2

exists ? has a name ?


@Kryzon>>thanks, very interesting !

Kryzon

Quote from: RemiD on February 06, 2021, 12:41:46
so the blend mode that i have explained :
color of pixel of top layer is added to color of pixel of below layer, then result color is divided by 2

exists ? has a name ?
That is mathematically the same as 'alpha blending' with a factor of 0.5.
It's an averaging of the two colors.

Alpha blending can also be called the more generic name "linear interpolation" , or LERP for short.

Kryzon

So if you're drawing an image with alpha blending and the alpha of a pixel from the image is 0.5, the color at that pixel will be mixed equally with the background color, giving a perfect average of the two.

The formula for alpha blending can be: 
S + (D - S) * F

With S for the source color (the top color, from the sprite), D for the destination color (the bottom color, or background) and F is the blending factor. 
When F is 0.5 (the same as 1/2), you have this: 
S + (D - S) * 0.5 =
= 1 S + 0.5 D - 0.5 S =
= 0.5 S + 0.5 D =
= (S + D) * 0.5 =
= (S + D) / 2