def align_rect(r, frame, align='tl', margin=0):
    """
    Modifies the rectangle rect so that it is aligned with the rectangle frame according to align
    and margin. See `blit_in_rect`.

    Args:
        r:
        frame:
        align:
        margin:
    """
    if 'l' in align:
        r.left = frame.left + margin
    elif 'r' in align:
        r.right = frame.right - margin
    else:
        r.centerx = frame.centerx
    if 't' in align:
        r.top = frame.top + margin
    elif 'b' in align:
        r.bottom = frame.bottom - margin
    else:
        r.centery = frame.centery
