def test_block_init(): assert Block(1, 1) == Block.from_id(1, 1) assert Block(b'stone') == Block.from_name('stone') assert Block('stone') == Block.from_name('stone') assert Block('air', 1) == Block.from_name('air', 1) assert Block('#ffffff') == Block.from_color('#ffffff') assert Block((0, 0, 0)) == Block.from_color((0, 0, 0)) assert Block(id=1) == Block.from_id(1) assert Block(name='grass') == Block.from_name('grass') assert Block(color='#ffffff', exact=False) == Block.from_color('#ffffff') with pytest.raises(TypeError): Block() with pytest.raises(TypeError): Block(1, 2, 3)
def test_block_from_color(): assert Block.from_color(b'#ffffff') == Block(35, 0) assert Block.from_color('#ffffff') == Block(35, 0) assert Block.from_color((255, 255, 255)) == Block(35, 0) assert Block.from_color((0.0, 0.0, 0.0)) == Block(35, 15) with pytest.raises(ValueError): Block.from_color('white') with pytest.raises(ValueError): Block.from_color((1, 2))
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(len(self.palette), palette=self.palette_img) new_state = { o + V(0, y, x): Block.from_color(self.palette[img.getpixel((x, y))], exact=True) for x in range(img.size[0]) for y in range(img.size[1]) } with self.world.connection.batch_start(): for v, b in track_changes(self.state, new_state).items(): self.world.blocks[v] = b self.state = new_state
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(len(self.palette), palette=self.palette_img) new_state = { o + V(0, y, x): Block.from_color(self.palette[img.getpixel( (x, y))], exact=True) for x in range(img.size[0]) for y in range(img.size[1]) } with self.world.connection.batch_start(): for v, b in track_changes(self.state, new_state).items(): self.world.blocks[v] = b self.state = new_state
def test_block_from_color_exact(): with pytest.raises(ValueError): Block.from_color(b'#ffffff', exact=True)