<commit_before>import math

from .. import checker


class SearchDirection:
    def __init__(self, level, fixed_rgb):
        self.level = level
        self.target_ratio = checker.level_to_ratio(level)
        self.fixed_luminance = checker.relative_luminance(fixed_rgb)

    def round(self, ratio):
        return self._math_round(ratio * 10) / 10.0

    def has_sufficient_contrast(self, rgb):
        return self.contrast_ratio(rgb) >= self.target_ratio

    def contrast_ratio(self, rgb):
        luminance = checker.relative_luminance(rgb)
        return checker.luminance_to_contrast_ratio(self.fixed_luminance,
                                                   luminance)


class ToDarkerSide(SearchDirection):
    def __init__(self, level, fixed_rgb):
        super().__init__(level, fixed_rgb)
        self._math_round = math.floor

    def increment_condition(self, contrast_ratio):
        return contrast_ratio > self.target_ratio


class ToBrighterSide(SearchDirection):
    def __init__(self, level, fixed_rgb):
        super().__init__(level, fixed_rgb)
        self._math_round = math.ceil

    def increment_condition(self, contrast_ratio):
        return self.target_ratio > contrast_ratio


def threshold_criteria(level, fixed_rgb, other_rgb):
    if should_scan_darker_side(fixed_rgb, other_rgb):
        return ToDarkerSide(level, fixed_rgb)

    return ToBrighterSide(level, fixed_rgb)

<commit_msg>simplify the implementation of threshold_finder.criteria.SearchDirection and its subclasses<commit_after>import math

from .. import checker


class SearchDirection:
    def __init__(self, level, fixed_rgb, math_round):
        self.level = level
        self.target_ratio = checker.level_to_ratio(level)
        self.fixed_luminance = checker.relative_luminance(fixed_rgb)
        self._math_round = math_round

    def round(self, ratio):
        return self._math_round(ratio * 10) / 10.0

    def has_sufficient_contrast(self, rgb):
        return self.contrast_ratio(rgb) >= self.target_ratio

    def contrast_ratio(self, rgb):
        luminance = checker.relative_luminance(rgb)
        return checker.luminance_to_contrast_ratio(self.fixed_luminance,
                                                   luminance)


class ToDarkerSide(SearchDirection):
    def increment_condition(self, contrast_ratio):
        return contrast_ratio > self.target_ratio


class ToBrighterSide(SearchDirection):
    def increment_condition(self, contrast_ratio):
        return self.target_ratio > contrast_ratio


def threshold_criteria(level, fixed_rgb, other_rgb):
    if should_scan_darker_side(fixed_rgb, other_rgb):
        return ToDarkerSide(level, fixed_rgb, math.floor)

    return ToBrighterSide(level, fixed_rgb, math.ceil)


{'fraction_non_alphanumeric': 0.07818181818181819, 'fraction_numerical': 0.0036363636363636364, 'mean_word_length': 3.3322834645669293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2006242', 'n_tokens_mistral': 834, 'n_tokens_neox': 789, 'n_words': 162}