Exemplo n.º 1
0
    def read_from_project(self, resource_open):
        with resource_open("sprite_group_palettes", "yml") as f:
            self.palette_table.from_yml_file(f)

        with resource_open("sprite_groups", "yml") as f:
            input = yml_load(f)
            num_groups = len(input)
            self.groups = []
            for i in range(num_groups):
                group = SpriteGroup(16)
                group.from_yml_rep(input[i])

                palette = EbPalette(1, 16)

                with resource_open("SpriteGroups/" + str(i).zfill(3),
                                   "png") as f2:
                    image = open_indexed_image(f2)
                    group.from_image(image)
                    palette.from_image(image)
                    del image

                self.groups.append(group)

                # Assign the palette number to the sprite
                for j in range(8):
                    if palette.list()[3:] == self.palette_table[j][0].list(
                    )[3:]:
                        group.palette = j
                        break
                else:
                    raise CoilSnakeError("Sprite Group #" + str(i).zfill(3) +
                                         " uses an invalid palette")
Exemplo n.º 2
0
    def read_from_project(self, resource_open):
        with resource_open("sprite_group_palettes", "yml", True) as f:
            self.palette_table.from_yml_file(f)

        with resource_open("sprite_groups", "yml", True) as f:
            input = yml_load(f)
            num_groups = len(input)
            self.groups = []
            for i in range(num_groups):
                group = SpriteGroup(16)
                group.from_yml_rep(input[i])

                palette = EbPalette(1, 16)

                with resource_open("SpriteGroups/" + str(i).zfill(3), "png") as f2:
                    image = open_indexed_image(f2)
                    group.from_image(image)
                    palette.from_image(image)
                    del image

                self.groups.append(group)

                # Assign the palette number to the sprite
                for j in range(8):
                    if palette.list() == self.palette_table[j][0].list():
                        group.palette = j
                        break
                else:
                    raise CoilSnakeError("Sprite Group #" + str(i).zfill(3) + " uses an invalid palette")
