def display_frame(self, image): (width, height) = image.size image_length = width * height strip_length = self._stripe.numPixels() if image_length != strip_length: raise AttributeError("trying to display image with different size than the stripe") for y in range(height): for x in range(width): r, g, b = image.getpixel((x, y)) color = Color(r, g, b) diode_index_in_stripe = y * width if y % 2 == 0: diode_index_in_stripe += x else: diode_index_in_stripe += (width - 1) - x self._stripe.setPixelColor(diode_index_in_stripe, color.get_as_int()) self._stripe.show()
class TestDevice(unittest.TestCase): def setUp(self): self.red = Color(255, 0, 0) self.green = Color(0, 255, 0) self.blue = Color(0, 0, 255) self.stripe_factory = MockStripeFactory() def create_stripe_and_device(self, size): device = Ws281xDevice(self.stripe_factory, size) stripe = self.stripe_factory.get_stripe() return stripe, device def test_3x1_panel(self): size = (3, 1) stripe, device = self.create_stripe_and_device(size) image = ImageFactory.create_rgb_image(size) image.putpixel((0, 0), self.red.get_as_tuple()) image.putpixel((1, 0), self.green.get_as_tuple()) image.putpixel((2, 0), self.blue.get_as_tuple()) device.display_frame(image) self.assertEqual(stripe.getPixelColor(0), self.red.get_as_int()) self.assertEqual(stripe.getPixelColor(1), self.green.get_as_int()) self.assertEqual(stripe.getPixelColor(2), self.blue.get_as_int()) def test_3x2_panel(self): size = (3, 2) stripe, device = self.create_stripe_and_device(size) image = ImageFactory.create_rgb_image(size) image.putpixel((0, 0), self.red.get_as_tuple()) image.putpixel((1, 0), self.green.get_as_tuple()) image.putpixel((2, 0), self.blue.get_as_tuple()) image.putpixel((0, 1), self.red.get_as_tuple()) image.putpixel((1, 1), self.green.get_as_tuple()) image.putpixel((2, 1), self.blue.get_as_tuple()) device.display_frame(image) self.assertEqual(stripe.getPixelColor(5), self.red.get_as_int()) self.assertEqual(stripe.getPixelColor(4), self.green.get_as_int()) self.assertEqual(stripe.getPixelColor(3), self.blue.get_as_int())
def test_to_int(self): color = Color(240, 15, 204) self.assertEqual(color.get_as_int(), 15732684)
def test_to_int(self): color = Color(255, 255, 255) self.assertEqual(color.get_as_int(), 16777215)