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 from_image(self, image, tileset, palette, no_flip=False): if palette.num_subpalettes == 1: self._from_image_with_single_subpalette(image, tileset, palette, no_flip) else: # Multiple subpalettes, so we have to figure out which tile should use which subpalette palette.from_image(image) rgb_image = image.convert("RGB") del (image) rgb_image_data = rgb_image.load() del rgb_image tile = [ array('B', [0] * tileset.tile_width) for i in xrange(tileset.tile_height) ] for arrangement_y in xrange(self.height): image_y = arrangement_y * tileset.tile_height for arrangement_x in xrange(self.width): image_x = arrangement_x * tileset.tile_width tile_colors = set() for tile_y in xrange(tileset.tile_height): image_tile_y = image_y + tile_y for tile_x in xrange(tileset.tile_width): r, g, b = rgb_image_data[image_x + tile_x, image_tile_y] tile_colors.add( EbColor(r=r & 0xf8, g=g & 0xf8, b=b & 0xf8)) try: subpalette_id = palette.get_subpalette_for_colors( tile_colors) except InvalidArgumentError as e: raise InvalidUserDataError( "Could not fit all colors in {}x{} square at ({},{}) into a single {}-color subpalette\nColors: {}\nPalette: {}" .format(tileset.tile_width, tileset.tile_height, image_x, image_y, palette.subpalette_length, list(tile_colors), palette.subpalettes)) for tile_y in xrange(tileset.tile_height): image_tile_y = image_y + tile_y for tile_x in xrange(tileset.tile_width): image_tile_x = image_x + tile_x tile[tile_y][tile_x] = palette.get_color_id( rgb_image_data[image_tile_x, image_tile_y], subpalette_id) tile_id, vflip, hflip = tileset.add_tile(tile, no_flip) arrangement_item = self.arrangement[arrangement_y][ arrangement_x] arrangement_item.tile = tile_id arrangement_item.subpalette = subpalette_id arrangement_item.is_vertically_flipped = vflip arrangement_item.is_horizontally_flipped = hflip arrangement_item.is_priority = False
def test_from_image_single_subpalette(self): palette = EbPalette(1, 2) tileset = EbGraphicTileset(num_tiles=6, tile_width=8, tile_height=8) arrangement = EbTileArrangement(width=6, height=1) arrangement.from_image(self.tile_8x8_2bpp_2_img, tileset=tileset, palette=palette) assert_equal(palette[0, 0], EbColor(0, 0, 0)) assert_equal(palette[0, 1], EbColor(0xf8, 0xf8, 0xf8)) item = arrangement[0, 0] assert_equal(item.subpalette, 0) assert_equal(arrangement[1, 0].tile, item.tile) assert_equal(arrangement[1, 0].is_horizontally_flipped, not item.is_horizontally_flipped) assert_equal(arrangement[1, 0].is_vertically_flipped, item.is_vertically_flipped) assert_equal(arrangement[1, 0].subpalette, 0) assert_equal(arrangement[2, 0].tile, item.tile) assert_equal(arrangement[2, 0].is_horizontally_flipped, item.is_horizontally_flipped) assert_equal(arrangement[2, 0].is_vertically_flipped, not item.is_vertically_flipped) assert_equal(arrangement[2, 0].subpalette, 0) assert_equal(arrangement[3, 0].tile, item.tile) assert_equal(arrangement[3, 0].is_horizontally_flipped, not item.is_horizontally_flipped) assert_equal(arrangement[2, 0].is_vertically_flipped, not item.is_vertically_flipped) assert_equal(arrangement[3, 0].subpalette, 0) assert_not_equal(arrangement[4, 0].tile, item.tile) assert_equal(arrangement[4, 0].subpalette, 0) assert_equal(arrangement[5, 0].tile, item.tile) assert_equal(arrangement[5, 0].is_horizontally_flipped, item.is_horizontally_flipped) assert_equal(arrangement[5, 0].is_vertically_flipped, item.is_vertically_flipped) assert_equal(arrangement[5, 0].subpalette, 0)
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)
def test_from_image_2_subpalettes(self): palette = EbPalette(2, 4) tileset = EbGraphicTileset(num_tiles=4, tile_width=8, tile_height=8) arrangement = EbTileArrangement(width=4, height=1) arrangement.from_image(image=self.tile_8x8_2bpp_3_img, tileset=tileset, palette=palette) img_palette = self.tile_8x8_2bpp_3_img.getpalette() self.tile_8x8_2bpp_3_img.putpalette([x & 0xf8 for x in img_palette]) before_image_rgb = self.tile_8x8_2bpp_3_img.convert("RGB") after_image_rgb = arrangement.image(tileset, palette).convert("RGB") assert_images_equal(before_image_rgb, after_image_rgb) assert_set_equal({palette[1, i] for i in range(4)}, { EbColor(24, 0, 248), EbColor(0, 248, 24), EbColor(152, 0, 248), EbColor(248, 144, 0) }) assert_set_equal({palette[0, i] for i in range(4)}, { EbColor(24, 0, 248), EbColor(0, 248, 24), EbColor(216, 248, 0), EbColor(152, 0, 248) }) assert_equal(arrangement[0, 0].tile, 0) assert_equal(arrangement[0, 0].subpalette, 0) assert_equal({tileset[0][0][i] for i in [-1, -2, -3, -4]}, {0, 1, 2, 3}) assert_equal(arrangement[1, 0].tile, 1) assert_equal(arrangement[1, 0].subpalette, 1) assert_equal({tileset[1][0][i] for i in [-1, -2, -3, -4]}, {0, 1, 2, 3}) assert_equal(arrangement[2, 0].tile, 2) assert_equal(arrangement[2, 0].subpalette, 0) assert_equal(arrangement[3, 0].tile, 3) assert_equal(arrangement[3, 0].subpalette, 1)
def test_8x8_5colors_2subpalettesof4(self): return eb_palette = EbPalette(num_subpalettes=2, subpalette_length=4) setup_eb_palette_from_image(eb_palette, self.tile_8x8_2bpp_3_img, 8, 8) assert_equal(eb_palette.num_subpalettes, 2) assert_equal(eb_palette.subpalette_length, 4) assert_set_equal({eb_palette[1, i] for i in range(4)}, { EbColor(24, 0, 248), EbColor(0, 248, 24), EbColor(152, 0, 248), EbColor(248, 144, 0) }) assert_set_equal({eb_palette[0, i] for i in range(4)}, { EbColor(24, 0, 248), EbColor(0, 248, 24), EbColor(216, 248, 0), EbColor(152, 0, 248) })
def test_init(self): color2 = EbColor(r=3, g=255, b=84) assert_equal(color2.r, 3) assert_equal(color2.g, 255) assert_equal(color2.b, 84)
def setup(self): self.color = EbColor()