Exemplo n.º 3
0
class TestEbPalette(BaseTestCase, TilesetImageTestCase):
    def setup(self):
        super(TestEbPalette, self).setup()
        self.palette = EbPalette(2, 3)

    def test_init(self):
        assert_equal(self.palette.num_subpalettes, 2)
        assert_equal(self.palette.subpalette_length, 3)

    def test_init_invalid(self):
        assert_raises(InvalidArgumentError, EbPalette, 0, 1)
        assert_raises(InvalidArgumentError, EbPalette, -1, 1)
        assert_raises(InvalidArgumentError, EbPalette, 1, 0)
        assert_raises(InvalidArgumentError, EbPalette, 1, -1)
        assert_raises(InvalidArgumentError, EbPalette, 0, 0)
        assert_raises(InvalidArgumentError, EbPalette, -1, -1)

    def test_num_colors(self):
        assert_equal(self.palette.num_colors(), 6)

    def test_getitem(self):
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (0, 0, 0))
        self.palette[0, 1].from_tuple((8, 16, 32))
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (8, 16, 32))

    def test_getitem_invalid(self):
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (-1, 0))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (0, -1))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (-1, -1))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (2, 0))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (0, 3))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (2, 3))

    def test_from_list(self):
        self.palette.from_list([
            0, 0, 0, 40, 8, 72, 80, 88, 48,
            248, 0, 0, 80, 56, 40, 16, 136, 136
        ])
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (40, 8, 72))
        assert_equal(self.palette[0, 2].tuple(), (80, 88, 48))
        assert_equal(self.palette[1, 0].tuple(), (248, 0, 0))
        assert_equal(self.palette[1, 1].tuple(), (80, 56, 40))
        assert_equal(self.palette[1, 2].tuple(), (16, 136, 136))

    def test_to_list(self):
        self.palette[0, 0].from_tuple((0, 0, 0))
        self.palette[0, 1].from_tuple((40, 8, 72))
        self.palette[0, 2].from_tuple((80, 88, 48))
        self.palette[1, 0].from_tuple((248, 0, 0))
        self.palette[1, 1].from_tuple((80, 56, 40))
        self.palette[1, 2].from_tuple((16, 136, 136))
        assert_list_equal(self.palette.list(), [0, 0, 0, 40, 8, 72, 80, 88, 48,
                                                248, 0, 0, 80, 56, 40, 16, 136, 136])

    def test_from_block(self):
        block = Block()
        block.from_list([0, 0, 37, 36, 106, 25,
                         31, 0, 234, 20, 34, 70])
        self.palette.from_block(block)
        assert_list_equal(self.palette.list(), [
            0, 0, 0, 40, 8, 72, 80, 88, 48,
            248, 0, 0, 80, 56, 40, 16, 136, 136
        ])

    def test_to_block(self):
        self.palette.from_list([
            0, 0, 0, 40, 8, 72, 80, 88, 48,
            248, 0, 0, 80, 56, 40, 16, 136, 136
        ])
        block = Block()
        block.from_list([0xff] * 50)
        self.palette.to_block(block, offset=1)
        assert_list_equal(block[0:14].to_list(), [0xff, 0, 0, 37, 36, 106, 25,
                                                  31, 0, 234, 20, 34, 70, 0xff])

    def test_from_image(self):
        self.palette.from_image(self.tile_image_01_img)
        assert_list_equal(self.palette.list(),
                          [0x00, 0x00, 0x00,
                           0x08, 0x00, 0xf8,
                           0xf8, 0x00, 0x00,
                           0x00, 0xf8, 0x18,
                           0xc0, 0xf8, 0x00,
                           0xf8, 0xf8, 0xf8])

    def test_to_image(self):
        image = Image.new('P', (10, 10))
        self.palette[0, 0].from_tuple((0, 0, 0))
        self.palette[0, 1].from_tuple((40, 8, 72))
        self.palette[0, 2].from_tuple((80, 88, 48))
        self.palette[1, 0].from_tuple((248, 0, 0))
        self.palette[1, 1].from_tuple((80, 56, 40))
        self.palette[1, 2].from_tuple((16, 136, 136))
        self.palette.to_image(image)
        assert_list_equal(image.getpalette()[0:(self.palette.num_colors() * 3)],
                          [0, 0, 0, 40, 8, 72, 80, 88, 48,
                           248, 0, 0, 80, 56, 40, 16, 136, 136])
        del image

    def test_add_colors_to_subpalette_single_color(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        for i in range(self.palette.num_subpalettes):
            for j in range(self.palette.subpalette_length):
                if (i, j) not in [(1, 0)]:
                    assert_false(self.palette[i, j].used)

    def test_add_colors_to_subpalette_shared_colors(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16), EbColor(r=128, g=0, b=16)])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        assert_equal(self.palette[1, 1].tuple(), (128, 0, 16))
        assert_true(self.palette[1, 1].used)
        for i in range(self.palette.num_subpalettes):
            for j in range(self.palette.subpalette_length):
                if (i, j) not in [(1, 0), (1, 1)]:
                    assert_false(self.palette[i, j].used)

    def test_add_colors_to_subpalette_shared_colors2(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16),
                                               EbColor(r=128, g=0, b=16)])
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16),
                                               EbColor(r=16, g=32, b=64),
                                               EbColor(r=32, g=32, b=32)])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        assert_equal(self.palette[1, 1].tuple(), (128, 0, 16))
        assert_true(self.palette[1, 1].used)
        assert_false(self.palette[1, 2].used)

        assert_equal(set([self.palette[0, x].tuple() for x in range(self.palette.subpalette_length)]),
                     set([(64, 32, 16), (16, 32, 64), (32, 32, 32)]))
        assert_equal([self.palette[0, x].used for x in range(self.palette.subpalette_length)].count(True), 3)
