Beispiel #1
0
    def test_Color__ne__(self):
        self.assertTrue(Color(0, 0, 0, 0) != Color(255, 0, 0, 0))
        self.assertTrue(Color(0, 0, 0, 0) != Color(0, 255, 0, 0))
        self.assertTrue(Color(0, 0, 0, 0) != Color(0, 0, 255, 0))
        self.assertTrue(Color(0, 0, 0, 0) != Color(0, 0, 0, 255))

        self.assertFalse(Color(255, 0, 0, 0) != Color(255, 0, 0, 0))
        self.assertFalse(Color(0, 255, 0, 0) != Color(0, 255, 0, 0))
        self.assertFalse(Color(0, 0, 255, 0) != Color(0, 0, 255, 0))
        self.assertFalse(Color(0, 0, 0, 255) != Color(0, 0, 0, 255))

        self.assertTrue(tuple(Color(0, 0, 0, 0)) != (255, 0, 0, 0))
        self.assertTrue(tuple(Color(0, 0, 0, 0)) != (0, 255, 0, 0))
        self.assertTrue(tuple(Color(0, 0, 0, 0)) != (0, 0, 255, 0))
        self.assertTrue(tuple(Color(0, 0, 0, 0)) != (0, 0, 0, 255))

        self.assertFalse(tuple(Color(255, 0, 0, 0)) != (255, 0, 0, 0))
        self.assertFalse(tuple(Color(0, 255, 0, 0)) != (0, 255, 0, 0))
        self.assertFalse(tuple(Color(0, 0, 255, 0)) != (0, 0, 255, 0))
        self.assertFalse(tuple(Color(0, 0, 0, 255)) != (0, 0, 0, 255))

        self.assertTrue(int(Color(0, 0, 0, 0)) != 0xff000000)
        self.assertTrue(int(Color(0, 0, 0, 0)) != 0x00ff0000)
        self.assertTrue(int(Color(0, 0, 0, 0)) != 0x0000ff00)
        self.assertTrue(int(Color(0, 0, 0, 0)) != 0x000000ff)

        self.assertFalse(int(Color(255, 0, 0, 0)) != 0xff000000)
        self.assertFalse(int(Color(0, 255, 0, 0)) != 0x00ff0000)
        self.assertFalse(int(Color(0, 0, 255, 0)) != 0x0000ff00)
        self.assertFalse(int(Color(0, 0, 0, 255)) != 0x000000ff)
