def test_creation_with_gradients(self, _init_pygame,
                                  default_ui_manager: UIManager):
     EllipseDrawableShape(containing_rect=pygame.Rect(0, 0, 100, 100),
                          theming_parameters={
                              'text':
                              'test',
                              'font':
                              default_ui_manager.get_theme().get_font(
                                  object_ids=[], element_ids=[]),
                              'shadow_width':
                              2,
                              'border_width':
                              2,
                              'normal_border':
                              ColourGradient(0, pygame.Color('#000000'),
                                             pygame.Color('#FFFFFF')),
                              'normal_bg':
                              ColourGradient(0, pygame.Color('#000000'),
                                             pygame.Color('#FFFFFF')),
                              'shape_corner_radius':
                              2,
                              'text_horiz_alignment':
                              'center',
                              'text_vert_alignment':
                              'center'
                          },
                          states=['normal'],
                          manager=default_ui_manager)
Ejemplo n.º 2
0
    def _load_colour_or_gradient_from_theme(
            theme_colours_dictionary: Dict[str, str],
            colour_id: str) -> Union[pygame.Color, ColourGradient]:
        """
        Load a single colour, or gradient, from theming file data.

        :param theme_colours_dictionary: Part of the theming file data relating to colours.
        :param colour_id: The ID of the colour or gradient to load.
        """
        loaded_colour_or_gradient = None
        string_data = theme_colours_dictionary[colour_id]
        if ',' in string_data:
            # expecting some type of gradient description in string data
            string_data_list = string_data.split(',')

            try:
                gradient_direction = int(string_data_list[-1])
            except ValueError:
                warnings.warn("Invalid gradient: " + string_data + " for id:" +
                              colour_id + " in theme file")
            else:
                if len(string_data_list) == 3:
                    # two colour gradient
                    try:
                        colour_1 = pygame.Color(string_data_list[0])
                        colour_2 = pygame.Color(string_data_list[1])
                        loaded_colour_or_gradient = ColourGradient(
                            gradient_direction, colour_1, colour_2)
                    except ValueError:
                        warnings.warn("Invalid gradient: " + string_data +
                                      " for id:" + colour_id +
                                      " in theme file")
                elif len(string_data_list) == 4:
                    # three colour gradient
                    try:
                        colour_1 = pygame.Color(string_data_list[0])
                        colour_2 = pygame.Color(string_data_list[1])
                        colour_3 = pygame.Color(string_data_list[2])
                        loaded_colour_or_gradient = ColourGradient(
                            gradient_direction, colour_1, colour_2, colour_3)
                    except ValueError:
                        warnings.warn("Invalid gradient: " + string_data +
                                      " for id:" + colour_id +
                                      " in theme file")
                else:
                    warnings.warn("Invalid gradient: " + string_data +
                                  " for id:" + colour_id + " in theme file")
        else:
            # expecting a regular hex colour in string data
            try:
                loaded_colour_or_gradient = pygame.Color(string_data)
            except ValueError:
                warnings.warn("Colour hex code: " + string_data + " for id:" +
                              colour_id + " invalid in theme file")

        if loaded_colour_or_gradient is None:
            # if the colour or gradient data is invalid, return a black default colour
            loaded_colour_or_gradient = pygame.Color("#000000")

        return loaded_colour_or_gradient
Ejemplo n.º 3
0
 def test_creation_with_filled_bar_no_gradient(
         self, _init_pygame, default_ui_manager: UIManager):
     RoundedRectangleShape(containing_rect=pygame.Rect(0, 0, 100, 100),
                           theming_parameters={
                               'text':
                               'test',
                               'font':
                               default_ui_manager.get_theme().get_font([]),
                               'shadow_width':
                               2,
                               'border_width':
                               1,
                               'normal_border':
                               ColourGradient(0, pygame.Color('#000000'),
                                              pygame.Color('#FFFFFF')),
                               'normal_bg':
                               ColourGradient(0, pygame.Color('#000000'),
                                              pygame.Color('#FFFFFF')),
                               'filled_bar':
                               pygame.Color('#000000'),
                               'filled_bar_width':
                               50,
                               'shape_corner_radius':
                               2,
                               'text_horiz_alignment':
                               'center',
                               'text_vert_alignment':
                               'center'
                           },
                           states=['normal'],
                           manager=default_ui_manager)
Ejemplo n.º 4
0
    def load_colour_or_gradient_from_theme(theme_colours_dictionary,
                                           colour_id):
        loaded_colour_or_gradient = None
        string_data = theme_colours_dictionary[colour_id]
        if ',' in string_data:
            # expecting some type of gradient description in string data
            string_data_list = string_data.split(',')
            gradient_direction = None
            try:
                gradient_direction = int(string_data_list[-1])
            except ValueError:
                warnings.warn("Invalid gradient: " + string_data + " for id:" +
                              colour_id + " in theme file")

            if gradient_direction is not None and len(string_data_list) == 3:
                # two colour gradient
                try:
                    colour_1 = pygame.Color(string_data_list[0])
                    colour_2 = pygame.Color(string_data_list[1])
                    loaded_colour_or_gradient = ColourGradient(
                        gradient_direction, colour_1, colour_2)
                except ValueError:
                    warnings.warn("Invalid gradient: " + string_data +
                                  " for id:" + colour_id + " in theme file")
            elif gradient_direction is not None and len(string_data_list) == 4:
                # three colour gradient
                try:
                    colour_1 = pygame.Color(string_data_list[0])
                    colour_2 = pygame.Color(string_data_list[1])
                    colour_3 = pygame.Color(string_data_list[2])
                    loaded_colour_or_gradient = ColourGradient(
                        gradient_direction, colour_1, colour_2, colour_3)
                except ValueError:
                    warnings.warn("Invalid gradient: " + string_data +
                                  " for id:" + colour_id + " in theme file")
            else:
                warnings.warn("Invalid gradient: " + string_data + " for id:" +
                              colour_id + " in theme file")
        else:
            # expecting a regular hex colour in string data
            try:
                loaded_colour_or_gradient = pygame.Color(string_data)
            except ValueError:
                warnings.warn("Colour hex code: " + string_data + " for id:" +
                              colour_id + " invalid in theme file")

        if loaded_colour_or_gradient is None:
            # if the colour or gradient data is invalid, return a black default colour
            loaded_colour_or_gradient = pygame.Color("#000000")

        return loaded_colour_or_gradient
