Exemple #1
0
def test_nearest_colour():
    """
    Find the nearest available colour
    """
    from painterm.colour import Colour, nearest_colour

    # empty palette
    assert_is_none(nearest_colour(Colour(0, 0, 0, 0), []))

    assert_equals(nearest_colour(Colour(64, 64, 64, 1.0), [Colour(0, 0, 0, 1.0), Colour(255, 255, 255, 1.0)]), 0)

    assert_equals(nearest_colour(Colour(64, 64, 64, 1.0), [Colour(255, 255, 255, 1.0), Colour(0, 0, 0, 1.0)]), 1)

    assert_equals(nearest_colour(Colour(64, 64, 64, 1.0), [Colour(64, 64, 0, 1.0), Colour(64, 0, 64, 1.0)]), 0)

    assert_equals(nearest_colour(Colour(64, 64, 64, 1.0), [Colour(64, 64, 0, 1.0), Colour(64, 64, 64, 1.0)]), 1)
Exemple #2
0
    def translate(self, colour):
        """
        Translate a colour into an ansi escape code.
        """
        if colour not in self._cache:
            ansi = nearest_colour(colour, self._palette)

            self._cache[colour] = ansi

        return self._cache[colour]