def triangle_wave(pos, peak=0.5):
    """Return a brightness level (0.0 to 1.0) corresponding to a position
       (0.0 to 1.0) within a triangle wave (spanning 0.0 to 1.0) with wave's
       peak brightness at a given position (0.0 to 1.0) within its span.
       Positions outside the wave's span return 0.0."""
    if 0.0 <= pos < 1.0:
        if pos <= peak:
            return pos / peak
        return (1.0 - pos) / (1.0 - peak)
    return 0.0