Ejemplo n.º 5
0
    def test_apply_gradient_to_surface(self, _init_pygame):
        gradient = ColourGradient(angle_direction=90,
                                  colour_1=pygame.Color("#FF0000"),
                                  colour_2=pygame.Color("#00FF00"),
                                  colour_3=pygame.Color("#0000FF"))

        test_surface = pygame.Surface((50, 50), flags=pygame.SRCALPHA, depth=32)
        test_surface.fill(pygame.Color(255, 255, 255, 255))

        gradient.apply_gradient_to_surface(test_surface)

        after_application_colour = test_surface.get_at((0, 0))

        assert after_application_colour == pygame.Color(0, 10, 242, 254)
Ejemplo n.º 6
0
    def test_creation(self, _init_pygame):
        gradient = ColourGradient(angle_direction=90,
                                  colour_1=pygame.Color("#FF0000"),
                                  colour_2=pygame.Color("#00FF00"),
                                  colour_3=pygame.Color("#0000FF"))

        assert str(gradient) == "90_255_0_0_255_0_255_0_255_0_0_255_255"
Ejemplo n.º 7
0
 def test_redraw_bg_and_fg_gradient(self, _init_pygame):
     dictionary = UIFontDictionary(BlockingThreadedResourceLoader())
     style = CharStyle()
     gradient = ColourGradient(0, pygame.Color('#FFFF00'), pygame.Color('#FF0000'))
     chunk = StyledChunk(font_size=14, font_name='fira_code',
                         chunk='text', style=style, colour=gradient,
                         bg_colour=gradient,
                         is_link=False, link_href='test', link_style=CharStyle(),
                         position=(0, 0), font_dictionary=dictionary)
     chunk.redraw()
Ejemplo n.º 8
0
 def test_redraw_changed_metrics(self, _init_pygame):
     dictionary = UIFontDictionary()
     style = CharStyle()
     gradient = ColourGradient(0, pygame.Color('#FFFF00'),
                               pygame.Color('#FF0000'))
     chunk = StyledChunk(font_size=14,
                         font_name='fira_code',
                         chunk='text',
                         style=style,
                         color=pygame.Color('#FFFF00'),
                         bg_color=gradient,
                         is_link=False,
                         link_href='test',
                         link_style=CharStyle(),
                         position=(0, 0),
                         font_dictionary=dictionary)
     chunk.chunk = 'testy'
     chunk.redraw()
Ejemplo n.º 9
0
    def test_finalise(self, _init_pygame, default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 30)
        the_font.origin = True
        the_font.pad = True

        chunk_1 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#FF0000'))

        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        chunk_1.finalise(layout_surface, pygame.Rect(0, 0, 200, 300),
                         chunk_1.y_origin, chunk_1.height, chunk_1.height)

        assert layout_surface.get_at((10, 10)) == pygame.Color('#FF0000')

        chunk_2 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=ColourGradient(
                                          0, pygame.Color('#FFFF00'),
                                          pygame.Color('#FF0000')),
                                      using_default_text_colour=False,
                                      bg_colour=pygame.Color('#FF0000'))

        layout_surface_2 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_2.fill((0, 0, 0, 0))
        chunk_2.finalise(layout_surface_2, pygame.Rect(0, 0, 200, 300),
                         chunk_2.y_origin, chunk_2.height, chunk_2.height)

        assert layout_surface_2.get_at((10, 10)) == pygame.Color('#FF0000')

        chunk_3 = TextLineChunkFTFont(
            text='test',
            font=the_font,
            underlined=False,
            colour=ColourGradient(0, pygame.Color('#FFFF00'),
                                  pygame.Color('#FF0000')),
            using_default_text_colour=False,
            bg_colour=ColourGradient(0, pygame.Color('#FFFF00'),
                                     pygame.Color('#FF0000')))

        layout_surface_3 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_3.fill((0, 0, 0, 0))
        chunk_3.finalise(layout_surface_3, pygame.Rect(0, 0, 200, 300),
                         chunk_3.y_origin, chunk_3.height, chunk_3.height)

        assert layout_surface_3.get_at((10, 10)) != pygame.Color('#00000000')

        chunk_4 = TextLineChunkFTFont(text='test',
                                      font=the_font,
                                      underlined=False,
                                      colour=pygame.Color('#FFFFFF'),
                                      using_default_text_colour=False,
                                      bg_colour=ColourGradient(
                                          0, pygame.Color('#FFFF00'),
                                          pygame.Color('#FF0000')))

        layout_surface_4 = pygame.Surface((200, 300),
                                          depth=32,
                                          flags=pygame.SRCALPHA)
        layout_surface_4.fill((0, 0, 0, 0))
        chunk_4.finalise(layout_surface_4, pygame.Rect(0, 0, 200, 300),
                         chunk_4.y_origin, chunk_4.height, chunk_4.height)

        assert layout_surface_4.get_at((10, 10)) != pygame.Color('#00000000')