def rescale(x, new_min, new_max, old_min, old_max):
    """
    Rescales a value from an old range [A,B] to a new range [C,D] using the equation:

    x' = C(1 - (x-A)/(B-A)) + D((x-A)/(B-A))

    :param x: value to be scaled
    :param new_min: new range minimum (C)
    :param new_max: new range maximum (D)
    :param old_min: old range minimum (A)
    :param old_max: old range maximum (B)
    :return: rescaled value
    """

    return new_min * (1 - (x-old_min)/(old_max-old_min)) \
           + new_max*((x-old_min)/(old_max-old_min))