Skip to content

Commit

Permalink
Merge pull request #597 from MyreMylar/premul-alpha-everywhere
Browse files Browse the repository at this point in the history
use premul_alpha everywhere
  • Loading branch information
MyreMylar authored Jun 2, 2024
2 parents 5fd3f4a + c163217 commit e9ae2b8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
4 changes: 4 additions & 0 deletions docs/source/theme_reference/theme_button.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,31 @@ Images
- "**package** - The name of the python package containing this resource - e.g. 'data.images'
- "**resource** - The file name of the resource in the python package - e.g. 'splat.png' - Use a 'package' and 'resource' or a 'path' not both.
- "**sub_surface_rect**" - An optional rectangle (described like "x,y,width,height") that will be used to grab a smaller portion of the image specified. This allows us to create many image surfaces from one image file.
- "**premultiplied**" - Optional parameter to declare that a loaded image already contains premultiplied alpha and does not need premultiplying. Set to "1" to enable, "0" to disable (default).

- "**hovered_image**" - The image displayed in the buttons hovered state. It has the following block of sub-parameters:

- "**path**" - The string path to the image to be displayed. OR
- "**package** - The name of the python package containing this resource - e.g. 'data.images'
- "**resource** - The file name of the resource in the python package - e.g. 'splat.png' - Use a 'package' and 'resource' or a 'path' not both.
- "**sub_surface_rect**" - An optional rectangle (described like "x,y,width,height") that will be used to grab a smaller portion of the image specified. This allows us to create many image surfaces from one image file.
- "**premultiplied**" - Optional parameter to declare that a loaded image already contains premultiplied alpha and does not need premultiplying. Set to "1" to enable, "0" to disable (default).

- "**selected_image**" - The image displayed in the buttons select focused state. It has the following block of sub-parameters:

- "**path**" - The string path to the image to be displayed. OR
- "**package** - The name of the python package containing this resource - e.g. 'data.images'
- "**resource** - The file name of the resource in the python package - e.g. 'splat.png' - Use a 'package' and 'resource' or a 'path' not both.
- "**sub_surface_rect**" - An optional rectangle (described like "x,y,width,height") that will be used to grab a smaller portion of the image specified. This allows us to create many image surfaces from one image file.
- "**premultiplied**" - Optional parameter to declare that a loaded image already contains premultiplied alpha and does not need premultiplying. Set to "1" to enable, "0" to disable (default).

- "**disabled_image**" - The image displayed in the buttons disabled state. It has the following block of sub-parameters:

- "**path**" - The string path to the image to be displayed. OR
- "**package** - The name of the python package containing this resource - e.g. 'data.images'
- "**resource** - The file name of the resource in the python package - e.g. 'splat.png' - Use a 'package' and 'resource' or a 'path' not both.
- "**sub_surface_rect**" - An optional rectangle (described like "x,y,width,height") that will be used to grab a smaller portion of the image specified. This allows us to create many image surfaces from one image file.
- "**premultiplied**" - Optional parameter to declare that a loaded image already contains premultiplied alpha and does not need premultiplying. Set to "1" to enable, "0" to disable (default).


Misc
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_panel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Images
- "**package** - The name of the python package containing this resource - e.g. 'data.images'
- "**resource** - The file name of the resource in the python package - e.g. 'splat.png' - Use a 'package' and 'resource' or a 'path' not both.
- "**sub_surface_rect**" - An optional rectangle (described like "x,y,width,height") that will be used to grab a smaller portion of the image specified. This allows us to create many image surfaces from one image file.
- "**premultiplied**" - Optional parameter to declare that a loaded image already contains premultiplied alpha and does not need premultiplying. Set to "1" to enable, "0" to disable (default).

Misc
----
Expand Down
10 changes: 8 additions & 2 deletions pygame_gui/core/ui_appearance_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,14 @@ def _load_images(self):
self.ui_element_image_surfaces[element_key][image_id] = surf_resource

def _load_image_from_path(self, res_data):
premultiplied = False
if 'premultiplied' in res_data:
premultiplied = bool(res_data['premultiplied'])
resource_id = res_data['path']
if resource_id in self.image_resources:
image_resource = self.image_resources[resource_id]
else:
image_resource = ImageResource(resource_id, res_data['path'])
image_resource = ImageResource(resource_id, res_data['path'], premultiplied)
if self._resource_loader.started():
error = image_resource.load()
if error is not None:
Expand All @@ -304,12 +307,15 @@ def _load_image_from_path(self, res_data):
return image_resource

