def apply_mask_to_layer(dest, source, alpha):
    """
    :param dest: a set of RGB triples (50, 100, 150), the lower bitmap layer that shows through the transparency
    :param source: a set of RGB triples (123, 100, 50), to have the transparency-mask applied to
    :param alpha: a single hex value (FF) that represents the transparency:
     - 0 is fully transparent, the dest layer shows through completely
     - FF is fully opaque, the source layer is all that's visible
     - other values are computed
    :return: a set of RGB triples (50, 60, 70)

    https://en.wikipedia.org/wiki/Alpha_compositing
    out_rgb = src_rgb * alpha + dst_rgb (1 - alpha)
    """

    # Handle the easy cases first:
    if alpha == 0:
        return dest

    if alpha == 255:
        return source

    alpha_pct = alpha / 255.0
    new_rgb = (int(source[0] * alpha_pct + dest[0] * (1 - alpha_pct)),
               int(source[1] * alpha_pct + dest[1] * (1 - alpha_pct)),
               int(source[2] * alpha_pct + dest[2] * (1 - alpha_pct)))

    return new_rgb