'''Collection of modules that implement the main logic of instance
methods of Color, ``.find_*_threshold``.
'''

def binary_search_width(init_width, min_width):
    i = 1
    init_width = float(init_width)
    d = init_width / pow(2, i)

    while d > min_width:
        yield d
        i += 1
        d = init_width / pow(2, i)


def find_ratio(other_rgb, criteria, rgb_with_ratio, init_ratio, init_width):
    target_ratio = criteria.target_ratio
    r = init_ratio
    sufficient_r = None

    for d in binary_search_width(init_width, 0.01):
        contrast_ratio = criteria.contrast_ratio(rgb_with_ratio(other_rgb, r))

        if contrast_ratio >= target_ratio:
            sufficient_r = r

        if contrast_ratio == target_ratio:
            break

        r += d if criteria.increment_condition(contrast_ratio) else -d

    return (r, sufficient_r)


def rgb_with_better_ratio(other_rgb, criteria, r, sufficient_r, calc_rgb):
    nearest = calc_rgb(other_rgb, r)
    satisfying_nearest = criteria.has_sufficient_contrast(nearest)

    if sufficient_r and not satisfying_nearest:
        return calc_rgb(other_rgb, sufficient_r)

    return nearest

@@ -546,24 +546,19 @@
         
-contrast
+new
 _ratio =
@@ -620,32 +620,27 @@
 %0A        if 
-contrast
+new
 _ratio %3E= ta
@@ -692,24 +692,19 @@
      if 
-contrast
+new
 _ratio =
@@ -785,24 +785,19 @@
 ndition(
-contrast
+new
 _ratio) 
@@ -858,33 +858,29 @@
 etter_ratio(
-other_rgb
+color
 , criteria, 
@@ -896,24 +896,30 @@
 ient_r, 
-calc_rgb
+rgb_with_ratio
 ):%0A    n
@@ -927,34 +927,36 @@
 arest = 
-calc_rgb(other_rgb
+rgb_with_ratio(color
 , r)%0A   
@@ -1087,26 +1087,28 @@
 urn 
-calc_rgb(other_rgb
+rgb_with_ratio(color
 , su

rename local variables for consistency