Example #1
0
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()
Example #2
0
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)
Example #3
0
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
Example #4
0
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
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
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)
Example #13
0
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)
Example #14
0
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
Example #15
0
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
Example #16
0
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)