Exemplo n.º 4
0
class TestEbPalette(BaseTestCase, TilesetImageTestCase):
    def setup(self):
        super(TestEbPalette, self).setup()
        self.palette = EbPalette(2, 3)

    def test_init(self):
        assert_equal(self.palette.num_subpalettes, 2)
        assert_equal(self.palette.subpalette_length, 3)

    def test_init_invalid(self):
        assert_raises(InvalidArgumentError, EbPalette, 0, 1)
        assert_raises(InvalidArgumentError, EbPalette, -1, 1)
        assert_raises(InvalidArgumentError, EbPalette, 1, 0)
        assert_raises(InvalidArgumentError, EbPalette, 1, -1)
        assert_raises(InvalidArgumentError, EbPalette, 0, 0)
        assert_raises(InvalidArgumentError, EbPalette, -1, -1)

    def test_num_colors(self):
        assert_equal(self.palette.num_colors(), 6)

    def test_getitem(self):
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (0, 0, 0))
        self.palette[0, 1].from_tuple((8, 16, 32))
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (8, 16, 32))

    def test_getitem_invalid(self):
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (-1, 0))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (0, -1))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (-1, -1))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (2, 0))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (0, 3))
        assert_raises(InvalidArgumentError, self.palette.__getitem__, (2, 3))

    def test_from_list(self):
        self.palette.from_list([
            0, 0, 0, 40, 8, 72, 80, 88, 48, 248, 0, 0, 80, 56, 40, 16, 136, 136
        ])
        assert_equal(self.palette[0, 0].tuple(), (0, 0, 0))
        assert_equal(self.palette[0, 1].tuple(), (40, 8, 72))
        assert_equal(self.palette[0, 2].tuple(), (80, 88, 48))
        assert_equal(self.palette[1, 0].tuple(), (248, 0, 0))
        assert_equal(self.palette[1, 1].tuple(), (80, 56, 40))
        assert_equal(self.palette[1, 2].tuple(), (16, 136, 136))

    def test_to_list(self):
        self.palette[0, 0].from_tuple((0, 0, 0))
        self.palette[0, 1].from_tuple((40, 8, 72))
        self.palette[0, 2].from_tuple((80, 88, 48))
        self.palette[1, 0].from_tuple((248, 0, 0))
        self.palette[1, 1].from_tuple((80, 56, 40))
        self.palette[1, 2].from_tuple((16, 136, 136))
        assert_list_equal(self.palette.list(), [
            0, 0, 0, 40, 8, 72, 80, 88, 48, 248, 0, 0, 80, 56, 40, 16, 136, 136
        ])

    def test_from_block(self):
        block = Block()
        block.from_list([0, 0, 37, 36, 106, 25, 31, 0, 234, 20, 34, 70])
        self.palette.from_block(block)
        assert_list_equal(self.palette.list(), [
            0, 0, 0, 40, 8, 72, 80, 88, 48, 248, 0, 0, 80, 56, 40, 16, 136, 136
        ])

    def test_to_block(self):
        self.palette.from_list([
            0, 0, 0, 40, 8, 72, 80, 88, 48, 248, 0, 0, 80, 56, 40, 16, 136, 136
        ])
        block = Block()
        block.from_list([0xff] * 50)
        self.palette.to_block(block, offset=1)
        assert_list_equal(
            block[0:14].to_list(),
            [0xff, 0, 0, 37, 36, 106, 25, 31, 0, 234, 20, 34, 70, 0xff])

    def test_from_image(self):
        self.palette.from_image(self.tile_image_01_img)
        assert_list_equal(self.palette.list(), [
            0x00, 0x00, 0x00, 0x08, 0x00, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0xf8,
            0x18, 0xc0, 0xf8, 0x00, 0xf8, 0xf8, 0xf8
        ])

    def test_to_image(self):
        image = Image.new('P', (10, 10))
        self.palette[0, 0].from_tuple((0, 0, 0))
        self.palette[0, 1].from_tuple((40, 8, 72))
        self.palette[0, 2].from_tuple((80, 88, 48))
        self.palette[1, 0].from_tuple((248, 0, 0))
        self.palette[1, 1].from_tuple((80, 56, 40))
        self.palette[1, 2].from_tuple((16, 136, 136))
        self.palette.to_image(image)
        assert_list_equal(
            image.getpalette()[0:(self.palette.num_colors() * 3)], [
                0, 0, 0, 40, 8, 72, 80, 88, 48, 248, 0, 0, 80, 56, 40, 16, 136,
                136
            ])
        del image

    def test_add_colors_to_subpalette_single_color(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        for i in range(self.palette.num_subpalettes):
            for j in range(self.palette.subpalette_length):
                if (i, j) not in [(1, 0)]:
                    assert_false(self.palette[i, j].used)

    def test_add_colors_to_subpalette_shared_colors(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])
        self.palette.add_colors_to_subpalette(
            [EbColor(r=64, g=32, b=16),
             EbColor(r=128, g=0, b=16)])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        assert_equal(self.palette[1, 1].tuple(), (128, 0, 16))
        assert_true(self.palette[1, 1].used)
        for i in range(self.palette.num_subpalettes):
            for j in range(self.palette.subpalette_length):
                if (i, j) not in [(1, 0), (1, 1)]:
                    assert_false(self.palette[i, j].used)

    def test_add_colors_to_subpalette_shared_colors2(self):
        self.palette.add_colors_to_subpalette([EbColor(r=64, g=32, b=16)])
        self.palette.add_colors_to_subpalette(
            [EbColor(r=64, g=32, b=16),
             EbColor(r=128, g=0, b=16)])
        self.palette.add_colors_to_subpalette([
            EbColor(r=64, g=32, b=16),
            EbColor(r=16, g=32, b=64),
            EbColor(r=32, g=32, b=32)
        ])

        assert_equal(self.palette[1, 0].tuple(), (64, 32, 16))
        assert_true(self.palette[1, 0].used)
        assert_equal(self.palette[1, 1].tuple(), (128, 0, 16))
        assert_true(self.palette[1, 1].used)
        assert_false(self.palette[1, 2].used)

        assert_equal(
            set([
                self.palette[0, x].tuple()
                for x in range(self.palette.subpalette_length)
            ]), set([(64, 32, 16), (16, 32, 64), (32, 32, 32)]))
        assert_equal([
            self.palette[0, x].used
            for x in range(self.palette.subpalette_length)
        ].count(True), 3)