def _load_image_resource(self, res_data):
premultiplied = False
if 'premultiplied' in res_data:
premultiplied = bool(res_data['premultiplied'])
resource_id = (str(res_data['package']) + '/' + str(res_data['resource']))
if resource_id in self.image_resources:
image_resource = self.image_resources[resource_id]
else:
package_resource = PackageResource(res_data['package'], res_data['resource'])
image_resource = ImageResource(resource_id, package_resource)
image_resource = ImageResource(resource_id, package_resource, premultiplied)
if self._resource_loader.started():
error = image_resource.load()
if error is not None:
Expand Down
23 changes: 5 additions & 18 deletions pygame_gui/core/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,6 @@ def restore_premul_col(premul_colour: pygame.Color) -> pygame.Color:
premul_colour.a)


def premul_alpha_surface(surface: pygame.surface.Surface) -> pygame.surface.Surface:
"""
Perform a pre-multiply alpha operation on a pygame surface's colours.
"""
surf_copy = surface.copy()
surf_copy.fill(pygame.Color('#FFFFFF00'), special_flags=pygame.BLEND_RGB_MAX)
manipulate_surf = pygame.surface.Surface(surf_copy.get_size(),
flags=pygame.SRCALPHA, depth=32)
# Can't be exactly transparent black or we trigger SDL1 'bug'
manipulate_surf.fill(pygame.Color('#00000001'))
manipulate_surf.blit(surf_copy, (0, 0))
surface.blit(manipulate_surf, (0, 0), special_flags=pygame.BLEND_RGB_MULT)
return surface


def render_white_text_alpha_black_bg(font: IGUIFontInterface,
text: str) -> pygame.surface.Surface:
"""
Expand Down Expand Up @@ -458,10 +443,12 @@ class ImageResource:
"""
def __init__(self,
image_id: str,
location: Union[PackageResource, str]):
location: Union[PackageResource, str],
premultiplied: bool):
self.image_id = image_id
self.location = location
self.loaded_surface: Optional[pygame.Surface] = None
self.is_file_premultiplied = premultiplied

def load(self) -> Union[Exception, None]:
"""
Expand All @@ -488,8 +475,8 @@ def load(self) -> Union[Exception, None]:
str(self.location))

# perform pre-multiply alpha operation
if error is None and self.loaded_surface is not None:
premul_alpha_surface(self.loaded_surface)
if error is None and self.loaded_surface is not None and not self.is_file_premultiplied:
self.loaded_surface = self.loaded_surface.premul_alpha()

return error

Expand Down
3 changes: 1 addition & 2 deletions pygame_gui/elements/ui_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pygame_gui.core import ObjectID
from pygame_gui.core.interfaces import IContainerLikeInterface, IUIManagerInterface
from pygame_gui.core import UIElement
from pygame_gui.core.utility import premul_alpha_surface
from pygame_gui.core.gui_type_hints import Coordinate, RectLike


Expand Down Expand Up @@ -94,7 +93,7 @@ def set_image(self,
"""
image_surface = new_image.convert_alpha()
if not image_is_alpha_premultiplied:
image_surface = premul_alpha_surface(image_surface)
image_surface = image_surface.premul_alpha()
if (image_surface.get_width() != self.rect.width or
image_surface.get_height() != self.rect.height):
self.original_image = image_surface
Expand Down
4 changes: 4 additions & 0 deletions pygame_gui/elements/ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ def focus(self):
self.drawable_shape.set_text(self.text)
self.drawable_shape.text_box_layout.set_cursor_position(self.edit_position)
self.drawable_shape.apply_active_text_changes()
self.drawable_shape.text_box_layout.turn_on_cursor()
self.redraw()

def process_event(self, event: pygame.event.Event) -> bool:
"""
Expand Down Expand Up @@ -568,6 +570,8 @@ def _process_action_key_event(self, event: pygame.event.Event) -> bool:
self.drawable_shape.text_box_layout.backspace_at_cursor()
self.drawable_shape.text_box_layout.set_cursor_position(self.edit_position)
self.drawable_shape.apply_active_text_changes()
if len(self.text) == 0:
self.redraw()
consumed_event = True
elif event.key == pygame.K_DELETE:
if abs(self.select_range[0] - self.select_range[1]) > 0:
Expand Down

0 comments on commit e9ae2b8

Please sign in to comment.