Beispiel #2
0
def color_combos():
    for x in all_combos:
        yield Color(*x)
    def test_Color_normalize(self):
        c = Color(204, 38, 194, 55)
        self.assertEqual(c.r, 204)
        self.assertEqual(c.g, 38)
        self.assertEqual(c.b, 194)
        self.assertEqual(c.a, 55)

        t = c.normalize()

        self.assertAlmostEqual(t[0], 0.800000, places=5)
        self.assertAlmostEqual(t[1], 0.149016, places=5)
        self.assertAlmostEqual(t[2], 0.760784, places=5)
        self.assertAlmostEqual(t[3], 0.215686, places=5)

        c = Color(255, 255, 255, 255)
        self.assertEqual(c.normalize(), (1.0, 1.0, 1.0, 1.0))
        c = Color(0, 0, 0, 0)
        self.assertEqual(c.normalize(), (0.0, 0.0, 0.0, 0.0))
        c = Color(128, 128, 128, 128)
        t = c.normalize()
        for v in t:
            self.assertAlmostEqual(v, 0.5, places=2)

        c = Color(128, 255, 0, 52)
        t = c.normalize()
        self.assertAlmostEqual(t[0], 0.5, places=2)
        self.assertEqual(t[1], 1.0)
        self.assertEqual(t[2], 0.0)
        # 52 / 255 ~= .20
        self.assertAlmostEqual(t[3], 0.2, places=2)
    def test_string_to_color(self):
        assertEqual = self.assertEqual
        assertRaises = self.assertRaises
        for method in (color.string_to_color, color.convert_to_color,
                       color.COLOR):
            assertEqual(method('#00000000').r, 0x00)
            assertEqual(method('#10000000').r, 0x10)
            assertEqual(method('#20000000').r, 0x20)
            assertEqual(method('#30000000').r, 0x30)
            assertEqual(method('#40000000').r, 0x40)
            assertEqual(method('#50000000').r, 0x50)
            assertEqual(method('#60000000').r, 0x60)
            assertEqual(method('#70000000').r, 0x70)
            assertEqual(method('#80000000').r, 0x80)
            assertEqual(method('#90000000').r, 0x90)
            assertEqual(method('#A0000000').r, 0xA0)
            assertEqual(method('#B0000000').r, 0xB0)
            assertEqual(method('#C0000000').r, 0xC0)
            assertEqual(method('#D0000000').r, 0xD0)
            assertEqual(method('#E0000000').r, 0xE0)
            assertEqual(method('#F0000000').r, 0xF0)
            assertEqual(method('#01000000').r, 0x01)
            assertEqual(method('#02000000').r, 0x02)
            assertEqual(method('#03000000').r, 0x03)
            assertEqual(method('#04000000').r, 0x04)
            assertEqual(method('#05000000').r, 0x05)
            assertEqual(method('#06000000').r, 0x06)
            assertEqual(method('#07000000').r, 0x07)
            assertEqual(method('#08000000').r, 0x08)
            assertEqual(method('#09000000').r, 0x09)
            assertEqual(method('#0A000000').r, 0x0A)
            assertEqual(method('#0B000000').r, 0x0B)
            assertEqual(method('#0C000000').r, 0x0C)
            assertEqual(method('#0D000000').r, 0x0D)
            assertEqual(method('#0E000000').r, 0x0E)
            assertEqual(method('#0F000000').r, 0x0F)

            assertRaises(ValueError, method, "0x12345")
            assertRaises(ValueError, method, "0x1234567")
            assertRaises(ValueError, method, "#123456789")
            assertRaises(ValueError, method, "#12345")
            assertRaises(ValueError, method, "#1234567")
            assertRaises(ValueError, method, "#123456789")

            assertRaises(ValueError, method, "# f000000")
            assertRaises(ValueError, method, "#f 000000")
            assertRaises(ValueError, method, "#-f000000")
            assertRaises(ValueError, method, "-#f000000")

            assertRaises(ValueError, method, "0x f000000")
            assertRaises(ValueError, method, "0xf 000000")
            assertRaises(ValueError, method, "0x-f000000")
            assertRaises(ValueError, method, "-0xf000000")

            assertRaises(ValueError, method, "#cc00qq")
            assertRaises(ValueError, method, "0xcc00qq")
            assertRaises(ValueError, method, "09abcdef")
            assertRaises(ValueError, method, "09abcde")
            assertRaises(ValueError, method, "quarky")

            cvals = list(color_combos())
            for index, val in enumerate(hex_combos()):
                assertEqual(method(val), cvals[index], "Failed for '%s'" % val)
            for index, val in enumerate(hash_combos()):
                assertEqual(method(val), cvals[index], "Failed for '%s'" % val)

        self.assertRaises(TypeError, color.string_to_color, 0xff000000)
        self.assertRaises(TypeError, color.string_to_color, Color())
 def test_Color__len__(self):
     c = Color(204, 38, 194, 55)
     self.assertEqual(len(c), 4)
     self.assertEqual(len(Color()), 4)
     self.assertEqual(len(Color(2)), 4)
 def test_Color__getitem__(self):
     c = Color(204, 38, 194, 55)
     self.assertEqual(c[0], 204)
     self.assertEqual(c[1], 38)
     self.assertEqual(c[2], 194)
     self.assertEqual(c[3], 55)
 def test_Color__repr__(self):
     c = Color(68, 38, 26, 69)
     c1 = eval(repr(c))
     self.assertEqual(c, c1)
    def test_Color__invert__(self):
        self.assertEqual(~Color(), Color(0, 0, 0, 0))
        self.assertEqual(~Color(0, 0, 0, 0), Color(255, 255, 255, 255))
        self.assertEqual(~Color(255, 0, 0, 0), Color(0, 255, 255, 255))
        self.assertEqual(~Color(0, 255, 0, 0), Color(255, 0, 255, 255))
        self.assertEqual(~Color(0, 0, 255, 0), Color(255, 255, 0, 255))
        self.assertEqual(~Color(0, 0, 0, 255), Color(255, 255, 255, 0))
        self.assertEqual(~Color(127, 127, 127, 0), Color(128, 128, 128, 255))

        self.assertEqual(~Color(1, 2, 3, 4), Color(254, 253, 252, 251))
        self.assertEqual(~Color(127, 127, 127, 0), Color(128, 128, 128, 255))
    def test_string_to_color(self):
        for method in (color.string_to_color, color.convert_to_color,
                       color.COLOR):
            assert method('#00000000').r == 0x00
            assert method('#10000000').r == 0x10
            assert method('#20000000').r == 0x20
            assert method('#30000000').r == 0x30
            assert method('#40000000').r == 0x40
            assert method('#50000000').r == 0x50
            assert method('#60000000').r == 0x60
            assert method('#70000000').r == 0x70
            assert method('#80000000').r == 0x80
            assert method('#90000000').r == 0x90
            assert method('#A0000000').r == 0xA0
            assert method('#B0000000').r == 0xB0
            assert method('#C0000000').r == 0xC0
            assert method('#D0000000').r == 0xD0
            assert method('#E0000000').r == 0xE0
            assert method('#F0000000').r == 0xF0
            assert method('#01000000').r == 0x01
            assert method('#02000000').r == 0x02
            assert method('#03000000').r == 0x03
            assert method('#04000000').r == 0x04
            assert method('#05000000').r == 0x05
            assert method('#06000000').r == 0x06
            assert method('#07000000').r == 0x07
            assert method('#08000000').r == 0x08
            assert method('#09000000').r == 0x09
            assert method('#0A000000').r == 0x0A
            assert method('#0B000000').r == 0x0B
            assert method('#0C000000').r == 0x0C
            assert method('#0D000000').r == 0x0D
            assert method('#0E000000').r == 0x0E
            assert method('#0F000000').r == 0x0F

            with pytest.raises(ValueError):
                method("0x12345")
            with pytest.raises(ValueError):
                method("0x1234567")
            with pytest.raises(ValueError):
                method("#123456789")
            with pytest.raises(ValueError):
                method("#12345")
            with pytest.raises(ValueError):
                method("#1234567")
            with pytest.raises(ValueError):
                method("#123456789")

            with pytest.raises(ValueError):
                method("# f000000")
            with pytest.raises(ValueError):
                method("#f 000000")
            with pytest.raises(ValueError):
                method("#-f000000")
            with pytest.raises(ValueError):
                method("-#f000000")

            with pytest.raises(ValueError):
                method("0x f000000")
            with pytest.raises(ValueError):
                method("0xf 000000")
            with pytest.raises(ValueError):
                method("0x-f000000")
            with pytest.raises(ValueError):
                method("-0xf000000")

            with pytest.raises(ValueError):
                method("#cc00qq")
            with pytest.raises(ValueError):
                method("0xcc00qq")
            with pytest.raises(ValueError):
                method("09abcdef")
            with pytest.raises(ValueError):
                method("09abcde")
            with pytest.raises(ValueError):
                method("quarky")

            cvals = list(color_combos())
            for index, val in enumerate(hex_combos()):
                assert method(val) == cvals[index], "Failed for '%s'" % val
            for index, val in enumerate(hash_combos()):
                assert method(val) == cvals[index], "Failed for '%s'" % val

        with pytest.raises(TypeError):
            color.string_to_color(0xff000000)
        with pytest.raises(TypeError):
            color.string_to_color(Color())
    def test_Color__eq__(self):
        assert Color(255, 0, 0, 0) == Color(255, 0, 0, 0)
        assert Color(0, 255, 0, 0) == Color(0, 255, 0, 0)
        assert Color(0, 0, 255, 0) == Color(0, 0, 255, 0)
        assert Color(0, 0, 0, 255) == Color(0, 0, 0, 255)

        assert not (Color(0, 0, 0, 0) == Color(255, 0, 0, 0))
        assert not (Color(0, 0, 0, 0) == Color(0, 255, 0, 0))
        assert not (Color(0, 0, 0, 0) == Color(0, 0, 255, 0))
        assert not (Color(0, 0, 0, 0) == Color(0, 0, 0, 255))

        assert tuple(Color(255, 0, 0, 0)) == (255, 0, 0, 0)
        assert tuple(Color(0, 255, 0, 0)) == (0, 255, 0, 0)
        assert tuple(Color(0, 0, 255, 0)) == (0, 0, 255, 0)
        assert tuple(Color(0, 0, 0, 255)) == (0, 0, 0, 255)

        assert not (tuple(Color(0, 0, 0, 0)) == (255, 0, 0, 0))
        assert not (tuple(Color(0, 0, 0, 0)) == (0, 255, 0, 0))
        assert not (tuple(Color(0, 0, 0, 0)) == (0, 0, 255, 0))
        assert not (tuple(Color(0, 0, 0, 0)) == (0, 0, 0, 255))

        assert int(Color(255, 0, 0, 0)) == 0xff000000
        assert int(Color(0, 255, 0, 0)) == 0x00ff0000
        assert int(Color(0, 0, 255, 0)) == 0x0000ff00
        assert int(Color(0, 0, 0, 255)) == 0x000000ff

        assert not (int(Color(0, 0, 0, 0)) == 0xff000000)
        assert not (int(Color(0, 0, 0, 0)) == 0x00ff0000)
        assert not (int(Color(0, 0, 0, 0)) == 0x0000ff00)
        assert not (int(Color(0, 0, 0, 0)) == 0x000000ff)
    def test_Color_normalize(self):
        c = Color(204, 38, 194, 55)
        assert c.r == 204
        assert c.g == 38
        assert c.b == 194
        assert c.a == 55

        t = c.normalize()

        assert round(abs(t[0] - 0.800000), 5) == 0
        assert round(abs(t[1] - 0.149016), 5) == 0
        assert round(abs(t[2] - 0.760784), 5) == 0
        assert round(abs(t[3] - 0.215686), 5) == 0

        c = Color(255, 255, 255, 255)
        assert c.normalize() == (1.0, 1.0, 1.0, 1.0)
        c = Color(0, 0, 0, 0)
        assert c.normalize() == (0.0, 0.0, 0.0, 0.0)
        c = Color(128, 128, 128, 128)
        t = c.normalize()
        for v in t:
            assert round(abs(v - 0.5), 2) == 0

        c = Color(128, 255, 0, 52)
        t = c.normalize()
        assert round(abs(t[0] - 0.5), 2) == 0
        assert t[1] == 1.0
        assert t[2] == 0.0
        # 52 / 255 ~= .20
        assert round(abs(t[3] - 0.2), 2) == 0
 def test_Color__getitem__(self):
     c = Color(204, 38, 194, 55)
     assert c[0] == 204
     assert c[1] == 38
     assert c[2] == 194
     assert c[3] == 55
 def test_Color__len__(self):
     c = Color(204, 38, 194, 55)
     assert len(c) == 4
     assert len(Color()) == 4
     assert len(Color(2)) == 4
    def test_Color__invert__(self):
        assert ~Color() == Color(0, 0, 0, 0)
        assert ~Color(0, 0, 0, 0) == Color(255, 255, 255, 255)
        assert ~Color(255, 0, 0, 0) == Color(0, 255, 255, 255)
        assert ~Color(0, 255, 0, 0) == Color(255, 0, 255, 255)
        assert ~Color(0, 0, 255, 0) == Color(255, 255, 0, 255)
        assert ~Color(0, 0, 0, 255) == Color(255, 255, 255, 0)
        assert ~Color(127, 127, 127, 0) == Color(128, 128, 128, 255)

        assert ~Color(1, 2, 3, 4) == Color(254, 253, 252, 251)
        assert ~Color(127, 127, 127, 0) == Color(128, 128, 128, 255)
 def test_Color__repr__(self):
     c = Color(68, 38, 26, 69)
     c1 = eval(repr(c))
     assert c == c1