def test_alpha_blend_call_blending(mocker): cb = util.fill((2, 2), [0, 128, 255, 1]) cs = util.fill((2, 2), [255, 128, 0, 1]) return_value, _ = split_alpha(cs) normal_stub = mocker.Mock(return_value=return_value) alpha_blend(cb, cs, normal_stub) # TODO: assert with parameters normal_stub.assert_called_once()
def soft_light(im1, im2): """Darkens or lightens the colors, depending on the source color value. The soft light formula is defined as: if(Cs <= 0.5) B(Cb, Cs) = Cb - (1 - 2 x Cs) x Cb x (1 - Cb) else B(Cb, Cs) = Cb + (2 x Cs - 1) x (D(Cb) - Cb) where if(Cb <= 0.25) D(Cb) = ((16 * Cb - 12) x Cb + 4) x Cb else D(Cb) = sqrt(Cb) See the W3C document: https://www.w3.org/TR/compositing-1/#blendingsoftlight Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _soft_light)
def test_alpha_blend_normal_rgb_with_opaque_source(): cb = util.fill((2, 2), [0, 128, 255]) cs = util.fill((2, 2), [255, 128, 0, 1]) cr = alpha_blend(cb, cs, _normal) expected = split_alpha(cs)[0] assert cr == expected
def test_alpha_blend_normal_opaque_backdrop_with_transparent_source(): cb = util.fill((2, 2), [0, 128, 255, 1]) cs = util.fill((2, 2), [255, 128, 0, 0]) cr = alpha_blend(cb, cs, _normal) expected = split_alpha(cb)[0] assert cr == expected
def exclusion(im1, im2): """Produces an effect like Difference but lower in contrast. The exclusion formula is defined as: B(Cb, Cs) = Cb + Cs - 2 x Cb x Cs See the W3C document: https://www.w3.org/TR/compositing-1/#blendingexclusion Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _exclusion)
def normal(im1, im2): """The blending formula simply selects the source color. The normal formula is defined as: B(Cb, Cs) = Cs See the W3C document: https://www.w3.org/TR/compositing-1/#blendingnormal Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _normal)
def difference(im1, im2): """Subtracts the darker of the two constituent colors from the lighter color. The difference formula is defined as: B(Cb, Cs) = | Cb - Cs | See the W3C document: https://www.w3.org/TR/compositing-1/#blendingdifference Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _difference)
def darken(im1, im2): """Selects the darker of the backdrop and source colors. The darken formula is defined as: B(Cb, Cs) = min(Cb, Cs) See the W3C document: https://www.w3.org/TR/compositing-1/#blendingdarken Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _darken)
def color(im1, im2): """Creates a color with the hue and saturation of the source color and the luminosity of the backdrop color. The color formula is defined as: B(Cb, Cs) = SetLum(Cs, Lum(Cb)) See the W3C document: https://www.w3.org/TR/compositing-1/#blendingcolor Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _color)
def multiply(im1, im2): """The source color is multiplied by the destination color and replaces the destination. The mutiply formula is defined as: B(Cb, Cs) = Cb x Cs See the W3C document: https://www.w3.org/TR/compositing-1/#blendingmultiply Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _multiply)
def screen(im1, im2): """Multiplies the complements of the backdrop and source color values, then complements the result. The screen formula is defined as: B(Cb, Cs) = 1 - [(1 - Cb) x (1 - Cs)] = Cb + Cs - (Cb x Cs) See the W3C document: https://www.w3.org/TR/compositing-1/#blendingscreen Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _screen)
def hard_light(im1, im2): """Multiplies or screens the colors, depending on the source color value The hard light formula is defined as: if(Cs <= 0.5) B(Cb, Cs) = Multiply(Cb, 2 x Cs) else B(Cb, Cs) = Screen(Cb, 2 x Cs -1) See the W3C document: https://www.w3.org/TR/compositing-1/#blendinghardlight Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _hard_light)
def color_dodge(im1, im2): """Brightens the backdrop color to reflect the source color. The color dodge formula is defined as: if(Cb == 0) B(Cb, Cs) = 0 else if(Cs == 1) B(Cb, Cs) = 1 else B(Cb, Cs) = min(1, Cb / (1 - Cs)) See the W3C document: https://www.w3.org/TR/compositing-1/#blendingcolordodge Arguments: im1: A backdrop image (RGB or RGBA). im2: A source image (RGB or RGBA). Returns: The output image. """ return alpha_blend(im1, im2, _color_dodge)
def test_alpha_blend_normal_transparent_backdrop_with_rgb(): cb = util.fill((2, 2), [0, 128, 255, 0]) cs = util.fill((2, 2), [255, 128, 0]) cr = alpha_blend(cb, cs, _normal) assert cr == cs
def test_alpha_blend_normal_rgb_with_transparent_source(): cb = util.fill((2, 2), [0, 128, 255]) cs = util.fill((2, 2), [255, 128, 0, 0]) cr = alpha_blend(cb, cs, _normal) assert cr == cb
def test_alpha_blend_normal_rgb_with_rgb(): cb = util.fill((2, 2), [0, 128, 255]) cs = util.fill((2, 2), [255, 128, 0]) cr = alpha_blend(cb, cs, _normal) assert cr == _normal(cb, cs)