def test_scroll_animation_horizontally_from_right_to_left(self): text_height = 10 font = Font() font.auto_adjust_font_size_to_height(text_height) background_color = Color.Red() effect = TextEffect(Color.Blue(), background_color) text = "KeesWare" effect.draw_text(text, font) effect.crop() self.image = effect.get_image() animation_size = (30, 10) animation = ScrollAnimation(animation_size, self.image, background_color) start_point = [animation_size[0], 0] image_size = self.image.size end_point = [-image_size[0], 0] # move by one pixel per frame frames_amount = image_size[0] + animation_size[0] animation.pre_render(start_point, end_point, frames_amount) first_frame = animation[0] middle_frame = animation[len(animation) / 2] animation.save("2.gif") self.assertEqual(first_frame.getpixel((0, 0)), background_color.get_as_tuple()) self.assertEqual(middle_frame.size, animation_size) self.assertEqual(middle_frame.getpixel((0, 0)), background_color.get_as_tuple()) self.assertEqual(middle_frame.getpixel((1, 5)), (0, 0, 255)) self.assertEqual(len(animation), frames_amount)
def recreate_text_effect(self): try: self.__logger.info(__name__ + " recreating text effect") width, height = self.__display.get_size() font = Font() font.auto_adjust_font_size_to_height(height) parameters = TextEffectParameters() parameters.text_color = Color.from_normalized_float( self.ids.text_color_button.background_color) parameters.background_color = Color.from_normalized_float( self.ids.background_color_button.background_color) parameters.text = self.ids.text_input.text parameters.display_size = self.__display.get_size() parameters.font = font self.__text_effect.apply(parameters) except Exception as ex: self.__logger.info( __name__ + " recreate text effect failed with ex={0}".format(ex.message)) popup = ErrorWindow() popup.gather_traces(ex.message) Clock.unschedule(self.send_current_frame) popup.open()
def apply_effect(self, time_delta): try: if self.__frame_count % 10 == 0: self.__logger.debug( __name__ + " drawing frame {0}".format(self.__frame_count)) self.__frame_count += 1 display_size = Context.get_display().get_size() effect = RainbowEffectAnimation(display_size) left_color = self.ids.left_color_button.background_color left_color_end = self.ids.left_color_button_end.background_color right_color = self.ids.right_color_button.background_color right_color_end = self.ids.right_color_button_end.background_color left_colors = [ Color.from_normalized_float(left_color).rgb, Color.from_normalized_float(left_color_end).rgb ] right_colors = [ Color.from_normalized_float(right_color).rgb, Color.from_normalized_float(right_color_end).rgb ] effect.set_left_colors(left_colors) effect.set_right_colors(right_colors) effect.set_duration(2000) effect.draw_vertical_rainbow_frame_at(self._time_elapsed) image = effect.get_image() self._effect_provider.set_image(image) self._effect_provider.apply_image() if self._direction is "Up": self._time_elapsed += int(time_delta * 1000) else: self._time_elapsed -= int(time_delta * 1000) if self._time_elapsed > 2000: self._direction = "Down" self._time_elapsed = 1999 if self._time_elapsed < 0: self._direction = "Up" self._time_elapsed = 0 except Exception as ex: self.__logger.error(__name__ + " drawing frame failed") popup = ErrorWindow() popup.gather_traces(ex.message) Clock.unschedule(self.apply_effect) popup.open()
def test_generate_initial_frame(self): size = (17, 10) effect = RainbowEffectAnimation(size) left_start_color = Color(255, 0, 0) left_end_color = Color(0, 0, 0) right_start_color = Color(0, 0, 255) right_end_color = Color(0, 255, 0) duration_in_miliseconds = 2000 effect.set_left_colors([left_start_color.rgb, left_end_color.rgb]) effect.set_right_colors([right_start_color.rgb, right_end_color.rgb]) effect.set_duration(duration_in_miliseconds) effect.draw_vertical_rainbow_frame_at(0) image = effect.get_image() self.assertEqual(image.getpixel((0, 0)), (255, 0, 0)) self.assertEqual(image.getpixel((16, 9)), (0, 0, 255)) self.assertEqual(image.getpixel((8, 4)), (127, 0, 127))
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()
def test_paint_straight_line(self): widget_size = (500, 1000) display_size = (50, 100) start_point = [100, 400] end_point = [400, 800] brush_color = Color.Blue() point_size = 1 effect = PaintEffect(display_size, widget_size) effect.set_point_size(point_size) effect.set_line_color(brush_color) effect.draw_lines([start_point, end_point]) image = effect.get_image() self.assertEqual(image.size, display_size) self.assertEqual(image.getpixel((10, 40)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((40, 80)), brush_color.get_as_tuple())
def test_create_30x10_rainbow_image(self): size = (29, 10) effect = RainbowEffect(size) start_color = Color(127, 255, 63) end_color = Color(255, 0, 180) effect.draw_vertical_rainbow(start_color, end_color) image = effect.get_image() self.assertEqual(image.getpixel((0, 5)), start_color.get_as_tuple()) self.assertEqual(image.getpixel((28, 9)), end_color.get_as_tuple()) self.assertEqual(image.getpixel((14, 0)), (191, 127, 121))
def test_create_3x2_rainbow_image(self): size = (3, 2) effect = RainbowEffect(size) start_color = Color(255, 0, 0) end_color = Color(0, 0, 255) effect.draw_vertical_rainbow(start_color, end_color) image = effect.get_image() self.assertEqual(image.getpixel((0, 0)), start_color.get_as_tuple()) self.assertEqual(image.getpixel((2, 1)), end_color.get_as_tuple()) self.assertEqual(image.getpixel((1, 1)), (127, 0, 127))
def test_paint_square(self): widget_size = (500, 1000) display_size = (50, 100) points = [[100, 100], [100, 900], [400, 900], [400, 100], [100, 100]] brush_color = Color.Red() point_size = 3 effect = PaintEffect(display_size, widget_size) effect.set_point_size(point_size) effect.set_line_color(brush_color) effect.draw_lines(points) image = effect.get_image() self.assertEqual(image.size, display_size) self.assertEqual(image.getpixel((10, 10)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((10, 90)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((40, 90)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((40, 10)), brush_color.get_as_tuple())
def test_paint_cross(self): widget_size = (500, 1000) display_size = (50, 100) vertical_line = [[250, 0], [250, 1000]] horizontal_line = [[0, 800], [500, 800]] brush_color = Color.White() point_size = 5 effect = PaintEffect(display_size, widget_size) effect.set_point_size(point_size) effect.set_line_color(brush_color) effect.draw_lines(vertical_line) effect.draw_lines(horizontal_line) image = effect.get_image() self.assertEqual(image.size, display_size) self.assertEqual(image.getpixel((25, 0)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((25, 99)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((0, 80)), brush_color.get_as_tuple()) self.assertEqual(image.getpixel((49, 80)), brush_color.get_as_tuple())
def test_scroll_animation_horizontally_from_right_to_left_and_down(self): text_height = 10 font = Font() font.auto_adjust_font_size_to_height(text_height) effect = TextEffect() text = "KeesWare" effect.draw_text(text, font) effect.crop() self.image = effect.get_image() animation_size = (30, 10) animation = ScrollAnimation(animation_size, self.image, Color.Black()) start_point = [0, 0] image_size = self.image.size end_point = [-image_size[0], -image_size[1]] # move by one pixel per frame frames_amount = image_size[0] + animation_size[0] animation.pre_render(start_point, end_point, frames_amount) middle_frame = animation[len(animation) / 2 - 1] self.assertEqual(middle_frame.size, animation_size) self.assertEqual(middle_frame.getpixel((6, 4)), (255, 255, 255)) self.assertEqual(len(animation), frames_amount)
def test_get_rgba(self): color = Color(240, 15, 204) self.assertEqual(color.get_as_rgba_tuple(), (240, 15, 204, 255))
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 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 apply(self, parameters): self.__effect.set_point_size(int(parameters.point_size)) self.__effect.set_line_color( Color.from_normalized_float(parameters.color)) self.__effect.draw_lines(parameters.points)
def test_to_int(self): color = Color(255, 255, 255) self.assertEqual(color.get_as_int(), 16777215)
def test_to_int(self): color = Color(240, 15, 204) self.assertEqual(color.get_as_int(), 15732684)
def test_convert_from_normalized_float(self): normalized_color = (1, 0, 0, 1) color = Color.from_normalized_float(normalized_color) self.assertEqual(color.get_as_tuple(), (255, 0, 0))
def __init__(self, size, widget_size): self.__size = size self.__widget_size = widget_size self.__point_size = 10 self.__line_color = Color.White() self.__image = ImageFactory.create_rgb_image(self.__size)